开发者

Customise NSLog so it shows less info

by default NSLog outputs a long string before the requested output,

e.g:

NSLog(@"Log message");

Outputs to the console:

2011-04-15 11:23:01.692 MyAppName[23160:903] Log message

I know I can add the filename and line number to the log, but how do I get rid of all the date, time and app name that appears before the message?

I find it really clutters the console in Xcode making it harder to find th开发者_StackOverflow中文版e information I'm after.


This is definitely the FIRST thing I do on a new project. NSLog(…) has diarrhea of the mouth. Here is a basic macro that lets you get some peace and quiet.. AND log basic objects without the annoying NSLog(@"%@", xYz); syntax (instead you just NSLog(xYz);).

#define NSLog(fmt...) NSShutUp(__PRETTY_FUNCTION__,fmt)
#define UTF8FMT(fmt,argL) \
       [NSString.alloc initWithFormat:fmt arguments:argL].UTF8String

void NSShutUp(const char*func, id fmt, ...) {
    if (![fmt isKindOfClass:NSString.class]) 
    // it's not a string (aka. the formatter), so print it)
      fprintf (stderr, "%s: %s\n", func, 
       [[NSString stringWithFormat:@"%@",fmt,nil]UTF8String]); 
    else {      va_list argList;     va_start (argList, fmt);
      fprintf (stderr, "%s: %s\n", func, UTF8FMT(fmt,argList)); 
      va_end  (argList);
}   }

/*  SAMPLE RUN */

int main (void) { NSString *a; NSNumber *b; NSArray *c;
    NSLog(a = @"Ahh, silence."    );
    NSLog(b = @(M_PI)             );
    NSLog(c = @[@"Arrays, baby!"] );
    // Old syntax still works.
    NSLog(@"%@ * %@ * %@",a,b,c);
    return 0;

}

OUTPUT

int main(): Ahh, silence.
int main(): 3.141592653589793
int main(): (
    "Arrays, baby!"
)
int main(): Ahh, silence. * 3.141592653589793 * (
    "Arrays, baby!"
)


i would recommend that you start using a better alternatives to NSlog like SOSMAX or NSLogger. Here is a bit overview of both of them http://learning-ios.blogspot.com/2011/05/better-nslog-ing.html

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜