Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Objective C to read log messages posted to the device console

How can my iOS application read messages from the devices console log. I want to programmatically read through these log entries (like reading a file?), select some, and email them to support.

I'm aware of one iPhone application which lets you view the log. It's name is Console. However, I can't figure out what classes or APIs he used. One person suggested it was done using ASL functions, but I don't know what these are or where they are documented.

I'm also aware of several alternatives to using NSLog such as NSLogger and CocoaLumberJack, but we aren't ready to implement these at this time.

Thanks very much for any help!

like image 737
Richard Brightwell Avatar asked May 26 '11 20:05

Richard Brightwell


People also ask

How do I view logs in Xcode?

In the Xcode menu hit Run - Console. This is where NSLog / print / printf etc statements output. The key command is Command + Shift + R.

What is Objective C in iOS?

Objective-C is the primary programming language you use when writing software for OS X and iOS. It's a superset of the C programming language and provides object-oriented capabilities and a dynamic runtime.

What is the difference between NSLog and printf?

NSLog is like a printf, but it does a bit more: A timestamp is added to the output. The output is sent to the Xcode console, or whatever stderr is defined as. It accepts all the printf specifiers, but it also accepts the @ operator for objects which displays the string provided by the object's description method.

How do I view iPhone logs?

Locate Crash Reports and Memory Logs on the DeviceOpen the Analytics & Improvements section of Settings on the device. See Share analytics, diagnostics, and usage information with Apple. Tap Analytics Data. Locate the log for your app.


1 Answers

This entry in the Cocoanetics blogs has sample code to access the system log on iOS using the ASL (Apple System Logger) API (man page):

aslmsg q, m; int i; const char *key, *val;  q = asl_new(ASL_TYPE_QUERY);  aslresponse r = asl_search(NULL, q); while (NULL != (m = aslresponse_next(r))) {     NSMutableDictionary *tmpDict = [NSMutableDictionary dictionary];      for (i = 0; (NULL != (key = asl_key(m, i))); i++)     {         NSString *keyString = [NSString stringWithUTF8String:(char *)key];          val = asl_get(m, key);          NSString *string = [NSString stringWithUTF8String:val];         [tmpDict setObject:string forKey:keyString];     }      NSLog(@"%@", tmpDict); } aslresponse_free(r); 

Note that you need to poll ASL to read the latest messages. The code above will also fail when ran on the iPhone simulator, but works just fine on an actual device.

If you don't want to fight the C ASL API, have a look at this Objective-C wrapper called ASLogger.

like image 70
Julio Gorgé Avatar answered Oct 03 '22 00:10

Julio Gorgé