James Eichele
James Eichele

Reputation: 119164

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

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

Upvotes: 34

Views: 11829

Answers (8)

bbum
bbum

Reputation: 162722

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]];

}

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

(Fixed the stray ctrl-b)

Upvotes: 27

ColdLogic
ColdLogic

Reputation: 7275

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.

Upvotes: 7

Albert Renshaw
Albert Renshaw

Reputation: 17902

Clean Log Macro

Here is a macro I've made for this purpose. It works exactly like NSLog but with just your text, no extra info


Macro (paste to your .h)

#define CLog(__string, ...) fprintf(stderr, "\n%s", [([NSString stringWithFormat:__string, ##__VA_ARGS__]) UTF8String])

Example use:

CLog(@"I am %i days and %i years old", 3, 7);

Logs:

I am 3 days and 7 years old

Upvotes: 1

larod
larod

Reputation: 435

This is way more easier than the suggested solutions. Using carelesslyChoosy's solution and adding a little bit more to make it log entries on release builds only you get the macro below. Just add this macro to your header or .pch file. This macro will show log entries when the DEBUG flag is enabled, on release builds you won't see log entries.

#ifdef DEBUG
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#define Log(x, ...) NSLog(@"%s %d: " x, __FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#define Log(x, ...)
#endif

Upvotes: 1

Inder Kumar Rathore
Inder Kumar Rathore

Reputation: 39988

Define a macro

#if __has_feature(objc_arc)
  #define MDLog(format, ...) CFShow((__bridge CFStringRef)[NSString stringWithFormat:format, ## __VA_ARGS__]);
#else
  #define MDLog(format, ...) CFShow([NSString stringWithFormat:format, ## __VA_ARGS__]);
#endif

And use this macro in you code like

NSLog(@"some log message");
MDLog(@"some log message");

Here is the output of console

NSLog->2014-01-28 10:43:17.873 TestApp[452:60b] some log message
MDLog -> some log message


P.S.

If anyone wants Custom Logs which gives you more info like method name / line number etc. can download the open source MLog.h on GitHub.

Upvotes: 13

Abhinav Singh
Abhinav Singh

Reputation: 8100

put in this one line code in your .pch file and you are done

#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);

notice the ## part

Upvotes: 9

TONy.W
TONy.W

Reputation: 1946

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__)

Upvotes: 5

JackRabbit
JackRabbit

Reputation: 11

In the top left corner of the console window there is a pulldown menu that says All Output / Debugger Output / Target Output.

Select Target Output. It worked on my older versions of Xcode, but to be honest with you it doesn't with my current 4.3 version.

I hope this helps you.

JR

Upvotes: 1

Related Questions