且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在应用程序运行时获得控制台读数?

更新时间:2021-12-26 06:18:01

您可以使用 <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;
}