Customise NSLog so it shows less info

杀马特。学长 韩版系。学妹 提交于 2020-01-23 17:51:47

问题


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 the information I'm after.


回答1:


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!"
)



回答2:


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



来源:https://stackoverflow.com/questions/5673659/customise-nslog-so-it-shows-less-info

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!