Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a way to capture the output of NSLog on an iPhone when not connected to a debugger?

I'm logging a bunch of data with NSLog(). Is there a way to capture the log data when my iPhone is not connected to my development machine and running under a debugger?

For example, can I redirect it to a file and then read the log file back through Xcode at a later point in time? I need to do this in order to test my app when the WiFi is poor, which necessitates that I go far away from my desk.

like image 483
Adam Rosenfield Avatar asked Jan 09 '09 18:01

Adam Rosenfield


People also ask

How do I view NSLog on my Iphone?

To see the logs, you need to open Xcode, click the 'Window' menu item, and then 'Organizer'. Then select your device and then select the 'Device Logs' tab. For some reason (for me at least) viewing the logs seems flaky, so if nothing shows up, you may need to completely quit Xcode and restart it.

Where does NSLog write to?

NSLog outputs messages to the Apple System Log facility or to the Console app (usually prefixed with the time and the process id).

How do I print a log in Objective C?

In order to print logs, we use the NSLog method in Objective-C programming language which we have used right from the Hello World example. Now, when we compile and run the program, we will get the following result. 2013-09-16 00:32:50.888 demo[16669] Hello, World!


2 Answers

The method below will create a file name “console.log” in the Documents folder of your application so you can later read it.

Call this method at the beginning of your program:

- (void) redirectConsoleLogToDocumentFolder
{
  NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
  NSString *documentsDirectory = [paths objectAtIndex:0];
  NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"];
  freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
}

The log will never be erased, so use with caution.

Once you have tested your app in the field, reconnect your phone to your Mac, in Xcode, open the Organizer. In the Summary panel, you have the list of all the apps on your phone. Expand the one you're debugging, and you'll see a package named "Application Data".

Click the arrow on the right of its name and save this. You'll end with a folder with a name of your Bundle Identifier followed by a date.

Inside this folder you'll find your Documents Folder, which should contain the console.log

like image 125
Stephan Burlot Avatar answered Oct 01 '22 22:10

Stephan Burlot


I'm pretty sure that NSLog() calls will be written to the system console log, so if you connect your iPhone to your computer after being offline, you should be able to look at the console log in XCode Organizer. The only caveat is that the console log is limited in size so older entries may be bumped off if you do a lot of logging.

like image 30
Marc Novakowski Avatar answered Oct 01 '22 22:10

Marc Novakowski