如何在 Xcode 中过滤控制台输出
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7900013/
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
How to filter console output in Xcode
提问by ArtFeel
In my iOS project, I use one 3rd-party library, which is is incredible spamming into the console output. Can I apply any filtering to debug output.
在我的 iOS 项目中,我使用了一个 3rd-party 库,这是一个令人难以置信的控制台输出垃圾邮件。我可以对调试输出应用任何过滤吗?
采纳答案by david
Works in Xcode 5.0 through 7.3, Apple no longer supports Xcode plug-ins as of Xcode 8.0.
适用于 Xcode 5.0 到 7.3,Apple 不再支持 Xcode 8.0 起的 Xcode 插件。
MCLogshould be what you're looking for.
MCLog应该是你要找的。
This is an XCode plugin.
这是一个 XCode 插件。
回答by Jano
If the library is using NSLog you can redefine it and discard the log message when it comes from the library. Example code:
如果库使用 NSLog,您可以重新定义它并丢弃来自库的日志消息。示例代码:
#define NSLog(args...) [[Logger singleton] debugWithLevel:kDebug line:__LINE__ funcName:__PRETTY_FUNCTION__ message:args];
// poor man's nslog
@interface Logger : NSObject
typedef enum {
kTrace=0, kDebug=1, kInfo=2, kWarn=3, kError=4, KSilent=5
} LoggerLevel;
// ...
@implementation Logger
+(Logger *)singleton {
static dispatch_once_t pred;
static Logger *shared = nil;
dispatch_once(&pred, ^{
shared = [[Logger alloc] init];
shared.logThreshold = kTrace;
});
return shared;
}
-(void) debugWithLevel:(LoggerLevel)level
line:(int)line
funcName:(const char *)funcName
message:(NSString *)msg, ... {
va_list ap;
va_start (ap, msg);
msg = [[[NSString alloc] initWithFormat:msg arguments:ap] autorelease];
va_end (ap);
msg = [NSString stringWithFormat:@"%s %50s:%3d - %@", levelName[level], funcName, line, msg];
// ... filter by class name ...
fprintf(stdout,"%s\n", [msg UTF8String]);
}
@end
Note that funcName
contains the classname and method sending the message. If the library is a good citizen and has classes that start with a prefix, discard the output if the className starts with that. Otherwise you have to load a list of classes from that library and check them before the fprintf line.
请注意,它funcName
包含发送消息的类名和方法。如果库是一个好公民并且有以前缀开头的类,如果 className 以前缀开头,则丢弃输出。否则,您必须从该库加载类列表并在 fprintf 行之前检查它们。
This of course, doesn't duplicate the log to syslogd like NSLog does, but who cares. :P
这当然不会像 NSLog 那样将日志复制到 syslogd,但谁在乎呢。:P
回答by djromero
It depends if you're using directly the 3rd party library source code in your project or a binary library.
这取决于您是直接使用项目中的第 3 方库源代码还是二进制库。
If you're using the source code I'd suggest to check what they are using to log the messages. It may have a way to reduce the verbosity. If they are using plain NSLog
the only option would be to redefine NSLog
in order to do some filtering, as Jano proposed you.
如果您使用的是源代码,我建议您检查它们用于记录消息的内容。它可能有一种减少冗长的方法。如果他们使用普通NSLog
的,唯一的选择是重新定义NSLog
以进行一些过滤,正如 Jano 建议的那样。
If they are using low level functions like printf
and the like, your best option is to replace them with your own custom logging macro, like:
如果他们使用诸如printf
之类的低级函数,您最好的选择是用您自己的自定义日志记录宏替换它们,例如:
#ifdef DEBUG_3P
#define LOG_3P(str) NSLog(@"%s", str)
#else
#define LOG_3P(str) /* nothing */
#endif
Then, replace printf("a c string message")
with LOG_3P("a c string message")
. You'll need to customize the solution, adjust macro parameters or even add several macros for your case. And make a few search and replace until it works.
然后,替换printf("a c string message")
为LOG_3P("a c string message")
。您需要自定义解决方案,调整宏参数,甚至为您的案例添加多个宏。并进行一些搜索和替换,直到它起作用为止。
When you want to see the 3rd party library logs, just define DEBUG_3P
in your build settings as C flags: -D DEBUG_3P
, otherwise it will be mute.
当您想查看 3rd 方库日志时,只需DEBUG_3P
在构建设置中定义为 C flags: -D DEBUG_3P
,否则它将静音。
If you're using a binary library you can just build it with its release configuration, disabling or reducing the logs verbosity to its minimum.
如果您使用的是二进制库,则可以使用其发布配置来构建它,禁用或将日志详细程度降至最低。
回答by david
For Swift, I wrote a wrapper around print() that does just this. See here: https://github.com/SebastianMecklenburg/TagLog
对于 Swift,我为 print() 编写了一个包装器来完成此操作。见这里:https: //github.com/SebastianMecklenburg/TagLog
It works by adding tags to debug messages and then filter the output by those tags. It works all in code and doesn't need an Xcode plugin.
它的工作原理是添加标签来调试消息,然后通过这些标签过滤输出。它全部在代码中工作,不需要 Xcode 插件。