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