在 Xcode 中,有没有办法禁用调用 NSLog 时出现在调试器控制台中的时间戳?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1354728/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Xcode, is there a way to disable the timestamps that appear in the debugger console when calling NSLog?
提问by e.James
Xcode's debugger console makes it easy to see any debugging messages my app sends out using NSLog()
, but it always sticks a timestamp prefix on them:
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
...
I have no use for this prefix, and it takes up a lot of room. I have a feeling it is hard-coded into Apple's logging system, but just in case there is a solution out there:
这个前缀我没用,占了很大的空间。我有一种感觉,它被硬编码到 Apple 的日志系统中,但以防万一有解决方案:
Can I have the debugger console show me log messages withoutthe timestamp prefix?
我可以让调试器控制台显示没有时间戳前缀的日志消息吗?
Something like this would be perfect:
这样的事情将是完美的:
some log message
another log message
...
采纳答案by bbum
NSLog() is what is doing that, not the debugger console.
NSLog() 是这样做的,而不是调试器控制台。
The easiest way to avoid it is to not use NSLog at all. You could use fprintf(), but that is a pain in that it doesn't support %@ format types.
避免它的最简单方法是根本不使用 NSLog。您可以使用 fprintf(),但这很麻烦,因为它不支持 %@ 格式类型。
I generally write a function for this:
我通常为此编写一个函数:
void MyLog(NSString *format, ...) {
va_list args;
va_start(args, format);
NSString *formattedString = [[NSString alloc] initWithFormat: format
arguments: args];
va_end(args);
[[NSFileHandle fileHandleWithStandardOutput]
writeData: [formattedString dataUsingEncoding: NSNEXTSTEPStringEncoding]];
}
Obviously, modify it to add a newline or use a shorter prefix, etc...
显然,修改它以添加换行符或使用较短的前缀等......
(Fixed the stray ctrl-b)
(修复了流浪的 ctrl-b)
回答by Inder Kumar Rathore
Define a macro
定义一个宏
#if __has_feature(objc_arc)
#define MDLog(format, ...) CFShow((__bridge CFStringRef)[NSString stringWithFormat:format, ## __VA_ARGS__]);
#else
#define MDLog(format, ...) CFShow([NSString stringWithFormat:format, ## __VA_ARGS__]);
#endif
And use this macro in you code like
并在你的代码中使用这个宏
NSLog(@"some log message");
MDLog(@"some log message");
Here is the output of console
这是控制台的输出
NSLog->2014-01-28 10:43:17.873 TestApp[452:60b] some log message
MDLog -> some log message
NSLog-> 2014-01-28 10:43:17.873 TestApp[452:60b] some log message
MDLog ->some log message
P.S.
聚苯乙烯
If anyone wants Custom Logs which gives you more info like method name / line number etc. can download the open source MLog.h on GitHub
.
如果有人想要自定义日志,它可以为您提供更多信息,例如方法名称/行号等,可以下载开源MLog.h on GitHub
。
回答by Abhinav Singh
put in this one line code in your .pch file and you are done
将这一行代码放入您的 .pch 文件中,您就完成了
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
notice the ## part
注意##部分
回答by ColdLogic
A way to keep using NSLog in conjunction with bbum's answer is to use a preprocessor macro to redefine NSLog to your own function
将 NSLog 与 bbum 的答案结合使用的一种方法是使用预处理器宏将 NSLog 重新定义为您自己的函数
#define USECUSTOMLOGS 1
#if USECUSTOMLOGS
#define NSLog MyLog
#endif
This will replace NSLog with MyLog on compile time. Basically you can keep using NSLog everywhere and it will still use your custom format for the console window. You can also change it back to use NSLog at anytime by changing the 1 to a 0.
这将在编译时用 MyLog 替换 NSLog。基本上你可以在任何地方继续使用 NSLog,它仍然会使用你的控制台窗口的自定义格式。您也可以随时通过将 1 更改为 0 将其改回使用 NSLog。
回答by TONy.W
ARC Version:
ARC版本:
void NFLog(NSString *format, ...)
{
va_list args;
va_start(args, format);
NSString *formattedString = [NSString stringWithFormat:format, args];
formattedString = [formattedString stringByAppendingString:@"\n"];
va_end(args);
[[NSFileHandle fileHandleWithStandardOutput] writeData: [formattedString dataUsingEncoding: NSUTF8StringEncoding]];
}
update:
更新:
What I am doing is add this code to xxx-Prefix.pch then you can use it anywhere:
我正在做的是将此代码添加到 xxx-Prefix.pch 然后你可以在任何地方使用它:
#define newLine do { [(NSFileHandle*)[NSFileHandle fileHandleWithStandardOutput] writeData:[@"\n" dataUsingEncoding: NSUTF8StringEncoding]]; } while(0);
#define NFLog(args,...) do { [(NSFileHandle*)[NSFileHandle fileHandleWithStandardOutput] writeData:[[NSString stringWithFormat:args, ##__VA_ARGS__] dataUsingEncoding: NSUTF8StringEncoding]]; } while(0); newLine
and if you want NSLog back:
如果你想要 NSLog 回来:
#define NFLog(args,...) NSLog(args,##__VA_ARGS__)
回答by larod
This is way more easier than the suggested solutions. Using carelesslyChoosy's solution and adding a little bit more to make it log entries on release builds only you get the macro below. Just add this macro to your header or .pch file. This macro will show log entries when the DEBUG flag is enabled, on release builds you won't see log entries.
这比建议的解决方案更容易。使用 carelessChoosy 的解决方案并添加更多内容以使其在发布版本上记录条目,只有您才能获得下面的宏。只需将此宏添加到您的标题或 .pch 文件中即可。此宏将在启用 DEBUG 标志时显示日志条目,在发布版本中您将看不到日志条目。
#ifdef DEBUG
#define NSLog(FORMAT, ...) printf("%s\n", [[NSString stringWithFormat:FORMAT, ##__VA_ARGS__] UTF8String]);
#define Log(x, ...) NSLog(@"%s %d: " x, __FUNCTION__, __LINE__, ##__VA_ARGS__)
#else
#define Log(x, ...)
#endif
回答by Albert Renshaw
Clean Log Macro
清理日志宏
Here is a macro I've made for this purpose. It works exactly like NSLog but with just your text, no extra info
这是我为此目的制作的宏。它的工作原理与 NSLog 完全一样,但只有您的文本,没有额外的信息
Macro (paste to your .h)
宏(粘贴到您的 .h)
#define CLog(__string, ...) fprintf(stderr, "\n%s", [([NSString stringWithFormat:__string, ##__VA_ARGS__]) UTF8String])
Example use:
使用示例:
CLog(@"I am %i days and %i years old", 3, 7);
Logs:
日志:
I am 3 days and 7 years old
I am 3 days and 7 years old
回答by HymanRabbit
In the top left corner of the console window there is a pulldown menu that says All Output / Debugger Output / Target Output.
在控制台窗口的左上角有一个下拉菜单,上面写着所有输出/调试器输出/目标输出。
Select Target Output. It worked on my older versions of Xcode, but to be honest with you it doesn't with my current 4.3 version.
选择目标输出。它适用于我的旧版本 Xcode,但老实说,它不适用于我当前的 4.3 版本。
I hope this helps you.
我希望这可以帮助你。
JR
JR