views:

309

answers:

5

Hi,

I use NSLog in my application. And I'd like to get rid of the annoying beginning of each string: "2009-07-01 21:11:06.508 MyApp[1191:207]".

Is there a way to do so? Probably another logging function?

Thanks.

A: 

You could print to sdterr:

fprintf(stderr, "%s", "Your message");

Edit: Check out this implementation by Karl Kraft

Marco Mustapic
That requires (converting to) a C string, and a format string with %@ in it won't work. It's not a bad idea, but using it directly is not a drop-in replacement for NSLog().
Quinn Taylor
True, you should wrap it in a function with varargs. I use one from Karl Kraft http://www.karlkraft.com/index.php/2009/03/23/114/
Marco Mustapic
I like the debug on/off switch in his approach, but he also doesn't %% escapes (admittedly rare, but problematic when they occur).
Quinn Taylor
+8  A: 

Read this: http://cocoaheads.byu.edu/wiki/different-nslog

It's a wiki page on our CocoaHeads site that explains how to create a "QuietLog" function that does what you're describing. It also shows how to wrap QuietLog into a macro called LocationLog so that it'll print out the file name and line number where you've got the log statement. I use it in all of my projects, and I don't lose stray "NSLog" statements anymore.

Dave DeLong
+1 Dang, I was about to post the same thing. I *wrote* that wiki page... ;-)
Quinn Taylor
And you got it from the Borkware Quickies, so it all evens out. ;)
Dave DeLong
+1  A: 

I like being able to use objective-C objects for format strings and arguments, so I re-wrote NSLog() and added it to my utilities.


+ (void)myLog:(NSString *)formatString, ...
{
    va_list args;
    va_start(args, formatString);
    NSString* output = [[NSString alloc] initWithFormat:formatString arguments:args];
    fprintf(stderr, "%s\n", [output UTF8String]);
    [output release];
    va_end(args);
}
Mark Thalman
If you read the link Dave linked to, you'd see that this is what we do, except we also handle %% entities in the format string.
Quinn Taylor
Quinn, I didn't see that post until after I posted my answer. Also there is something to be said for having the answer up front and not having to follow an external link.
Mark Thalman
No worries. (SO will usually tell you when other answers have ben posted, but overlap is quite normal.) You're right, sometimes it's nice to have the code right in the answer. However, once it passes a certain size, or in this case when the wiki page is likely to change (which it has since Dave's post) it may be better to link.
Quinn Taylor
Also, note that this is a slightly different approach by creating a class method (which is called on that class) rather than a C function like NSLog() that can be used anywhere. Just depends on what the asker would prefer, and the scope of usage.
Quinn Taylor
+1  A: 

Have you tried CFShow()?

lucidbeing
(Added link to docs) I wasn't aware of that function, good to know. However, it's not a drop-in replacement, since you'd have to have an NSString* to cast as a CFStringRef. Still, one could easily define a macro or function to wrap CFShow()...
Quinn Taylor
Unfortunately, CFShow has some very weird behaviour in Leopard which it erroneously hard wraps lines unreadably. For Leopard, its definitely best to avoid CFShow altogether.
Peter N Lewis
A: 

Sorry, unable to comment on original answer above..

The Cocoa Heads link is broken.

Use http://cocoaheads.byu.edu/wiki/different-nslog to find the wiki page.

ohhorob
Thanks, I fixed it in my answer. =)
Dave DeLong