Skip to content

Instantly share code, notes, and snippets.

@enzo-yang
Created April 7, 2013 09:55
Show Gist options
  • Select an option

  • Save enzo-yang/5329839 to your computer and use it in GitHub Desktop.

Select an option

Save enzo-yang/5329839 to your computer and use it in GitHub Desktop.
Get Log Outputs In Objective C Program Programmatically
#import <asl.h>
- (void)updateLogs {
NSDateFormatter *formater = [[NSDateFormatter alloc] init];
[formater setDateFormat:@"MM-dd HH:mm:ss"];
aslmsg query = NULL, message = NULL;
aslresponse response = NULL;
query = asl_new(ASL_TYPE_QUERY);
const char *time = [[NSString stringWithFormat:@"%d", _lastTime] UTF8String];
asl_set_query(query, ASL_KEY_TIME, time, ASL_QUERY_OP_GREATER | ASL_QUERY_OP_NUMERIC);
asl_set_query(query, ASL_KEY_FACILITY, [[[NSBundle mainBundle] bundleIdentifier] UTF8String], ASL_QUERY_OP_EQUAL);
response = asl_search(NULL, query);
while (NULL != (message = aslresponse_next(response))) {
const char *content = asl_get(message, ASL_KEY_MSG);
const char *time = asl_get(message, ASL_KEY_TIME);
_lastTime = atoi(time);
LogRecord *record = [[LogRecord new] autorelease];
record.content = [[[NSString alloc] initWithUTF8String:content] autorelease];
record.time = [formater stringFromDate:[NSDate dateWithTimeIntervalSince1970:_lastTime]];
[((NSMutableArray *)self.logs) addObject:record];
}
aslresponse_free(response);
asl_free(query);
[formater release];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment