28

I would like to redirect NSog() to file, but still to see the output in console. I am aware that stderr can be redirected to file using:

freopen("file.log", "a+", stderr);

but after redirecting it to file, the log is no more shown in the console output.

I could build a custom wrapper around NSLog() but I would not get logged crash logs that are written to stderr in the moment of app crash.

I was also experimenting with dup() and other methods for duplicating file descriptors, but the output was ether in file or in console, never in both.

Similar questions was asked here: Write stderr on iPhone to both file and console but without accepted answer, or with suggestion to use a NSLog() wrapper.

Has anyone an idea on how to manage this work? Thanx in advance.

UPDATE: The most important part of redirecting is to have system error logs (stderr) written to both console and file.

Community
  • 1
  • 1
Miroslav Kovac
  • 1,168
  • 1
  • 16
  • 27
  • What if you do two separate logs, send one to file, and one to console, but keep them the same? – Jtaylorapps Mar 08 '12 at 15:05
  • How do you suggest to manage this? The most important thing is to have system error logs written to both console and file. – Miroslav Kovac Mar 08 '12 at 15:11
  • 2
    `NSLog` by default logs to console, maybe you could just extract your logs from there http://stackoverflow.com/questions/7150849/how-can-i-get-a-console-readout-at-runtime-in-an-application/7151773#7151773 – Joe Mar 08 '12 at 15:15
  • Sorry, I didn't understand that you were doing this for error logs, I skimmed through and assumed it was just use of NSLog – Jtaylorapps Mar 08 '12 at 15:24

2 Answers2

35

The way we have implemented is:

if (!isatty(STDERR_FILENO)) {
    // Redirection code
}

This is based on general assumption that if a console is attached, you do not need to store those logs in the file since you are already debugging. So whenever the console is attached, logs will be printed there, otherwise saved to a file.

Sailesh
  • 25,517
  • 4
  • 34
  • 47
9

According to the docs, you will need a custom log facility.

You need at a minimum, a function that takes variadic arguments and printd to NSLog and fprintf, something like:

void myLog(NSString* format, ...)
{
    va_list argList;
    va_start(argList, format);
    NSString* formattedMessage = [[NSString alloc] initWithFormat: format arguments: argList];
    va_end(argList);
    NSLog(@"%@", formattedMessage);
    fprintf(myFileDescriptor, "%s\n", [formattedMessage UTF8String]);
    [formattedMessage release]; // if not on ARC
}

Or there are Cocoa logging frameworks.

Looking for a Specific Cocoa Logging Framework

Community
  • 1
  • 1
JeremyP
  • 84,577
  • 15
  • 123
  • 161
  • Thanks, this is good, but check my updated question. I am looking for some way to get system's stderr logs to both console and file, not only my custom logs. Thanks. – Miroslav Kovac Mar 08 '12 at 16:06
  • @Miroslav: I don't think that's possible without a similar change to what I have given you. – JeremyP Mar 09 '12 at 13:51