Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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 ... 
like image 247
e.James Avatar asked Aug 30 '09 19:08

e.James


People also ask

How do I check Xcode logs?

In the Xcode menu hit Run - Console. This is where NSLog / print / printf etc statements output. The key command is Command + Shift + R.

Is there a console in Xcode?

In Xcode 4 short cut to display and hide console is (command-shift-Y) , this will show the console and debugger below ur text edior in the same window.

How do I show debugger in Xcode?

When you run an application in Xcode, the debugger is automatically started and attached to the process of the application. Click the Run button in the top left or press Command + R. From the moment the application is up and running, we can start inspecting the process and, if necessary, debug it.


2 Answers

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)

like image 160
bbum Avatar answered Oct 27 '22 18:10

bbum


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.

like image 35
Inder Kumar Rathore Avatar answered Oct 27 '22 19:10

Inder Kumar Rathore