I need to merge stderr and stdout because I want my debug and the exceptions in the same log file. I tried
NSString *logPath = [NSHomeDirectory() stringByAppendingPathComponent:@"Library/Tag Monster/log.log"];
freopen([logPath fileSystemRepresentation], "a", stderr);
freopen([logPath fileSystemRepresentation], "a", stdout);
but this messes up the order. It prints the stderr messages at the top of the file.
Or is there a better way to log in cocoa? NSLog just spams the syslog 😛
Edit:
Thants my log macro:
#ifdef DEBUG_MODE_LEVEL_KEEP
#define DLogK(...) (void)printf("%s: %s\n", __PRETTY_FUNCTION__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String])
#else
#define DLogK(...)
#endif
If I just redirect stderr to a logfile and log with
fprintf(stderr, "%s: %s\n", __PRETTY_FUNCTION__, [[NSString stringWithFormat:__VA_ARGS__] UTF8String])
it does not work either. Shouldn’t that just work?
Thanks
Using
dup2()inunistd.h, you could close bothstderrandstdoutand re-direct them to a file. For instance:Now both
stderrandstdout, when used, will write to my_log_file.txt. While I’m not sure about iOS, this should work perfectly fine on OSX.