2

I have integrated a 3rd party framework into my app.

Periodically, this framework writes messages to the log.

Is there any way I can catch those messages in my application code?

I need to extract some useful data from there.

user3666197
  • 1
  • 6
  • 50
  • 92
Rizon
  • 1,516
  • 4
  • 25
  • 45

2 Answers2

0

You say it writes it to a "log" - if it's a file it logs too, then write you own background task that looks at the file size every so often, and if it's changed then post a wakeup to some other part of your code. If its logging to "standard out" or "standard error" (fd==1 or fd==2), you can open your own log file and then reassign the FILE *. Now everything sent to stdout or stderr will get sent to your file.

EDIT: there are numerous posts on SO how to use freemen - its like one line of code - start with this one Rerouting stdin and stdout from C. You may have to experiment with the mode, but I suspect "a" will work, if not try "w". The file will be created if it does not exist.

The code will be something like `freopen(pathToMyLogFile, "a", stdout)', check the return code too. Search for the command in the Xcode document view or use man from the terminal.

Community
  • 1
  • 1
David H
  • 40,852
  • 12
  • 92
  • 138
  • You're right, I meant std-out\std-err. How can I determine which one of them is used by the framework? And more important, how do I change the it to a local file? – Rizon May 28 '13 at 16:54
  • Map first stdout and see if that works - if not try stderr. Its a line of code to change each way. – David H May 28 '13 at 21:05
  • of course I'll also have to clear the file every now and then during the run, so it won't get too big..correct? – Rizon May 28 '13 at 21:58
  • Yes you will have to erase it if it gets too big. – David H May 28 '13 at 22:42
  • Erase it?! in the middle of the execution? then I won't get the following log messages. I'll have to clear it somehow, no? – Rizon May 28 '13 at 23:04
  • What OSX does is move the file every so often then start a new file. Read up,on how it does it or just read up on log file management. I suggest trying to get it to work wt all first. – David H May 29 '13 at 11:08
-1

If you know the logging method of the framework, I mean the method which contains NSLog of your interest, you can swizzle the method with your own and capture the messages before printing it.

In objC method name is mapped to its implementation(IMP). Exchanging method implementation(IMP) is called method swizzling. With method swizzling you can make method A to call IMP B and vice versa. There are many resources available to explain you in detail.

Applicability of this technique depends on the way the framework log information. However this can give you a lead to achieve what you are trying for.

Vignesh
  • 10,205
  • 2
  • 35
  • 73
  • C-functions are not swizzlable. – CodaFi May 28 '13 at 18:26
  • @CodaFi, I know C functions are not swizzlable. There is no mention of C code in the question. I haven't said he can swizzle NSLog either. I was taking about method which contains the log. So I feel the neg vote is unjust. – Vignesh May 29 '13 at 09:27
  • So what's `NSLog()` then? It sure as heck ain't an ObjC method. – CodaFi May 29 '13 at 16:01
  • Did you really understand the answer?. you got to add NSLog any of the ObjC method. i am talking about the wrapper method. "I mean the method which contains NSLog of your interest". – Vignesh May 29 '13 at 16:08
  • And how would you suggest he go about finding said method if it's buried deep within a 3rd party library? – CodaFi May 29 '13 at 16:18
  • And how do you know it's buried deep or not. Y can't it be simple logger method which process passed string and print it in a format. That's y I mentioned applicability depends on the way framework is implemented. Nevertheless the reason for down voting is not justified. It may be far fetched solution but its not wrong in anyway. – Vignesh May 29 '13 at 17:31