views:

416

answers:

1

XCode's debugger console makes it easy to see any debugging messages my app sends out using NSLog(), but it always sticks a timestamp prefix on them:

2009-08-30 04:54:48.128 MyApp[94652:a0f] some log message
2009-08-30 04:54:50.647 MyApp[94652:a0f] another log message
...

I have no use for this prefix, and it takes up a lot of room. I have a feeling it is hard-coded into Apple's logging system, but just in case there is a solution out there:

Can I have the debugger console show me log messages without the timestamp prefix?

Something like this would be perfect:

some log message
another log message
...
+6  A: 

NSLog() is what is doing that, not the debugger console.

The easiest way to avoid it is to not use NSLog at all. You could use fprintf(), but that is a pain in that it doesn't support %@ format types.

I generally write a function for this:

void MyLog(NSString *format, ...) {
    va_list args;
    va_start(args, format);
    NSString *formattedString = [[NSString alloc] initWithFormat: format
                                                  arguments: args];
    va_end(args);
    [[NSFileHandle fileHandleWithStandardOutput]
        writeData: [formattedString dataUsingEncoding: NSNEXTSTEPStringEncoding];
    [formattedString release];
}

Obviously, modify it to add a newline or use a shorter prefix, etc...

(Fixed the stray ctrl-b)

bbum
I had to use `[NSFileHandle fileHandleWithStandardOutput]` to make it work, but it definitely did the trick. Thank you!
e.James
Sorry about that -- I hit ctrl-b in the middle of typing. Fixed. :)
bbum
`NSNEXTSTEPStringEncoding`? Not `NSUTF8StringEncoding`?
Peter Hosey
An anachronism; take your pick. I've found -- in the past -- that the NeXTSTEP encoding would spew fewer unprintable characters. But UTF8 support in the toolchain is much better these days.
bbum