iphone - 关闭特定对象的控制台日志记录

标签 iphone objective-c xcode logging mpmovieplayercontroller

有点烦人: 自从我开始使用 MPMoviePlayerController 以来,控制台充满了来自 MPAVController 的信息。 例如:

[MPAVController] Autoplay: _streamLikelyToKeepUp: 1 -> 1
[MPAVController] Autoplay: Disabling autoplay

这有点烦人,因为我总是不得不搜索自己记录的信息。 有没有办法关闭特定对象或框架的日志记录?

最佳答案

我不认为这种过滤是开箱即用的。但是可以将 stderr(由 NSLog 使用)重定向到管道,在后台线程中从该管道读取,然后将通过过滤器的消息打印到 stdout(也被调试器捕获)。这段代码完成了这项工作:

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 输出始终以日期和时间开头。

此代码的第二个问题是它不会尝试拆分/合并从管道读取的字符串。不能保证每次读取都会有一个 NSLog。他们可能会聚在一起,或者时间太长而会分开。要处理此问题,需要对从管道读取的数据进行额外处理。

无论如何,对于许多实际用途来说,这应该足够了。

关于iphone - 关闭特定对象的控制台日志记录,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12804425/

相关文章:

iphone - 逻辑问题,根据数据库条目向 UI 添加按钮

iphone - 如何根据ios sdk中放置的uiimage的坐标获取 View 的坐标?

ios - 在 tableView 中检索选定的行

ios - 使 requestAVAssetForVideo 同步

iphone - 如何找出 Xcode 调试器(适用于 iPhone)中引发的异常?

ios - iOS8 中状态栏的黑色

iOS 后台应用程序 : best way to run app

ios - 添加带有披露指示器的 UIVewiTabelCell 链接

objective-c - 在 Swift 中查找而不是过滤数组

iphone - iPhone 应用程序升级到 xcode 3.2.3 后出现链接器错误