【问题标题】:rsyslog prints same log twicersyslog 两次打印相同的日志
【发布时间】:2016-06-19 08:44:17
【问题描述】:

我正在 Debian 用户空间中运行一个应用程序,并且我正在使用 rsyslog 进行日志记录。 我使用以下命令在主线程中打开登录:

openlog(NULL, LOG_CONS | LOG_NDELAY, LOG_LOCAL0);

在此之后主线程创建 2 个线程(分离):

    pthread_create(&tx_tid, NULL,tx_main, NULL);
if(0 != th_ret_val)
{
    LOG(LEVEL_ERR,"failed to create tx thread, ret_val = %d",th_ret_val);
}
pthread_detach(tx_tid);

pthread_create(&rx_tid, NULL,rx_main, NULL);
if(0 != th_ret_val)
{
    LOG(LEVEL_ERR,"failed to create rx thread, ret_val = %d",th_ret_val);
}
pthread_detach(rx_tid);

并执行 pthread_exit。

在日志中,我看到一些日志被打印了两次(我知道这是同一个日志,因为我添加了一个在 LOG 上递增的令牌号,并且打印了相同的令牌号)我还看到一些日志丢失了!

谁能帮我解释一下为什么会这样?

注意: LOG 宏是:

#define LOG(prio, ...) my_log(__FILE__, __LINE__, __func__, prio, __VA_ARGS__)

而my_log的实现如下:

#define LOG_MAX_LEN 200

static char full_fmt[LOG_MAX_LEN];

void my_log(const char *file, int line, const char *func, int prio, const char *fmt, ...) { va_list 参数;

snprintf(full_fmt, LOG_MAX_LEN, "LOG:tid-%d %-30s:%003d, %-20s - %s", pthread_self(), file, line, func, fmt);
va_start(args, full_fmt);
vsyslog(prio, full_fmt, args);
//vprintf(full_fmt, args);
va_end(args);

}

【问题讨论】:

    标签: embedded-linux rsyslog


    【解决方案1】:

    我找到了问题的根源。我没有使用互斥锁保护 my_log,发送到 syslog 的消息是全局字符串 full_fmt。 添加互斥锁解决了这个问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-03-11
      • 2012-09-06
      • 2021-10-12
      • 2016-12-23
      • 1970-01-01
      相关资源
      最近更新 更多