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

后端 未结 2 1028
栀梦
栀梦 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:27

    You can do so using . 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;
    }
    

提交回复
热议问题