Skip to content

Commit

Permalink
Log: Allow direct logging to journald if ENABLE_JOURNALD is set
Browse files Browse the repository at this point in the history
This allows for more metadata in the journal over syslog, like automatic
line and source file information being logged
  • Loading branch information
imwints committed Aug 29, 2023
1 parent c2ac974 commit 60ad8c1
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 4 deletions.
13 changes: 11 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -156,8 +156,17 @@ else
LTO := $(THREADS)
endif

#? Look for syslog.h
override ADDFLAGS += $(strip $(shell echo "#include <syslog.h>\nint main() {}" | $(CXX) -o /dev/null -x c++ - >/dev/null 2>&1 && echo "-DHAVE_SYSLOG"))
#? Enable journald direct logging
ifeq ($(ENABLE_JOURNALD),true)
HAVE_JOURNALD := $(shell echo "#include <systemd/sd-journal.h>\nint main() {}" | $(CXX) -lsystemd -o /dev/null -x c++ - >/dev/null 2>&1 && echo true)
endif

ifeq ($(HAVE_JOURNALD),true)
override ADDFLAGS += -DHAVE_JOURNALD -lsystemd
else
#? Look for syslog.h
override ADDFLAGS += $(strip $(shell echo '#include <syslog.h>\nint main() {}' | $(CXX) -o /dev/null -x c++ - >/dev/null 2>&1 && echo '-DHAVE_SYSLOG'))
endif

#? The Directories, Source, Includes, Objects and Binary
SRCDIR := src
Expand Down
11 changes: 9 additions & 2 deletions src/btop_log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,9 @@
#include <mutex>
#include <string>

#if defined(HAVE_SYSLOG)
#if defined(HAVE_JOURNALD)
#include <systemd/sd-journal.h>
#elif defined(HAVE_SYSLOG)
#include <syslog.h>
#endif

Expand Down Expand Up @@ -54,14 +56,19 @@ namespace Logger {
atomic_lock lck(busy, true);
lose_priv neutered{};

#if defined(HAVE_SYSLOG)
#if defined(HAVE_JOURNALD) || defined(HAVE_SYSLOG)
int status = LOG_DEBUG;
switch (level) {
case 1: status = LOG_ERR; break;
case 2: status = LOG_WARNING; break;
case 3: status = LOG_INFO; break;
case 4: status = LOG_DEBUG; break;
}
#endif

#if defined(HAVE_JOURNALD)
sd_journal_print(status, msg.data());
#elif defined(HAVE_SYSLOG)
syslog(status, msg.data());
#else
if (logfile.empty()) {
Expand Down

0 comments on commit 60ad8c1

Please sign in to comment.