问题描述
这有点令人烦恼:
自从我开始使用MPMoviePlayerController以来,控制台中的MPAVController信息过满了。
例如:
It's some kind of annoying:Since I started using the MPMoviePlayerController the console is overfilled with information from MPAVController.Eg:
[MPAVController] Autoplay: _streamLikelyToKeepUp: 1 -> 1
[MPAVController] Autoplay: Disabling autoplay
这是一种烦人的因为我总是必须搜索我自己的记录信息。
有没有办法关闭特定对象或框架的日志记录?
This is some kind of annoying because I always have to search for my own logged information.Is there a way to turn off logging for specific objects or frameworks?
推荐答案
我不认为这样的过滤可以开箱即用。但是可以将 stderr
(由 NSLog
使用)重定向到管道中,在后台从该管道读取线程然后将通过过滤器的消息打印到 stdout
(也由调试器捕获)。这段代码完成了这项工作:
I don't think such filtering is possible out of the box. But it's possible to redirect stderr
(which is used by NSLog
) into a pipe, read from that pipe in a background thread and then print messages that pass through the filter onto stdout
(which is captured by the debugger as well). This code does the job:
int main(int argc, char *argv[])
{
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_LOW, 0), ^(void) {
size_t const BUFFER_SIZE = 2048;
// Create a pipe
int pipe_in_out[2];
if (pipe(pipe_in_out) == -1)
return;
// Connect the 'in' end of the pipe to the stderr
if (dup2(pipe_in_out[1], STDERR_FILENO) == -1)
return;
char *buffer = malloc(BUFFER_SIZE);
if (buffer == 0)
return;
for (;;)
{
// Read from the 'out' end of the pipe
ssize_t bytes_read = read(pipe_in_out[0], buffer, BUFFER_SIZE);
if (bytes_read <= 0)
break;
// Filter and print to stdout
if (should_show(buffer)) // TODO: Apply filters here
fwrite(buffer, 1, bytes_read, stdout);
}
free(buffer);
close(pipe_in_out[1]);
});
// Rest of main
}
请注意这段代码非常简单,无法处理所有极端情况。首先,它捕获所有 stderr
输出,而不仅仅是 NSLog
。也许这可以通过检查内容来过滤掉。 NSLog
输出始终以日期和时间开始。
Please note that this code is quite simple and doesn't handle all corner cases. First of all it captures all stderr
output and not just NSLog
. Maybe this could be filtered out by checking against the content. NSLog
output always starts with the date and time.
此代码的第二个问题是它没有尝试分割/连接从管道中读取的字符串。无法保证每次读取都会有一个 NSLog
。他们可能会聚在一起或者太长并且会分裂。要处理这个问题,需要额外处理从管道中读取的数据。
Second problem with this code is that it doesn't try to split/join strings it reads from the pipe. There's no guarantee that there will be one NSLog
per read. They could be coming together or be too long and would be split. To handle this it would require additional processing of the data read from the pipe.
无论如何,出于许多实际目的,这应该足够了。
Anyway, for many practical purposes this should be enough.
这篇关于关闭特定对象的控制台日志记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!