有没有一种方法来重定向系统日志消息到标准输出?

编程入门 行业动态 更新时间:2024-10-24 05:18:41
本文介绍了有没有一种方法来重定向系统日志消息到标准输出?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我有一个可以在两种模式下运行的应用程序,或者使用CLI,或者作为一个守护进程。

I have an application that can run in two modes, either with a CLI, or as a daemon.

我使用的syslog()的记录。但是,在命令行模式下运行,我想所有的日志,除了那些标记 LOG_DEBUG ,消息被发送到控制台,而不是记录。

I am using syslog() for logging. However, when run in CLI mode I'd like all the logging, except those marked LOG_DEBUG, messages to be sent to the console instead of logged.

我曾尝试使用 setlogmask(),但这似乎并没有重定向到控制台。

I have tried to use setlogmask(), but that doesn't seem to redirect to console.

什么是做到这一点的最好方法是什么?

What is the best way to do this?

推荐答案

作为由maverik的意见建议,我写了周围的syslog的包装,确定是否将输出发送到日志或控制台。这万一有谁需要这个。

As suggested in the comments by maverik, I wrote a wrapper around syslog that determines whether to send the output to the log or console. Here it is in case anyone ever needs this.

void mylog (int level, const char *format, ...) { va_list args; va_start (args, format); if (remote) { vsyslog(level, format, args); } else { if (level == LOG_DEBUG) vsyslog(level, format, args); else vprintf(format, args); } va_end(args); }

更多推荐

有没有一种方法来重定向系统日志消息到标准输出?

本文发布于:2023-11-25 14:10:55,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1630073.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:方法来   重定向   消息   标准   系统

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!