From 95a58e9fea0592527297ebd7a694656e0a3d734f Mon Sep 17 00:00:00 2001 From: Matt Aber Date: Fri, 24 May 2024 13:20:46 -0400 Subject: [PATCH] Add support for systemd sinks Change-Id: I2cd3d0c480af9456d551f3f7642aa93764d62a06 --- src/KDSpdSetup/details.cpp | 12 ++++++++++++ src/KDSpdSetup/details.h | 25 +++++++++++++++++++++++++ src/KDSpdSetup/setup.cpp | 8 ++++++++ 3 files changed, 45 insertions(+) diff --git a/src/KDSpdSetup/details.cpp b/src/KDSpdSetup/details.cpp index 64a4b58..f4cec2d 100644 --- a/src/KDSpdSetup/details.cpp +++ b/src/KDSpdSetup/details.cpp @@ -216,4 +216,16 @@ auto genFromWinStr(toml::string &&typeStr) -> spdlog::sink_ptr } #endif +#ifdef _KDSPDSETUP_SYSTEMD_ +auto genFromSystemdStr(toml::string &&typeStr, std::string &&ident, bool const enableFormatting) -> spdlog::sink_ptr +{ + if (typeStr == "systemd_sink_st") { + return createSystemdSinkStPtr(std::move(ident), enableFormatting); + } + if (typeStr == "systemd_sink_mt") { + return createSystemdSinkMtPtr(std::move(ident), enableFormatting); + } +} +#endif + } // namespace KDSPDSetup::details diff --git a/src/KDSpdSetup/details.h b/src/KDSpdSetup/details.h index eb7b800..7329ad2 100644 --- a/src/KDSpdSetup/details.h +++ b/src/KDSpdSetup/details.h @@ -28,6 +28,10 @@ #elif _WIN32 #include #endif +#if __has_include() +#include +#define _KDSPDSETUP_SYSTEMD_ +#endif #include @@ -93,6 +97,8 @@ static auto const linuxStrs{ std::vector{ "syslog_sink_st", "syslog */ static auto const winStrs{ std::vector{ "msvc_sink_st", "msvc_sink_mt" } }; +static auto const systemdStrs{ std::vector{ "systemd_sink_st", "systemd_sink_mt" } }; + /** * @brief A simple map associating strings of `spdlog::level::level_enum` names to the enums themselves. * Used to pass an enum to `spdlog::logger::set_level` given a string read from a TOML table. @@ -312,6 +318,12 @@ class SPDMaps #define createMsvcSinkMtPtr createMsvcSinkPtr #endif +#ifdef _KDSPDSETUP_SYSTEMD_ +#define createSystemdSinkStPtr createSystemdSinkPtr + +#define createSystemdSinkMtPtr createSystemdSinkPtr +#endif + /** * @brief Returns true if a string `typeStr` is present in a vector `strList`, and false if not. * Used to identify a group to which a sink's `type` belongs when reading from a configuration file. @@ -574,6 +586,14 @@ auto createMsvcSinkPtr() -> std::shared_ptr> } #endif +#ifdef _KDSPDSETUP_SYSTEMD_ +template +auto createSystemdSinkPtr(std::string &&ident, bool const enableFormatting) -> std::shared_ptr> +{ + return std::make_shared>(ident, enableFormatting); +} +#endif + /** * @brief Return the result of calling KDSPDSetup::details::createFileSinkPtr with the correct template argument * based on the value of `typeStr`. Uses macros `createFileSinkStPtr` and `createFileSinkMtPtr` for clarity. @@ -651,4 +671,9 @@ auto genFromWinStr(toml::string &&typeStr) -> spdlog::sink_ptr; #endif +#ifdef _KDSPDSETUP_SYSTEMD_ +auto genFromSystemdStr(toml::string &&typeStr, std::string &&ident, bool const enableFormatting) -> spdlog::sink_ptr; + +#endif + } // namespace KDSPDSetup::details diff --git a/src/KDSpdSetup/setup.cpp b/src/KDSpdSetup/setup.cpp index 605c2b2..4f8560c 100644 --- a/src/KDSpdSetup/setup.cpp +++ b/src/KDSpdSetup/setup.cpp @@ -83,6 +83,14 @@ void setupSink(toml::table &&sinkTable) sinkPtr = details::genFromWinStr(std::move(typeStr)); #else return; +#endif + } else if (details::inTypelist(typeStr, details::systemdStrs)) { +#ifdef _KDSPDSETUP_SYSTEMD_ + auto ident = (sinkTable.contains("ident")) ? sinkTable.at("ident").as_string().str : ""s; + auto const enableFormatting = (sinkTable.contains("enable_formatting")) ? sinkTable.at("enable_formatting").as_boolean() : false; + sinkPtr = details::genFromSystemdStr(std::move(typeStr), std::move(ident), enableFormatting); +#else + return; #endif }