14 using std::literals::string_literals::operator
""s;
18 void handleMultipleFileSink(toml::string &&typeStr, toml::table &&sinkTable, spdlog::sink_ptr &sinkPtr,
bool const &truncate)
20 auto baseFilename = sinkTable.at(
"base_filename").as_string();
21 auto maxFiles = (sinkTable.contains(
"max_files")) ?
static_cast<uint16_t
>(sinkTable.at(
"max_files").as_integer()) : uint16_t{ 0 };
28 sinkPtr =
details::genFromDailyStr(std::move(typeStr), std::move(sinkTable), truncate, std::move(baseFilename), maxFiles);
34 auto const truncate = (sinkTable.contains(
"truncate")) ? sinkTable.at(
"truncate").as_boolean() :
false;
45 spdlog::sink_ptr sinkPtr;
47 auto const name = sinkTable.at(
"name").as_string();
48 auto typeStr = sinkTable.at(
"type").as_string();
59 throw std::out_of_range(
"KDSPDSetup: sink type "s + typeStr.str +
" is not valid"s);
61 toml::string level =
"";
62 if (sinkTable.contains(
"level")) {
63 level = sinkTable.at(
"level").as_string();
77 sinkPtr = details::genFromLinuxStr(std::move(typeStr), std::move(sinkTable));
83 sinkPtr = details::genFromWinStr(std::move(typeStr));
91 throw std::out_of_range(
"KDSPDSetup: level "s + level.str +
" not found"s);
101 auto sinks = toml::find<std::vector<toml::table>>(data,
"sink");
103 for (
auto &&sinkTable : sinks) {
110 if (data.contains(
"global_pattern")) {
111 auto const globalPattern = toml::find<toml::string>(data,
"global_pattern");
112 spdlog::set_pattern(globalPattern);
115 if (data.contains(
"pattern")) {
116 auto const patterns = toml::find<std::vector<toml::table>>(data,
"pattern");
118 for (
auto &&patternTable : patterns) {
119 auto const name = patternTable.at(
"name").as_string();
120 auto const value = patternTable.at(
"value").as_string();
129 if (data.contains(
"global_thread_pool")) {
130 auto const globalThreadPool = toml::find(data,
"global_thread_pool");
132 auto const queueSize = toml::find<std::size_t>(globalThreadPool,
"queue_size");
133 auto const numThreads = toml::find<std::size_t>(globalThreadPool,
"num_threads");
134 spdlog::init_thread_pool(queueSize, numThreads);
137 if (data.contains(
"thread_pool")) {
138 auto const threadPools = toml::find<std::vector<toml::table>>(data,
"thread_pool");
140 for (
auto &&poolTable : threadPools) {
141 auto const name = poolTable.at(
"name").as_string();
142 auto const queueSize =
static_cast<std::size_t
>(poolTable.at(
"queue_size").as_integer());
143 auto const numThreads =
static_cast<std::size_t
>(poolTable.at(
"num_threads").as_integer());
151 toml::string
const &pattern)
153 auto const threadPool = (loggerTable.contains(
"thread_pool")) ? loggerTable.at(
"thread_pool").as_string() :
"";
155 static std::shared_ptr<spdlog::details::thread_pool> threadPoolPtr;
157 if (threadPool !=
"") {
159 throw std::out_of_range(
"KDSPDSetup: threadpool "s + threadPool.str +
" not found"s);
162 auto const queueSize = threadPoolPair.first;
163 auto const numThreads = threadPoolPair.second;
164 threadPoolPtr = std::make_shared<spdlog::details::thread_pool>(queueSize, numThreads);
166 threadPoolPtr = spdlog::thread_pool();
169 auto const overflowPolicy =
170 (loggerTable.contains(
"overflow_policy")) ? loggerTable.at(
"overflow_policy").as_string() :
"block";
173 throw std::out_of_range(
"KDSPDSetup: overflow policy "s + overflowPolicy.str +
" not found"s);
175 auto logger = std::make_shared<spdlog::async_logger>(name, sinkList.begin(), sinkList.end(), std::move(threadPoolPtr),
179 logger->set_pattern(pattern);
182 spdlog::register_logger(logger);
186 toml::string
const &pattern)
188 auto logger = std::make_shared<spdlog::logger>(name, sinkList.cbegin(), sinkList.cend());
190 logger->set_pattern(
static_cast<std::string
>(pattern));
193 if (loggerTable.contains(
"level")) {
194 auto const level = loggerTable.at(
"level").as_string();
197 throw std::out_of_range(
"KDSPDSetup: level "s + level.str +
" not found"s);
202 spdlog::register_logger(logger);
207 auto const name = loggerTable.at(
"name").as_string();
208 if (spdlog::get(name) !=
nullptr) {
212 auto const sinks = loggerTable.at(
"sinks").as_array();
214 auto sinkList = std::vector<spdlog::sink_ptr>{};
215 for (
auto &&sink : sinks) {
220 std::cerr <<
"KDSPDSetup: setting up logger " << name
221 <<
" - skipped sink " << sink.as_string().str <<
" as it was not found";
225 std::string pattern =
"";
227 if (loggerTable.contains(
"pattern")) {
229 throw std::out_of_range(
"KDSPDSetup: pattern "s + loggerTable.at(
"pattern").as_string().str +
" not found"s);
234 auto const type = (loggerTable.contains(
"type")) ? loggerTable.at(
"type").as_string() :
"";
236 if (type ==
"async") {
245 auto const loggers = toml::find<std::vector<toml::table>>(data,
"logger");
246 for (
auto &&loggerTable : loggers) {
static void emplaceSinkMap(std::pair< toml::string, spdlog::sink_ptr > &&_pr)
Emplace a pair in the private member KDSPDSetup::details::SPDMaps::mSinkMap. This function is called ...
static auto sinkMap() -> std::map< toml::string, spdlog::sink_ptr > const &
Getter method for the private member KDSPDSetup::details::SPDMaps::mSinkMap. Used to index the map an...
static auto patternMap() -> std::map< toml::string, toml::string > const &
Getter method for the private member KDSPDSetup::details::SPDMaps::mPatternMap. Used to index the map...
static auto threadPoolMap() -> std::map< toml::string, std::pair< std::size_t, std::size_t >> const &
Getter method for the private member KDSPDSetup::details::SPDMaps::mThreadPoolMap....
static void emplaceThreadPoolMap(std::pair< toml::string, std::pair< std::size_t, std::size_t >> &&_pr)
Emplace a pair in the private member KDSPDSetup::details::SPDMaps::mThreadPoolMap....
static void emplacePatternMap(std::pair< toml::string, toml::string > &&_pr)
Emplace a pair in the private member KDSPDSetup::details::SPDMaps::mSinkMap. This function is called ...
static auto const linuxStrs
Vector of strings of spdlog syslog sink typenames. Used when matching a type string from a TOML table...
static auto const dailyStrs
Vector of strings of spdlog daily file sink typenames. Used when matching a type string from a TOML t...
static auto const nullStrs
Vector of strings of spdlog null sink typenames. Used when matching a type string from a TOML table t...
auto genFromDailyStr(toml::string &&typeStr, toml::table &&sinkTable, bool const &truncate, toml::string &&baseFilename, uint16_t const &maxFiles) -> spdlog::sink_ptr
Return the result of calling KDSPDSetup::details::createDailyFileSinkPtr with the correct template ar...
static auto const fileStrs
Vector of strings of spdlog basic file sink typenames. Used when matching a type string from a TOML t...
bool inTypelist(std::string const &typeStr, std::vector< std::string > const &strList)
Returns true if a string typeStr is present in a vector strList, and false if not....
static auto const rotateStrs
Vector of strings of spdlog rotating file sink typenames. Used when matching a type string from a TOM...
static auto const winStrs
Vector of strings of spdlog MSVC sink typenames. Used when matching a type string from a TOML table t...
static auto const overflowMap
A simple map associating strings of spdlog::async_overflow_policy names to the enums themselves....
auto genFromFileStr(toml::string &&typeStr, toml::table &&sinkTable, bool const &truncate) -> spdlog::sink_ptr
Return the result of calling KDSPDSetup::details::createFileSinkPtr with the correct template argumen...
auto genFromRotateStr(toml::string &&typeStr, toml::table &&sinkTable, toml::string &&baseFilename, std::size_t const &maxFiles) -> spdlog::sink_ptr
Return the result of calling KDSPDSetup::details::createRotatingFileSinkPtr with the correct template...
static auto const levelMap
A simple map associating strings of spdlog::level::level_enum names to the enums themselves....
auto genFromNullOrStdStr(toml::string &&typeStr) -> spdlog::sink_ptr
Return the result of calling KDSPDSetup::details::createStdoutSinkPtr with the correct template argum...
static auto const stdStrs
Vector of strings of spdlog standard output sink typenames. Used when matching a type string from a T...
This namespace contains the functions directly called by KDSPDSetup::setupFrom, along with helpers ca...
void setupLoggers(toml::value const &data)
Using data read from a toml file, extract vector of tables representing every logger specified....
void setupPatterns(toml::value const &data)
Using data read from a toml file, search for the key global_pattern. If it exists,...
void setupLogger(toml::table const &loggerTable)
Given a table representation of a logger whose name is not already registered with spdlog,...
void setupSink(toml::table &&sinkTable)
Given a table representation of a sink, creates an spdlog::sink_ptr, and evaluates the type of sink s...
void registerSynchronousLogger(toml::table const &loggerTable, toml::string const &name, std::vector< spdlog::sink_ptr > const &sinkList, toml::string const &pattern)
Creates an std::shared_ptr<spdlog::logger> initialized with name and the sink pointers in sinklist,...
void handleMultipleFileSink(toml::string &&typeStr, toml::table &&sinkTable, spdlog::sink_ptr &sinkPtr, bool const &truncate)
Helper function for setting up sinks that use multiple files. The function extracts values for base_f...
void setupSinks(toml::value const &data)
Using data read from a toml file, extract vector of tables representing every sink specified....
void setupThreadPools(toml::value const &data)
Using data read from a toml file, search for a table global_thread_pool, and initialize it with the s...
void handleTruncatableSink(toml::string &&typeStr, toml::table &&sinkTable, spdlog::sink_ptr &sinkPtr)
Helper function for setting up file-based sinks that require a truncate argument in their constructor...
void registerAsynchronousLogger(toml::table const &loggerTable, toml::string const &name, std::vector< spdlog::sink_ptr > const &sinkList, toml::string const &pattern)
Creates an std::shared_ptr<spdlog::async_logger> initialized with name, the sink pointers in sinklist...