3

I am trying to redirect all NSLog output to be able to see all my logs in a UITextView inside the app (for debug purposes, and maybe later to log everything using TestFlight). I can't use the well-known DLog because I want to catch logs from frameworks I don't own.

I'm doing this by 'redirecting' stderr to a pipe and reading from this pipe. Anyway, this works well on the simulator and on the device when the app is launched from within Xcode. But, this does not work when the app is launched "by hand" on the device.

It seems that NSLog calls have no effect on the device: I don't read anything from the pipe. However, if I manually write on stderr : fprintf(stderr, "test") I can read it from the pipe, so the problem is not with stderr but rather with the NSLog function.

Why does it do nothing on devices ? Is there a way to make it write on stderr even on the device ?

If it is not possible to use NSLog on devices, is there another way to gather logs from external frameworks ?

Thank you.

  • I was under the impression that the NSLog outputs are not processed unless it's run through a debugger. Obviously there's no debugger when the app is launched by hand, but there is when launched in the simulator. The lines of code occupied by NSLog are effectively ignored when run in a production environment i.e. on the device. – Dan Hanly Jul 05 '12 at 15:35
  • You can access the NSLog output by looking at the device with Organizer. – Hot Licks Jul 05 '12 at 15:41
  • Thank you @DanielHanly. I hope there is a way to trick the device into thinking it's running in a "debug environment". How am I supposed to log what is happening on TestFlight then ? Hot Licks : Well, I want some kind of real-time logging, and I also need to send the logs to TestFlight. – user1504454 Jul 05 '12 at 15:47
  • I think the NSLogs go to syslog, not stderr. – C0deH4cker Jul 05 '12 at 16:00
  • @DanielHanly, it not ignored in production environment. NSLog will output to device console log all the time. If you wrap NSLog into a macro, you can control the output for DEBUG and RELEASE mode. – Black Frog Jul 05 '12 at 17:26

2 Answers2

4

NSLog goes to the system log on devices. You can access the system log using the C API asl (Apple System Log); also check out this blog post.

However if you're using TestFlight it's much easier to just replace/append those NSLog statements with the TFLog statement.

TFLog(@"This comment is live on TestFlight.");
dmur
  • 530
  • 3
  • 14
0

If you want some UILogging, you could consider using ULog, a UIAlertView based Logging #define directive.

// ULog will show the UIAlertView only when the DEBUG variable is set 

#ifdef DEBUG
#   define ULog(fmt, ...)  { UIAlertView *alert = [[UIAlertView alloc] initWithTitle:[NSString stringWithFormat:@"%s\n [Line %d] ", __PRETTY_FUNCTION__, __LINE__] message:[NSString stringWithFormat:fmt, ##__VA_ARGS__]  delegate:nil cancelButtonTitle:@"Ok" otherButtonTitles:nil]; [alert show]; }
#else
#   define ULog(...)
#endif

(source: http://overbythere.co.uk/blog/2012/01/alternatives-nslog)

Pascal
  • 15,257
  • 2
  • 52
  • 65