在OSX10.8中,输出到stdout和stderr的结果不再是Console.app。我希望在不使用NSLog的情况下以Console.app格式获得输出,因为我需要支持使用基本的打印语句来打印调试信息的代码(有关一些背景信息,请参阅https://bitbucket.org/ronaldoussoren/py2app/issue/77 )。
系统日志输出会以某种方式出现在NSLog (苹果系统日志)日志中,因为您可以使用"syslog -C“查看这些日志行。这就是为什么我尝试将以下代码添加到我的应用程序中:
aslclient c = asl_open("py2app", "com.apple.console", ASL_OPT_NO_DELAY);
int fd = dup(2);
asl_set_filter(c, ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG));
asl_add_log_file(c, fd);
asl_log(c, NULL, ASL_LEVEL_INFO, "Hello world from py2app launcher");
asl_log_descriptor(c, NULL, ASL_LEVEL_INFO, 1, ASL_LOG_DESCRIPTOR_WRITE);
asl_log_descriptor(c, NULL, ASL_LEVEL_INFO, 2, ASL_LOG_DESCRIPTOR_WRITE);这在某种程度上是可行的:当我将行写入stdout流时,这些行将被ASL转换:输出现在以通常的日志前缀作为前缀:
Nov 20 13:46:14 Gondolin.local py2app[43722] <Info>: Hello world from py2app launcher但是,日志文件不会在ASL数据存储或Console.app中结束。
有人知道我做错了什么吗?
发布于 2013-05-14 01:02:06
下面的C代码似乎可以做我想做的事情:
#include <asl.h>
#include <unistd.h>
#include <stdio.h>
static void
setup_logging(void)
{
aslmsg msg;
aslclient c = asl_open("py2app", "com.apple.console", 0);
msg = asl_new(ASL_TYPE_MSG);
asl_set(msg, ASL_KEY_FACILITY, "com.apple.console");
asl_set(msg, ASL_KEY_LEVEL, ASL_STRING_NOTICE);
asl_set(msg, ASL_KEY_READ_UID, "-1");
int fd = dup(2);
//asl_set_filter(c, ASL_FILTER_MASK_UPTO(ASL_LEVEL_DEBUG));
asl_add_log_file(c, fd);
asl_log(c, NULL, ASL_LEVEL_INFO, "Hello world from py2app launcher");
asl_log_descriptor(c, msg, ASL_LEVEL_INFO, 1, ASL_LOG_DESCRIPTOR_WRITE);
asl_log_descriptor(c, msg, ASL_LEVEL_INFO, 2, ASL_LOG_DESCRIPTOR_WRITE);
}
int main(void)
{
setup_logging();
printf("hello world, this is a printf\n");
}与我的第一次尝试相比,这包含了一个单一的更改:它使用"aslmsg“参数为asl_log_descriptor显式地设置了ASL Facility、Level和ReadUID。如果没有这些参数,消息将不会以Console.app格式结束。尤其需要ReadUID能够在不具有超级用户权限的情况下读取日志条目。
注意:为了方便测试,您可以使用"syslog -C | tail“来读取控制台日志。如果没有ReadUID,我的程序输出只有在我使用"sudo syslog -C“时才可见。
发布于 2014-09-10 14:35:56
不需要执行asl_add_log_file(c,dup(2))。
此外,您可以只在调用asl_log_descriptor而不是在asl_msg中设置日志级别。请注意,当您在信息级别上看不到syslog中的消息时,原因是默认情况下会过滤掉通知下的消息(请参阅/etc/asl.conf)。
示例:
#include <asl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int main() {
asl_log_descriptor(NULL, NULL, ASL_LEVEL_INFO, STDOUT_FILENO, ASL_LOG_DESCRIPTOR_WRITE);
asl_log_descriptor(NULL, NULL, ASL_LEVEL_NOTICE, STDERR_FILENO, ASL_LOG_DESCRIPTOR_WRITE);
fprintf(stdout, "This is written to stdout which will be at log level info.");
fprintf(stderr, "This is written to stderr which will be at log level notice.");
return 0;
}https://stackoverflow.com/questions/13473864
复制相似问题