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

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

问题描述

为了调试的目的,我想在运行时访问控制台打印输出,类似于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).

我对文档进行了一些搜索,由苹果提供,但我觉得我缺少一些重要的东西。任何洞察?

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天全站免登陆