Skip to content

Commit 95a58e9

Browse files
committed
Add support for systemd sinks
Change-Id: I2cd3d0c480af9456d551f3f7642aa93764d62a06
1 parent cb731ae commit 95a58e9

File tree

3 files changed

+45
-0
lines changed

3 files changed

+45
-0
lines changed

src/KDSpdSetup/details.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -216,4 +216,16 @@ auto genFromWinStr(toml::string &&typeStr) -> spdlog::sink_ptr
216216
}
217217
#endif
218218

219+
#ifdef _KDSPDSETUP_SYSTEMD_
220+
auto genFromSystemdStr(toml::string &&typeStr, std::string &&ident, bool const enableFormatting) -> spdlog::sink_ptr
221+
{
222+
if (typeStr == "systemd_sink_st") {
223+
return createSystemdSinkStPtr(std::move(ident), enableFormatting);
224+
}
225+
if (typeStr == "systemd_sink_mt") {
226+
return createSystemdSinkMtPtr(std::move(ident), enableFormatting);
227+
}
228+
}
229+
#endif
230+
219231
} // namespace KDSPDSetup::details

src/KDSpdSetup/details.h

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@
2828
#elif _WIN32
2929
#include <spdlog/sinks/msvc_sink.h>
3030
#endif
31+
#if __has_include(<systemd/sd-journal.h>)
32+
#include <spdlog/sinks/systemd_sink.h>
33+
#define _KDSPDSETUP_SYSTEMD_
34+
#endif
3135

3236
#include <toml.hpp>
3337

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

100+
static auto const systemdStrs{ std::vector<std::string>{ "systemd_sink_st", "systemd_sink_mt" } };
101+
96102
/**
97103
* @brief A simple map associating strings of `spdlog::level::level_enum` names to the enums themselves.
98104
* Used to pass an enum to `spdlog::logger::set_level` given a string read from a TOML table.
@@ -312,6 +318,12 @@ class SPDMaps
312318
#define createMsvcSinkMtPtr createMsvcSinkPtr<std::mutex>
313319
#endif
314320

321+
#ifdef _KDSPDSETUP_SYSTEMD_
322+
#define createSystemdSinkStPtr createSystemdSinkPtr<spdlog::details::null_mutex>
323+
324+
#define createSystemdSinkMtPtr createSystemdSinkPtr<std::mutex>
325+
#endif
326+
315327
/**
316328
* @brief Returns true if a string `typeStr` is present in a vector `strList`, and false if not.
317329
* 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<spdlog::sinks::msvc_sink<Mutex>>
574586
}
575587
#endif
576588

589+
#ifdef _KDSPDSETUP_SYSTEMD_
590+
template<typename Mutex>
591+
auto createSystemdSinkPtr(std::string &&ident, bool const enableFormatting) -> std::shared_ptr<spdlog::sinks::systemd_sink<Mutex>>
592+
{
593+
return std::make_shared<spdlog::sinks::systemd_sink<Mutex>>(ident, enableFormatting);
594+
}
595+
#endif
596+
577597
/**
578598
* @brief Return the result of calling KDSPDSetup::details::createFileSinkPtr with the correct template argument
579599
* 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;
651671

652672
#endif
653673

674+
#ifdef _KDSPDSETUP_SYSTEMD_
675+
auto genFromSystemdStr(toml::string &&typeStr, std::string &&ident, bool const enableFormatting) -> spdlog::sink_ptr;
676+
677+
#endif
678+
654679
} // namespace KDSPDSetup::details

src/KDSpdSetup/setup.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,14 @@ void setupSink(toml::table &&sinkTable)
8383
sinkPtr = details::genFromWinStr(std::move(typeStr));
8484
#else
8585
return;
86+
#endif
87+
} else if (details::inTypelist(typeStr, details::systemdStrs)) {
88+
#ifdef _KDSPDSETUP_SYSTEMD_
89+
auto ident = (sinkTable.contains("ident")) ? sinkTable.at("ident").as_string().str : ""s;
90+
auto const enableFormatting = (sinkTable.contains("enable_formatting")) ? sinkTable.at("enable_formatting").as_boolean() : false;
91+
sinkPtr = details::genFromSystemdStr(std::move(typeStr), std::move(ident), enableFormatting);
92+
#else
93+
return;
8694
#endif
8795
}
8896

0 commit comments

Comments
 (0)