Skip to content

Commit

Permalink
Use systemd journal for logging when in use
Browse files Browse the repository at this point in the history
When we detect that stderr is going to the systemd journal, we use the
function sd_journal_print() instead of fprintf(stderr, ...) for log
output.  Primarily, this avoids printing duplicate timestamp and pid,
thus making the log a bit cleaner.  Secondarily, this adds metadata
such as the severity to the logs, so that if the journal gets sent on
to syslog, the messages have useful metadata attached.
  • Loading branch information
petere committed Jul 15, 2020
1 parent 31c2fb1 commit a439248
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 7 deletions.
14 changes: 9 additions & 5 deletions .travis.yml
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
dist: bionic
language: c
addons:
apt:
update: true
packages:
- libevent-dev
jobs:
include:
- env: configure_args=''
- env: configure_args='--enable-cassert'
- env: configure_args='--enable-cassert --with-uregex'
- env: configure_args='CPPFLAGS=-DUSE_SYSTEMD LIBS=-lsystemd'
- compiler: clang
- dist: trusty
- dist: xenial
- dist: focal
- arch: arm64
- arch: ppc64le
before_install: |
set -e
sudo apt-get update
install: |
set -e
pkgs="libevent-dev"
case $configure_args in *USE_SYSTEMD*) pkgs="$pkgs libsystemd-dev";; esac
sudo apt-get -y install $pkgs
script: |
set -e
./autogen.sh
Expand Down
35 changes: 33 additions & 2 deletions usual/logging.c
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@

#include <usual/logging.h>

#include <sys/stat.h>

#include <usual/ctype.h>
#include <usual/string.h>
#include <usual/time.h>
Expand All @@ -27,6 +29,11 @@
#include <syslog.h>
#endif

#ifdef USE_SYSTEMD
#define SD_JOURNAL_SUPPRESS_LOCATION
#include <systemd/sd-journal.h>
#endif

#ifdef WIN32
#define LOG_EMERG 0
#define LOG_ALERT 1
Expand Down Expand Up @@ -196,8 +203,32 @@ void log_generic(enum LogLevel level, void *ctx, const char *fmt, ...)
}
}

if (!cf_quiet && level <= cf_stderr_level)
fprintf(stderr, "%s [%u] %s %s\n", timebuf, pid, lev->tag, msg);
if (!cf_quiet && level <= cf_stderr_level) {
#ifdef USE_SYSTEMD
static bool journal_stream_checked = false;
static bool use_systemd_journal = false;

if (!journal_stream_checked) {
if (getenv("JOURNAL_STREAM")) {
long long unsigned int f1, f2;
if (sscanf(getenv("JOURNAL_STREAM"), "%llu:%llu", &f1, &f2) == 2) {
struct stat st;
dev_t js_dev = f1;
ino_t js_ino = f2;
if (fstat(fileno(stderr), &st) >= 0)
if (js_dev == st.st_dev && js_ino == st.st_ino)
use_systemd_journal = true;
}

}
journal_stream_checked = true;
}
if (use_systemd_journal)
sd_journal_print(lev->syslog_prio, "%s", msg);
else
#endif
fprintf(stderr, "%s [%u] %s %s\n", timebuf, pid, lev->tag, msg);
}

if (log_file && level <= cf_logfile_level)
fprintf(log_file, "%s [%u] %s %s\n", timebuf, pid, lev->tag, msg);
Expand Down

0 comments on commit a439248

Please sign in to comment.