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

时间:2022-10-01 20:47:55

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).

出于调试目的,我想在运行时访问控制台打印输出,其方式类似于App Store上的控制台应用程序(可在此处找到)。

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?

我做了一些搜索文档,我找不到Apple提供的任何东西,但我觉得我错过了一些重要的东西。任何见解?

Thanks.

谢谢。

2 个解决方案

#1


17  

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;
}

#2


0  

If your device is attached to Xcode, you can see console output (NSLogs and such) in the debug area:

如果您的设备连接到Xcode,您可以在调试区域中看到控制台输出(NSLog等):

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

If you're running the app and connecting to Xcode later, I believe you can get console logs in the Organizer.

如果您正在运行应用程序并稍后连接到Xcode,我相信您可以在管理器中获取控制台日志。


Edit: to access the log file at runtime, you should try /var/log/system.log — but even better I recommend using a custom debug function, which would write to the system log and/or a text view in your app. (Check out NSLogv, which will be useful when writing a wrapper function.) This also has the advantage of letting you disable all debug logs from one place (just change your debug function).

编辑:要在运行时访问日志文件,您应该尝试/var/log/system.log - 但更好的是我建议使用自定义调试功能,该功能将写入系统日志和/或应用程序中的文本视图。 (查看NSLogv,这在编写包装函数时很有用。)这样做的好处是可以从一个地方禁用所有调试日志(只需更改调试功能)。

#1


17  

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;
}

#2


0  

If your device is attached to Xcode, you can see console output (NSLogs and such) in the debug area:

如果您的设备连接到Xcode,您可以在调试区域中看到控制台输出(NSLog等):

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

If you're running the app and connecting to Xcode later, I believe you can get console logs in the Organizer.

如果您正在运行应用程序并稍后连接到Xcode,我相信您可以在管理器中获取控制台日志。


Edit: to access the log file at runtime, you should try /var/log/system.log — but even better I recommend using a custom debug function, which would write to the system log and/or a text view in your app. (Check out NSLogv, which will be useful when writing a wrapper function.) This also has the advantage of letting you disable all debug logs from one place (just change your debug function).

编辑:要在运行时访问日志文件,您应该尝试/var/log/system.log - 但更好的是我建议使用自定义调试功能,该功能将写入系统日志和/或应用程序中的文本视图。 (查看NSLogv,这在编写包装函数时很有用。)这样做的好处是可以从一个地方禁用所有调试日志(只需更改调试功能)。