Skip to content

Commit

Permalink
Add support for systemd sinks
Browse files Browse the repository at this point in the history
Change-Id: I2cd3d0c480af9456d551f3f7642aa93764d62a06
  • Loading branch information
mattkdab committed May 24, 2024
1 parent cb731ae commit 95a58e9
Show file tree
Hide file tree
Showing 3 changed files with 45 additions and 0 deletions.
12 changes: 12 additions & 0 deletions src/KDSpdSetup/details.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
25 changes: 25 additions & 0 deletions src/KDSpdSetup/details.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@
#elif _WIN32
#include <spdlog/sinks/msvc_sink.h>
#endif
#if __has_include(<systemd/sd-journal.h>)
#include <spdlog/sinks/systemd_sink.h>
#define _KDSPDSETUP_SYSTEMD_
#endif

#include <toml.hpp>

Expand Down Expand Up @@ -93,6 +97,8 @@ static auto const linuxStrs{ std::vector<std::string>{ "syslog_sink_st", "syslog
*/
static auto const winStrs{ std::vector<std::string>{ "msvc_sink_st", "msvc_sink_mt" } };

static auto const systemdStrs{ std::vector<std::string>{ "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.
Expand Down Expand Up @@ -312,6 +318,12 @@ class SPDMaps
#define createMsvcSinkMtPtr createMsvcSinkPtr<std::mutex>
#endif

#ifdef _KDSPDSETUP_SYSTEMD_
#define createSystemdSinkStPtr createSystemdSinkPtr<spdlog::details::null_mutex>

#define createSystemdSinkMtPtr createSystemdSinkPtr<std::mutex>
#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.
Expand Down Expand Up @@ -574,6 +586,14 @@ auto createMsvcSinkPtr() -> std::shared_ptr<spdlog::sinks::msvc_sink<Mutex>>
}
#endif

#ifdef _KDSPDSETUP_SYSTEMD_
template<typename Mutex>
auto createSystemdSinkPtr(std::string &&ident, bool const enableFormatting) -> std::shared_ptr<spdlog::sinks::systemd_sink<Mutex>>
{
return std::make_shared<spdlog::sinks::systemd_sink<Mutex>>(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.
Expand Down Expand Up @@ -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
8 changes: 8 additions & 0 deletions src/KDSpdSetup/setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down

0 comments on commit 95a58e9

Please sign in to comment.