我愿意在这个问题上给予赏金。我已经添加了一个,但它已经过期而没有给予奖励的答案。所以,如果你知道答案,请给我一个提示,我会另外给你一个赏金。
如果我理解正确,则会在NSLog
次调用后使用asl。所以我阅读了手册页并使用了谷歌并最终得到了印象,我可以添加一个额外的文件来收集asl_add_log_file
(man page)的日志输出。与使用谷歌发现的其他问题一样,我的输出文件为空,并且没有显示日志消息。
到目前为止,通常的解决方案似乎是freopen
stderr
到文件。我运行了一些测试,并希望实现一个简单的日志轮换。对新文件执行另一个freopen
时,会丢失一些NSLog
条消息。所以我正在寻找另一种解决方案。
要在Mac OS X上测试,我在Xcode中创建了一个控制台应用程序,并将此代码添加到main:
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSLog(@"docdir = %@", documentsDirectory);
NSString *fileName =[NSString stringWithFormat:@"asl-created-%@.log",[NSDate date]];
NSString *logFilePath = [documentsDirectory stringByAppendingPathComponent:fileName];
NSLog(@"logFilePath = %@", logFilePath);
int asl_fd = open([logFilePath cStringUsingEncoding:NSASCIIStringEncoding], O_CREAT | O_APPEND);
if (asl_fd==-1) {
NSLog(@"could not open output file");
}
asl_object_t asl;
asl = asl_open(NULL, NULL, 0);
asl_add_log_file(asl, asl_fd);
NSLog(@"test output");
/* EDIT: added this */
NSLog(@"YEAH! test output");
asl_log(asl, NULL, ASL_LEVEL_ERR, "asl_log output");
aslmsg asl_msg = asl_new(ASL_TYPE_MSG);
asl_set(asl_msg, ASL_KEY_MSG, "asl test output");
asl_set(asl_msg, ASL_KEY_HOST, "somehost");
asl_send(asl, asl_msg);
asl_free(asl_msg);
/* (EDIT: added this) */
asl_remove_log_file(asl, asl_fd);
close(asl_fd);
编辑:但是,如上所述,日志文件为空。我原以为,“测试输出”出现在那里。
我添加了一些asl日志调用来测试它是否在直接使用asl时有效。通过asl_log
和asl_send
记录的两条消息确实出现在创建的文件中。但是,基于NSLog
的日志消息不会出现。
是否可以使用asl_add_log_file
来调整NSLog
“后端”?