As we know , we can use ASL (Apple System Logger) API in objective C to read logs and also with using asl_search it is possible to retrieve specific application logs.
But the problem is output of asl does not include the time when the log was created.
For example , when you open system.log in directory /var/log with Apple System Logger ,you see logs like this :
Nov 28 09:19:37 localhost bootlog[0]: BOOT_TIME 1354123177 0
But when quering asl with objective C , it reports every attribute of log except the time when the log is created, it means in example mentioned above , asl_search does not report Nov 28 09:19:37
Is there anyway to include time of creating logs with quering asl in objective C?
If this is not possible , what is another approach to retrieving time of logs?
This is my code , note that the time of logs which we can see in system.log files in /var/log does not appear in output of my code .
int main (int argc, const char * argv[])
{
NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
aslmsg q, m;
int i;
const char *key, *val;
q = asl_new(ASL_TYPE_QUERY);
asl_set_query(q, ASL_KEY_SENDER, "bootlog", ASL_QUERY_OP_EQUAL);
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);
[pool drain];
return 0;
}
The timestamp of the log message is the value of the
ASL_KEY_TIMEkey in the message. The value is a UNIX time (seconds since 1.1.1970).You can convert the timestamp of the log message to
NSDatewith