views:

1364

answers:

3

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.

+9  A: 

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.

Marc Novakowski
Good call - I'm new to iPhone development, and the only way I knew how to see the output of NSLog() was in the debugger console.
Adam Rosenfield
+7  A: 

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 filed, 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 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

Stephan Burlot
One tiny little change, if you're concerned about the size of the log, is to change the "a+" to "w" in freopen(). But that creates the other caveat, be sure to get this file between program runs as it will be recreated from scratch each time the program is run (this can be as great a hindrance as a 'too big' file)
KevinDTimm
A: 

Marc Novakowski is right. The console output gets logged on the phone, and when you connect your Mac to your phone, the console output is available in the organizer window.

Just select your phone in the devices panel, and select the console tab. The recent console output will be right there. Very cool.

I just figured this out. That will be very helpful in situations like you describe where you need to be away from your computer.

BTW, I have a solution to your need to test weak WiFi signals. Just wrap your phone in aluminum foil, with the USB cable sticking out from the foil wrapper. If you are running with it tethered you can single-step through the code in the debugger. You can't manipulate the user interface, but you can degrade/eliminate radio signals like 3D, WiFi, and GPS. If you want to degrade the signal strength but not cancel it completely, you may need to experiment with partly covering the phone with foil, putting small holes in the covering, etc.

One caution: Don't leave the phone wrapped in foil for more than a few minutes at a time. It will likely overheat.

Duncan C