自定义NSLog,以便它显示更少的信息 [英] Customise NSLog so it shows less info
问题描述
默认情况下NSLog在请求的输出之前输出一个长字符串,
例如:
<
NSLog(@Log message);
输出到控制台:
2011-04-15 11:23:01.692 MyAppName [23160:903]日志消息
我知道我可以添加文件名和行号到日志,但如何摆脱所有的日期,时间和应用程序名称之前的消息?
我发现它真的在Xcode中混乱了控制台,使我很难找到我之后的信息。
这绝对是我在一个新项目做的第一件事。 NSLog(...)
有腹泻。这里是一个基本的宏,让你得到一些和平与安静 ..和日志基本对象没有恼人的 NSLog(@%@,xYz);
语法(而不是 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])
//它不是字符串。格式化程序),所以打印它)
fprintf(stderr,%s:%s\\\
,func,
[[NSString stringWithFormat:@%@,fmt,nil] UTF8String ]);
else {va_list argList; va_start(argList,fmt);
fprintf(stderr,%s:%s\\\
,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!]);
//旧语法仍然有效。
NSLog(@%@ *%@ *%@,a,b,c);
return 0;
}
OUTPUT
int main():Ahh,silence。
int main():3.141592653589793
int main():(
Arrays,baby!
)
int main():Ahh, * 3.141592653589793 *(
Arrays,baby!
)
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.
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!"
)
这篇关于自定义NSLog,以便它显示更少的信息的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!