Log Messages

One of the things that’s going to start to irritate you fairly quickly is how hard it is to debug applications using the Redpark cable. With the cable plugged in, you’re going to be unable to see output to the debug console: for instance, log messages you might normally print to the console using the NSLog method.

Fortunately, there is a way around this. We can actually redirect the stderr output that would normally appear in the Debug area to a file, which we can then view inside our application itself.

Open up the SerialConsole project in Xcode and click on the main.m file, which is located in the Supporting Files group in the Project pane, and add the highlighted lines below to it:

#import <UIKit/UIKit.h>

int main(int argc, char *argv[]){
    NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];

    NSArray *paths =
       NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *log = [[paths objectAtIndex:0] stringByAppendingPathComponent: @"ns.log"];

    NSFileManager *fileMgr = [NSFileManager defaultManager];
    [fileMgr removeItemAtPath:log error:nil];

    freopen([log fileSystemRepresentation], "a", stderr);

    int retVal = UIApplicationMain(argc, argv, nil, nil);
    [pool release];
    return retVal;
}

This will redirect the stderr output to a file called ns.log inside the application’s Documents directory. The file will be deleted and recreated each time the application is started.

Now that we’ve done this, we need to create a view controller to allow us to ...

Get iOS Sensor Apps with Arduino now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.