My intention is to read all the system log messages pertaining to my application(all those log messages present inside the system.log file), and store it in a log file within the app's Document folder. I went through this code and used this answer.
Here is my code:
NSDate *currentDate = [[NSDate alloc] init];
NSDateFormatter *formatter = [[NSDateFormatter alloc] init];
[formatter setDateFormat:[NSString stringWithFormat: @"dd-MM-yyyy-HHmmss"]];
NSString *logFileName = [[formatter stringFromDate:currentDate] stringByAppendingString:@".log"];
NSError *error;
NSMutableString *content = [[NSMutableString alloc] init];
aslmsg q, m;
int i;
const char *key, *val;
q = asl_new(ASL_TYPE_QUERY);
aslresponse r = asl_search(NULL, q);
while (NULL != (m = asl_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];
}
[content appendString:[NSString stringWithFormat:@"%@",tmpDict]];
}
asl_release(r);
NSString *filePath = [[NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES) firstObject] stringByAppendingPathComponent:logFileName];
[[NSString stringWithString:content] writeToFile:filePath atomically:YES encoding:NSUTF8StringEncoding error:&error];
This is just a part of code of my IOS app. I am running my app on the IOS Simulator.
The log files gets successfully created. It has some content which is predominantly JSON.
However, the file does not have those log messages which I outputting through NSLog, yet those log messages are present inside the System's log file.
I am quite new to IOS Development. As stated, my purpose to access the System's log files. So I want to capture each log message and save it into my personal directory.
I searched a lot on this Stackoverflow. However, I haven't found the exact thread which could help me on this. Can anyone guide me appropriately? (I hope this question isn't duplicated.)