XCode的调试器控制台可以很容易地看到我的应用发出的任何调试消息NSLog(),但它总是在它们上面添加一个时间戳前缀:
2009-08-30 04:54:48.128 MyApp[94652:a0f] some log message
2009-08-30 04:54:50.647 MyApp[94652:a0f] another log message
...
Run Code Online (Sandbox Code Playgroud)
我没有使用这个前缀,它占用了很多空间.我有一种感觉它被硬编码到Apple的日志记录系统中,但以防万一有一个解决方案:
我可以让调试器控制台显示没有时间戳前缀的日志消息吗?
像这样的东西是完美的:
some log message
another log message
...
Run Code Online (Sandbox Code Playgroud) 我几乎用这段代码完成了一个干净的NSLog:
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, __VA_ARGS__] UTF8String]);
Run Code Online (Sandbox Code Playgroud)
如果我这样做,这工作正常:
NSLog(@"Show %@ message", @"this");
Run Code Online (Sandbox Code Playgroud)
但是,如果我使用它将会失败
NSLog(@"One argument");
Run Code Online (Sandbox Code Playgroud)
因为__VA_ARGS__什么都没有,所以它产生了
printf("%s\n", [[NSString stringWithFormat:@"One argument",] UTF8String]);
Run Code Online (Sandbox Code Playgroud)
所以,问题是逗号.因为这是宏,__VA_ARGS__没什么.所以我不能做__VA_ARGS__==nil因为会产生==nil而且会失败的事情.
问题很简单:什么时候__VA_ARGS__什么都不做?或者只有在有更多参数时才使用逗号.
NSLog()是否有不带时间戳和日期戳的打印变体,以及自动换行?
谢谢.现在使用以下代码,我可以打印NSString,cString或对象:
#import <Foundation/Foundation.h>
#import <stdio.h>
int main (int argc, const char * argv[]) {
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
NSString *s = @"Hello, World!";
NSDate *today = [NSDate date];
NSLog(@"%@", s);
printf("%s at %s", [s UTF8String], [[today description] UTF8String]);
[pool drain];
return 0;
}
Run Code Online (Sandbox Code Playgroud) 我用的时候
NSLog(@"fooBar")
Run Code Online (Sandbox Code Playgroud)
它打印出很多我不想要的东西:
2009-09-03 13:46:34.531 MyApp[3703:20b] fooBar
Run Code Online (Sandbox Code Playgroud)
有没有办法在没有这个大前缀的情况下将某些内容打印到控制台?我想在控制台中画一张桌子和其他东西,这样空间才是至关重要的......