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!
I've successfully used freopen(...) on the phone to re-direct output to my own file.
This code works for me:
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):
This code works great for me..
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.
Consider using Cocoa Lumberjack. It's a light but flexible utility to replace NSLog functionality. In my opinion it's in the same class as Log4J, allowing for custom appenders and the like. It has an SQLite logger, for example.