【发布时间】:2016-01-01 09:10:09
【问题描述】:
我的目的是读取与我的应用程序相关的所有系统日志消息(所有这些日志消息都存在于 system.log 文件中),并将其存储在应用程序 Document 文件夹中的日志文件中。 我浏览了this code 并使用了this answer。
这是我的代码:
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];
这只是我的 IOS 应用程序代码的一部分。我在 IOS 模拟器上运行我的应用程序。
日志文件已成功创建。它有一些主要是 JSON 的内容。
但是,该文件没有我通过 NSLog 输出的那些日志消息,但这些日志消息存在于系统的日志文件中。
我对 IOS 开发很陌生。如前所述,我访问系统日志文件的目的。所以我想捕获每条日志消息并将其保存到我的个人目录中。
我在这个 Stackoverflow 上搜索了很多。但是,我还没有找到可以帮助我的确切线程。任何人都可以适当地指导我吗? (我希望这个问题不要重复。)
【问题讨论】:
-
为什么不使用自定义日志功能来保存日志?
-
@SolaWing 你的意思是,我应该为此编写自己的函数吗?嗯,不错的主意。但我仍然想知道,我怎样才能访问 system.log 文件?
标签: ios objective-c logging nslog