From e452bc061036c98e15675c40d4c8b5fde66c447a 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 | 24 ++++++++++++++++++++++++ src/KDSpdSetup/details.h | 28 ++++++++++++++++++++++++++++ src/KDSpdSetup/setup.cpp | 7 +++++++ 3 files changed, 59 insertions(+) diff --git a/src/KDSpdSetup/details.cpp b/src/KDSpdSetup/details.cpp index 64a4b58..9060b0c 100644 --- a/src/KDSpdSetup/details.cpp +++ b/src/KDSpdSetup/details.cpp @@ -119,6 +119,16 @@ auto createSyslogSinkTuple(toml::table const &sinkTable) } #endif +#ifdef _KDSPDSETUP_SYSTEMD_ +auto createSystemdSinkTuple(toml::table &&sinkTable) -> std::tuple +{ + auto ident = (sinkTable.contains("ident")) ? sinkTable.at("ident").as_string() : ""; + auto const enableFormatting = (sinkTable.contains("enable_formatting")) ? sinkTable.at("enable_formatting").as_boolean() : false; + + return std::make_tuple(std::move(ident), enableFormatting); +} +#endif + auto genFromFileStr(toml::string &&typeStr, toml::table &&sinkTable, bool const &truncate) -> spdlog::sink_ptr { if (typeStr == "basic_file_sink_st") { @@ -216,4 +226,18 @@ auto genFromWinStr(toml::string &&typeStr) -> spdlog::sink_ptr } #endif +#ifdef _KDSPDSETUP_SYSTEMD_ +auto genFromSystemdStr(toml::string &&typeStr, toml::table &&sinkTable) -> spdlog::sink_ptr +{ + if (typeStr == "systemd_sink_st") { + return createSystemdSinkStPtr(std::move(sinkTable)); + } + if (typeStr == "systemd_sink_mt") { + return createSystemdSinkMtPtr(std::move(sinkTable)); + } + + return nullptr; +} +#endif + } // namespace KDSPDSetup::details diff --git a/src/KDSpdSetup/details.h b/src/KDSpdSetup/details.h index eb7b800..10735bf 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,17 @@ auto createMsvcSinkPtr() -> std::shared_ptr> } #endif +#ifdef _KDSPDSETUP_SYSTEMD_ +auto createSystemdSinkTuple(toml::table &&sinkTable) -> std::tuple; + +template +auto createSystemdSinkPtr(toml::table &&sinkTable) -> std::shared_ptr> +{ + auto tup = createSystemdSinkTuple(std::move(sinkTable)); + return std::make_shared>(std::get<0>(tup), std::get<1>(tup)); +} +#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 +674,9 @@ auto genFromWinStr(toml::string &&typeStr) -> spdlog::sink_ptr; #endif +#ifdef _KDSPDSETUP_SYSTEMD_ +auto genFromSystemdStr(toml::string &&typeStr, toml::table &&sinkTable) -> spdlog::sink_ptr; + +#endif + } // namespace KDSPDSetup::details diff --git a/src/KDSpdSetup/setup.cpp b/src/KDSpdSetup/setup.cpp index 605c2b2..fa55a7a 100644 --- a/src/KDSpdSetup/setup.cpp +++ b/src/KDSpdSetup/setup.cpp @@ -83,6 +83,13 @@ void setupSink(toml::table &&sinkTable) sinkPtr = details::genFromWinStr(std::move(typeStr)); #else return; +#endif + } else if (details::inTypelist(typeStr, details::systemdStrs)) { +#ifdef _KDSPDSETUP_SYSTEMD_ + + sinkPtr = details::genFromSystemdStr(std::move(typeStr), std::move(sinkTable)); +#else + return; #endif }