Redirecting log data on the iPhone
You'd think that NSLog would write to stderr, right? Right? Um, no. At least not on the iPhone. This morning I spent way too much time trying to figure out why my freopen([logPath fileSystemRepresentation], "a", stderr); line wasn't working and redirecting NSLog output to my log file.
Sure, I could *create* the log file with this line but no text was actually going there. That is, until I switched from NSLog to CFShow. Then suddenly everything worked. So if you want to redirect output on the iPhone, you'll need to CFShow unless someone has an idea of what NSLog outputs to. stdapple? stdannoying? Let me know.
Update: Since CFShow doesn't seem to take format strings, I went ahead and wrote up the following log function that seems to work fine with stderr on the iPhone. Let me know if you end up using this and if it's useful:
#includevoid dolog(id formatstring,...) { va_list arglist; if (formatstring) { va_start(arglist, formatstring); id outstring = [[NSString alloc] initWithFormat:formatstring arguments:arglist]; fprintf(stderr, "%s\n", [outstring UTF8String]); va_end(arglist); } }
Categories
MacComments (4)
Read More Entries by Erica Sadun.

This worked, Thanks!
I think that GSLog (also found by Erica) makes this tip deprecated
As a sanity check: Is this code missing an autorelease message for outstring?
this was helpful. i can now debug my iphone app. i'm glad you figured it out. thanks!