From 9aa6cdc494337fa8183222c4ed4ed420c420d9e5 Mon Sep 17 00:00:00 2001 From: gabime Date: Fri, 28 Jun 2019 17:29:52 +0300 Subject: [PATCH] Updated systemd sink and tests --- include/spdlog/sinks/systemd_sink.h | 79 +++++++++++++++-------------- tests/CMakeLists.txt | 17 ++++++- tests/test_systemd.cpp | 14 ++--- 3 files changed, 64 insertions(+), 46 deletions(-) diff --git a/include/spdlog/sinks/systemd_sink.h b/include/spdlog/sinks/systemd_sink.h index ab41864b..15938fa2 100644 --- a/include/spdlog/sinks/systemd_sink.h +++ b/include/spdlog/sinks/systemd_sink.h @@ -12,26 +12,6 @@ namespace spdlog { namespace sinks { -inline int syslog_level(level::level_enum l) -{ - switch (l) - { - case level::off: - case level::trace: - case level::debug: - return LOG_DEBUG; - case level::info: - return LOG_INFO; - case level::warn: - return LOG_WARNING; - case level::err: - return LOG_ERR; - case level::critical: - return LOG_CRIT; - default: - throw std::invalid_argument("systemd_sink.h syslog_level()"); - } -} /** * Sink that write to systemd journal using the `sd_journal_send()` library call. @@ -51,31 +31,52 @@ public: systemd_sink &operator=(const systemd_sink &) = delete; protected: + + std::array syslog_levels_ { + /* level::trace */ LOG_DEBUG, + /* level::debug */ LOG_DEBUG, + /* level::info */ LOG_INFO, + /* level::warn */ LOG_WARNING, + /* level::err */ LOG_ERR, + /* level::critical */ LOG_CRIT, + /* level::off */ LOG_INFO + }; + void sink_it_(const details::log_msg &msg) override { - 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"; + int err; - if( !msg.source.filename || !msg.source.funcname || msg.source.line == 0 ) { - // Do not send source location if not available - key_file = nullptr; + // Do not send source location if not available + if(msg.source.empty()) + { + // Note: function call inside '()' to avoid macro expansion + err = (sd_journal_send)( + "MESSAGE=%.*s", static_cast(msg.payload.size()), msg.payload.data(), + "PRIORITY=%d", syslog_level(msg.level), + nullptr); + } + else + { + err = (sd_journal_send)( + "MESSAGE=%.*s", static_cast(msg.payload.size()), msg.payload.data(), + "PRIORITY=%d", syslog_level(msg.level), + "SOURCE_FILE=%s", msg.source.filename, + "SOURCE_LINE=%d", msg.source.line, + "SOURCE_FUNC=%s", msg.source.funcname, + 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"); + if(err) + { + throw spdlog_ex("Failed writing to systemd", errno); } + + } + + + int syslog_level(level::level_enum l) + { + return syslog_levels_.at(static_cast(l)); } void flush_() override {} diff --git a/tests/CMakeLists.txt b/tests/CMakeLists.txt index ea1b11fa..8047310d 100644 --- a/tests/CMakeLists.txt +++ b/tests/CMakeLists.txt @@ -1,3 +1,8 @@ +find_package(PkgConfig) +if(PkgConfig_FOUND) + pkg_check_modules(systemd libsystemd) +endif() + set(SPDLOG_UTESTS_SOURCES test_errors.cpp test_file_helper.cpp @@ -17,6 +22,10 @@ set(SPDLOG_UTESTS_SOURCES test_stdout_api.cpp test_dup_filter.cpp) +if(systemd_FOUND) + set(SPDLOG_UTESTS_SOURCES ${SPDLOG_UTESTS_SOURCES} test_systemd.cpp) +endif() + file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/logs") enable_testing() @@ -25,11 +34,14 @@ if(SPDLOG_BUILD_TESTS) add_executable(spdlog-utests ${SPDLOG_UTESTS_SOURCES}) spdlog_enable_warnings(spdlog-utests) target_link_libraries(spdlog-utests PRIVATE spdlog) + + if(systemd_FOUND) + target_link_libraries(spdlog-utests PRIVATE ${systemd_LIBRARIES}) + endif() if(SPDLOG_SANITIZE_ADDRESS) spdlog_enable_sanitizer(spdlog-utests) endif() add_test(NAME spdlog-utests COMMAND spdlog-utests) - endif() # The header-only library version tests @@ -37,6 +49,9 @@ if(SPDLOG_BUILD_HO_TESTS) add_executable(spdlog-utests-ho ${SPDLOG_UTESTS_SOURCES}) spdlog_enable_warnings(spdlog-utests-ho) target_link_libraries(spdlog-utests-ho PRIVATE spdlog::spdlog_header_only) + if(systemd_FOUND) + target_link_libraries(spdlog-utests-ho PRIVATE ${systemd_LIBRARIES}) + endif() if(SPDLOG_SANITIZE_ADDRESS) spdlog_set_address_sanitizer(spdlog-utests-ho) endif() diff --git a/tests/test_systemd.cpp b/tests/test_systemd.cpp index 096dc103..8688f41d 100644 --- a/tests/test_systemd.cpp +++ b/tests/test_systemd.cpp @@ -1,13 +1,15 @@ #include "includes.h" -#include +#include "spdlog/sinks/systemd_sink.h" TEST_CASE("systemd", "[all]") { auto systemd_sink = std::make_shared(); - systemd_sink->set_level(spdlog::level::level_enum::err); spdlog::logger logger("spdlog_systemd_test", systemd_sink); - - logger.debug("test debug"); - SPDLOG_LOGGER_ERROR((&logger), "test error"); - logger.info("test info"); + logger.set_level(spdlog::level::trace); + logger.trace("test spdlog trace"); + logger.debug("test spdlog debug"); + SPDLOG_LOGGER_INFO((&logger), "test spdlog info"); + SPDLOG_LOGGER_WARN((&logger), "test spdlog warn"); + SPDLOG_LOGGER_ERROR((&logger), "test spdlog error"); + SPDLOG_LOGGER_CRITICAL((&logger), "test spdlog critical"); }