我正在与系统日志一起创建一个自定义日志处理程序。我正在尝试使用sd_journal API,但我有几个问题:
下面是我使用的示例代码:
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <systemd/sd-journal.h>
#include <systemd/sd-daemon.h>
int main(int argc, char *argv[]) {
int ret_val = 0;
int count = 0;
sd_journal *jd;
sd_journal_print(LOG_INFO, "Hello World, this is PID %lu!", (unsigned long) getpid());
do {
ret_val = sd_journal_open (&jd, SD_JOURNAL_SYSTEM | SD_JOURNAL_RUNTIME_ONLY | SD_JOURNAL_LOCAL_ONLY);
if (ret_val != 0) {
fprintf(stderr, "Failed to open journal: %s\n", strerror(-ret_val));
break;
}
printf ("Current Journal was loaded successfully!\n");
const void *d;
size_t l;
SD_JOURNAL_FOREACH_DATA (jd, d, l) {
printf("%.*s\n", (int)l, (const char*) d);
count++;
}
sd_journal_close(jd);
printf ("# of Journal entries read: %d\n", count);
} while (0);
return 0;
} 发布于 2016-12-11 00:53:58
我花了一段时间才弄明白,但问题相当简单。问题是在使用
SD_JOURNAL_RUNTIME_ONLY而journald 存储被指定为持久。
对我来说,持久性日志不会进入运行时日志缓冲区,这是不直观的。因此,模拟journalctl -f功能的唯一途径就是打开本地期刊,寻找尾巴。
https://stackoverflow.com/questions/40120363
复制相似问题