Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

Thanks.

like image 826
Hyperbole Avatar asked Aug 22 '11 16:08

Hyperbole


People also ask

How can I see the output of a console application in Visual Studio?

Press F11 . Visual Studio calls the Console. WriteLine(String, Object, Object) method. The console window displays the formatted string.


1 Answers

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;
}
like image 60
Joe Avatar answered Sep 28 '22 08:09

Joe