views:

8371

answers:

7

What would be the best way to write log statements to a file or database in an iPhone application?

Ideally, NSLog() output could be redirected to a file using freopen(), but I've seen several reports that it doesn't work. Does anyone have this going already or have any ideas how this might best be done?

Thanks!

+4  A: 

I've successfully used freopen(...) on the phone to re-direct output to my own file.

Ben Gottlieb
Do you remember which arguments you used?
Mike McMaster
I used freopen([newFileName UTF8String], "w+", stderr), which redirected all console output to logFileName.
Ben Gottlieb
Just to follow up - this worked fine. The only bummer is that NSLog output stops appearing on the console after calling freopen, but it's a pretty minor issue.
Mike McMaster
+15  A: 

If you want to use Cocoa, NSString and NSData have methods for reading/writing to file and NSFileManager gives you file operations. Here's an example (should work on iPhone):

NSData *dataToWrite = [[NSString stringWithString:@"String to write"] dataUsingEncoding:NSUTF8StringEncoding];

NSString *docsDirectory = [NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) objectAtIndex:0];
NSString *path = [docsDirectory stringByAppendingPathComponent:@"fileName.txt"];

// Write the file
[dataToWrite writeToFile:path atomically:YES];

// Read the file
NSString *stringFromFile = [[NSString alloc] initWithContentsOfFile:path];  

// Check if file exists
NSFileManager *fileManager = [NSFileManager defaultManager];
[fileManager fileExistsAtPath:path]; // Returns a BOOL    

// Remove the file
[fileManager removeItemAtPath:path error:NULL];

// Cleanup
[stringFromFile release];
[fileManager release];
Martin Gordon
Thanks. It looks like:[fileManager removeItemAtIndexPath:path error:NULL];should be:[fileManager removeItemAtPath:path error:NULL]; Works fine otherwise. I'll leave the question open for now to see what else people suggest. This is helpful though.
Mike McMaster
Oops. Just fixed that. I was doing some table view programming while answering your question and this slipped through.
Martin Gordon
Nice short sample, thank you.
Jirapong
A: 

Hi Mike

Had any luck? I tried what ben wrote but nothing was written to the file. If you have any conclusions please post them.

Thanks

Gidon

Hi Gidon. I'm planning to update this question - just need to get a few things done first. :-)
Mike McMaster
+3  A: 

This code works for me:

- (void)applicationDidFinishLaunching:(UIApplication *)application {
#if TARGET_IPHONE_SIMULATOR == 0
    freopen([@"/tmp/my_logs.txt" fileSystemRepresentation], "w", stderr);
#endif
}
nst
A: 

how to limit the log size?

Hikaru
+1  A: 

hi all. This code works great for me..

#if TARGET_IPHONE_SIMULATOR == 0
    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSString *logPath = [documentsDirectory stringByAppendingPathComponent:@"console.log"];
    freopen([logPath cStringUsingEncoding:NSASCIIStringEncoding],"a+",stderr);
#endif

You can then get the log file off the iphone using the method outlined here http://blog.coriolis.ch/2009/01/09/redirect-nslog-to-a-file-on-the-iphone/#more-85

Note that using freopen will STOP THE CONSOLE IN XCODE working.. however, for some reason the console you can view in xcode's organiser still works great.

Ben Clayton
A: 

[[NSString stringWithString:@"String to write"] works well. Tried to put a "£" in, and you get the "£" but with a symbol infront of it. Can anyone suggest how to get rid of this symbol?

Harry