C 中的日志错误
我正在 Linux 环境下开发一个 C 项目,我正在寻找一种向日志文件添加错误的有效方法。 我尝试使用 Syslog 进行以下初始化:
setlogmask(LOG_UPTO(7));
openlog(name, LOG_CONS | LOG_NDELAY | LOG_PID, LOG_USER);
但似乎它运行得太慢。我需要它工作得非常快.. 有人可以帮忙吗?也许系统日志不是正确的方法。
I am working on a project in C under a Linux environment and I'm looking for an efficient way to add errors to a log file.
I tried to use Syslog with the following initialization:
setlogmask(LOG_UPTO(7));
openlog(name, LOG_CONS | LOG_NDELAY | LOG_PID, LOG_USER);
But it seems that it works too slow. I need it to work very fast..
Can someone help with that? Maybe the syslog is not the right approach.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
一种可能会失去使用 syslog 的一些灵活性,即让程序使用普通 I/O 设施(可能要小心使用刷新)自行写入错误日志。
One possibility, which loses some of the flexibility of using syslog, is to have your program write its error log itself, using the normal I/O facilities (probably with careful use of flushing).
您可以编写一个自定义的轻量级记录器,也可以使用第 3 方开源记录器...
例如,第 3 部分 C++ 记录器 [ http://logging.apache.org/log4cxx/]
这里是简单的 [buggy] 自定义记录器 [ 摘自《C++ Time saving Techniques For Dummies》一书]
用法:
警告:这是一个“玩具”记录器,有一些错误,仅供您了解一些自定义记录器...不要直接在实际应用程序中使用..
You can write a custom light weight logger or may 3rd party open source one...
For example 3rd part C++ logger [ http://logging.apache.org/log4cxx/]
And Here is simple [buggy] custom logger [ From book C++ Timesaving Techniques For Dummies ]
Usage :
Warning: This is "toy" logger with some bugs to just give you some idea about custom logger...Do not use directly in real applications..
也许您的系统日志守护进程在每次写入后都会进行同步。另一种方法可能是按照其他人的建议直接记录到文件中。
Probably your syslog daemon is doing a sync after each write. An alternative might be to log directly to a file as suggested by others.
使用
printk(KERNEL "...")
并使用dmesg
获取日志怎么样?What about using
printk(KERNEL "...")
, and get your logs withdmesg
?