如何在应用程序运行时获得控制台读数? [英] How can I get a console readout at runtime in an application?

查看:29
本文介绍了如何在应用程序运行时获得控制台读数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

出于调试目的,我想在运行时以类似于 App Store 上当前控制台应用程序的方式访问控制台打印输出(可以找到 这里).

For debugging purposes, I'd like to access console printouts at runtime in a way similar to the Console app current on the App Store (that can be found here).

我对文档进行了一些搜索,但找不到 Apple 提供的任何内容,但我觉得我错过了一些重要的东西.有什么见解吗?

I did some searching of the docs and I can't find anything that's provided by Apple, but I feel like I'm missing something important. Any insight?

谢谢.

推荐答案

您可以使用 <asl.h>.下面是我拼凑在一起创建控制台消息数组的示例.

You can do so using <asl.h>. Here is an example that I threw together to create an array of console messages.

-(NSArray*)console
{
    NSMutableArray *consoleLog = [NSMutableArray array];

    aslclient client = asl_open(NULL, NULL, ASL_OPT_STDERR);

    aslmsg query = asl_new(ASL_TYPE_QUERY);
    asl_set_query(query, ASL_KEY_MSG, NULL, ASL_QUERY_OP_NOT_EQUAL);
    aslresponse response = asl_search(client, query);

    asl_free(query);

    aslmsg message;
    while((message = asl_next(response)) != NULL)
    {
        const char *msg = asl_get(message, ASL_KEY_MSG);
        [consoleLog addObject:[NSString stringWithCString:msg encoding:NSUTF8StringEncoding]];
    }
    if (message != NULL) {
        asl_free(message);
    }
    asl_free(response);
    asl_close(client);

    return consoleLog;
}

这篇关于如何在应用程序运行时获得控制台读数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆