Adam Rosenfield
Adam Rosenfield

Reputation: 400612

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.

Upvotes: 13

Views: 11431

Answers (4)

Stephan Burlot
Stephan Burlot

Reputation: 5077

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

Upvotes: 29

Chris R. Donnelly
Chris R. Donnelly

Reputation: 3135

There is also a Console app in the App Store, but it only works for iOS 6 and earlier.

When iOS 7 came out, it effectively stopped working. (It can now only read its own Console logs, not that of the entire system.)

Upvotes: 5

Duncan C
Duncan C

Reputation: 131491

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.

Upvotes: 2

Marc Novakowski
Marc Novakowski

Reputation: 45408

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.

Upvotes: 16

Related Questions