diff --git a/include/spdlog/sinks/systemd_sink.h b/include/spdlog/sinks/systemd_sink.h index e3d513a0..9e908b6f 100644 --- a/include/spdlog/sinks/systemd_sink.h +++ b/include/spdlog/sinks/systemd_sink.h @@ -18,16 +18,14 @@ namespace sinks { /** * Sink that write to systemd journal using the `sd_journal_send()` library call. - * - * Locking is not needed, as `sd_journal_send()` itself is thread-safe. */ template class systemd_sink : public base_sink { public: - // - systemd_sink() - : syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG, + explicit systemd_sink(bool enable_formatting) + : enable_formatting_{enable_formatting} + , syslog_levels_{{/* spdlog::level::trace */ LOG_DEBUG, /* spdlog::level::debug */ LOG_DEBUG, /* spdlog::level::info */ LOG_INFO, /* spdlog::level::warn */ LOG_WARNING, @@ -42,14 +40,26 @@ public: systemd_sink &operator=(const systemd_sink &) = delete; protected: + bool enable_formatting_ = false; using levels_array = std::array; levels_array syslog_levels_; void sink_it_(const details::log_msg &msg) override { int err; + string_view_t payload; + memory_buf_t formatted; + if (enable_formatting_) + { + base_sink::formatter_->format(msg, formatted); + payload = string_view_t(formatted.data(), formatted.size()); + } + else + { + payload = msg.payload; + } - size_t length = msg.payload.size(); + size_t length = payload.size(); // limit to max int if (length > static_cast(std::numeric_limits::max())) { @@ -60,12 +70,12 @@ protected: if (msg.source.empty()) { // Note: function call inside '()' to avoid macro expansion - err = (sd_journal_send)("MESSAGE=%.*s", static_cast(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), + err = (sd_journal_send)("MESSAGE=%.*s", static_cast(length), payload.data(), "PRIORITY=%d", syslog_level(msg.level), "SYSLOG_IDENTIFIER=%.*s", static_cast(msg.logger_name.size()), msg.logger_name.data(), nullptr); } else { - err = (sd_journal_send)("MESSAGE=%.*s", static_cast(length), msg.payload.data(), "PRIORITY=%d", syslog_level(msg.level), + err = (sd_journal_send)("MESSAGE=%.*s", static_cast(length), payload.data(), "PRIORITY=%d", syslog_level(msg.level), "SYSLOG_IDENTIFIER=%.*s", static_cast(msg.logger_name.size()), msg.logger_name.data(), "CODE_FILE=%s", msg.source.filename, "CODE_LINE=%d", msg.source.line, "CODE_FUNC=%s", msg.source.funcname, nullptr); } @@ -90,14 +100,14 @@ using systemd_sink_st = systemd_sink; // Create and register a syslog logger template -inline std::shared_ptr systemd_logger_mt(const std::string &logger_name) +inline std::shared_ptr systemd_logger_mt(const std::string &logger_name, bool enable_formatting = false) { - return Factory::template create(logger_name); + return Factory::template create(logger_name, enable_formatting); } template -inline std::shared_ptr systemd_logger_st(const std::string &logger_name) +inline std::shared_ptr systemd_logger_st(const std::string &logger_name, bool enable_formatting = false) { - return Factory::template create(logger_name); + return Factory::template create(logger_name, enable_formatting); } } // namespace spdlog