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 ringbuffer sinks #11

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
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

auto genFromRingBufferStr(toml::string &&typeStr, std::size_t const nItems) -> spdlog::sink_ptr
{
if (typeStr == "ringbuffer_sink_st") {
return createRingBufferSinkStPtr(nItems);
}
if (typeStr == "ringbuffer_sink_mt") {
return createRingBufferSinkMtPtr(nItems);
}

return nullptr;
}

} // namespace KDSPDSetup::details
15 changes: 15 additions & 0 deletions src/KDSpdSetup/details.h
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
#elif _WIN32
#include <spdlog/sinks/msvc_sink.h>
#endif
#include <spdlog/sinks/ringbuffer_sink.h>

#include <toml.hpp>

Expand Down Expand Up @@ -93,6 +94,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 ringBufferStrs{ std::vector<std::string>{ "ringbuffer_sink_st", "ringbuffer_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 +315,10 @@ class SPDMaps
#define createMsvcSinkMtPtr createMsvcSinkPtr<std::mutex>
#endif

#define createRingBufferSinkStPtr createRingBufferSinkPtr<spdlog::details::null_mutex>
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could we use a "using" instead of a #define for these? Or does that make something down the line more tricky?


#define createRingBufferSinkMtPtr createRingBufferSinkPtr<std::mutex>

/**
* @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 +581,12 @@ auto createMsvcSinkPtr() -> std::shared_ptr<spdlog::sinks::msvc_sink<Mutex>>
}
#endif

template<typename Mutex>
auto createRingBufferSinkPtr(std::size_t const nItems) -> std::shared_ptr<spdlog::sinks::ringbuffer_sink<Mutex>>
{
return std::make_shared<spdlog::sinks::ringbuffer_sink<Mutex>>(nItems);
}

/**
* @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 +664,6 @@ auto genFromWinStr(toml::string &&typeStr) -> spdlog::sink_ptr;

#endif

auto genFromRingBufferStr(toml::string &&typeStr, const std::size_t nItems) -> spdlog::sink_ptr;

} // namespace KDSPDSetup::details
7 changes: 6 additions & 1 deletion src/KDSpdSetup/setup.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ void setupSink(toml::table &&sinkTable)
auto typeStr = sinkTable.at("type").as_string();

bool ok = false;
for (auto &typeList : { details::fileStrs, details::rotateStrs, details::dailyStrs, details::nullStrs, details::stdStrs, details::linuxStrs, details::winStrs }) {
for (auto &typeList : { details::fileStrs, details::rotateStrs, details::dailyStrs, details::nullStrs, details::stdStrs, details::linuxStrs, details::winStrs, details::ringBufferStrs }) {
if (details::inTypelist(typeStr, typeList)) {
ok = true;
break;
Expand Down Expand Up @@ -86,6 +86,11 @@ void setupSink(toml::table &&sinkTable)
#endif
}

else if (details::inTypelist(typeStr, details::ringBufferStrs)) {
auto const nItems = static_cast<size_t>(sinkTable.at("n_items").as_integer());
sinkPtr = details::genFromRingBufferStr(std::move(typeStr), nItems);
}

if (level != "") {
if (!details::levelMap.contains(level))
throw std::out_of_range("KDSPDSetup: level "s + level.str + " not found"s);
Expand Down