How can I get a console readout at runtime in an application?

后端 未结 2 1015
栀梦
栀梦 2020-12-08 08:39

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

相关标签:
2条回答
  • 2020-12-08 09:22

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

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


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

    0 讨论(0)
  • 2020-12-08 09:27

    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;
    }
    
    0 讨论(0)
提交回复
热议问题