diff --git a/include/spdlog/sinks/systemd_sink.h b/include/spdlog/sinks/systemd_sink.h index ae8ace75..ab41864b 100644 --- a/include/spdlog/sinks/systemd_sink.h +++ b/include/spdlog/sinks/systemd_sink.h @@ -7,8 +7,6 @@ #include "spdlog/details/null_mutex.h" #include "spdlog/details/synchronous_factory.h" -#include -#include #include namespace spdlog { @@ -36,16 +34,16 @@ inline int syslog_level(level::level_enum l) } /** - * Sink that write to systemd using the `sd_journal_print()` library call. + * Sink that write to systemd journal using the `sd_journal_send()` library call. * - * Locking is not needed, as `sd_journal_print()` itself is thread-safe. + * Locking is not needed, as `sd_journal_send()` itself is thread-safe. */ template class systemd_sink : public base_sink { public: // - explicit systemd_sink(void) {} + explicit systemd_sink() {} ~systemd_sink() override {} @@ -55,8 +53,29 @@ public: protected: void sink_it_(const details::log_msg &msg) override { - if (sd_journal_print(syslog_level(msg.level), "%.*s", static_cast(msg.payload.size()), msg.payload.data())) + const char* key_msg = "MESSAGE=%.*s"; + const char* key_prio = "PRIORITY=%d"; + const char* key_file = "CODE_FILE=%s"; + const char* key_line = "CODE_LINE=%d"; + const char* key_func = "CODE_FUNC=%s"; + + if( !msg.source.filename || !msg.source.funcname || msg.source.line == 0 ) { + // Do not send source location if not available + key_file = nullptr; + } + + // Note: function call inside '()' to avoid macro expansion + int err = (sd_journal_send)( + key_msg, static_cast(msg.payload.size()), msg.payload.data(), + key_prio, syslog_level(msg.level), + key_file, msg.source.filename, + key_line, msg.source.line, + key_func, msg.source.funcname, + nullptr); + + if( err ) { throw spdlog_ex("Failed writing to systemd"); + } } void flush_() override {} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index 9bde839c..03c135f0 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -14,7 +14,7 @@ set(SPDLOG_UTESTS_SOURCES test_mpmc_q.cpp test_sink.h test_fmt_helper.cpp - test_stdout_api.cpp) + test_stdout_api.cpp) file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs") enable_testing() diff --git a/tests/test_systemd.cpp b/tests/test_systemd.cpp index 0909082c..096dc103 100644 --- a/tests/test_systemd.cpp +++ b/tests/test_systemd.cpp @@ -8,6 +8,6 @@ TEST_CASE("systemd", "[all]") spdlog::logger logger("spdlog_systemd_test", systemd_sink); logger.debug("test debug"); - logger.error("test error"); + SPDLOG_LOGGER_ERROR((&logger), "test error"); logger.info("test info"); }