Alex Rivera | Logout

In Xcode, is there a way to disable the timestamps that appear in the debugger console when calling NSLog?

Asked 2009-08-30T19:40:24.617
34

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
...
Edit
Report

2 Answers

7

A way to keep using NSLog in conjunction with bbum's answer is to use a preprocessor macro to redefine NSLog to your own function

#define USECUSTOMLOGS 1
#if USECUSTOMLOGS
#define NSLog MyLog
#endif

This will replace NSLog with MyLog on compile time. Basically you can keep using NSLog everywhere and it will still use your custom format for the console window. You can also change it back to use NSLog at anytime by changing the 1 to a 0.

answered 2011-04-21T00:23:16.063
5

ARC Version:

void NFLog(NSString *format, ...)
{
    va_list args;
    va_start(args, format);
    NSString *formattedString = [NSString stringWithFormat:format, args];
    formattedString = [formattedString stringByAppendingString:@"\n"];
    va_end(args);
    [[NSFileHandle fileHandleWithStandardOutput] writeData: [formattedString dataUsingEncoding: NSUTF8StringEncoding]];
}

update:

What I am doing is add this code to xxx-Prefix.pch then you can use it anywhere:

#define newLine         do { [(NSFileHandle*)[NSFileHandle fileHandleWithStandardOutput] writeData:[@"\n" dataUsingEncoding: NSUTF8StringEncoding]]; } while(0);
#define NFLog(args,...) do { [(NSFileHandle*)[NSFileHandle fileHandleWithStandardOutput] writeData:[[NSString stringWithFormat:args, ##__VA_ARGS__] dataUsingEncoding: NSUTF8StringEncoding]]; } while(0); newLine

and if you want NSLog back:

#define NFLog(args,...) NSLog(args,##__VA_ARGS__)
answered 2012-12-14T16:35:01.720

Your Answer