Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for systemd sinks #12

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 24 additions & 0 deletions src/KDSpdSetup/details.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -119,6 +119,16 @@ auto createSyslogSinkTuple(toml::table const &sinkTable)
}
#endif

#ifdef _KDSPDSETUP_SYSTEMD_
auto createSystemdSinkTuple(toml::table &&sinkTable) -> std::tuple<std::string const, bool const>
{
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") {
Expand Down Expand Up @@ -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
28 changes: 28 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,17 @@ auto createMsvcSinkPtr() -> std::shared_ptr<spdlog::sinks::msvc_sink<Mutex>>
}
#endif

#ifdef _KDSPDSETUP_SYSTEMD_
auto createSystemdSinkTuple(toml::table &&sinkTable) -> std::tuple<std::string const, bool const>;

template<typename Mutex>
auto createSystemdSinkPtr(toml::table &&sinkTable) -> std::shared_ptr<spdlog::sinks::systemd_sink<Mutex>>
{
auto tup = createSystemdSinkTuple(std::move(sinkTable));
return std::make_shared<spdlog::sinks::systemd_sink<Mutex>>(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.
Expand Down Expand Up @@ -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
7 changes: 7 additions & 0 deletions src/KDSpdSetup/setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down