KDSPDSetup
Small library to initialize spdlog loggers from a toml configuration file.
details.h
Go to the documentation of this file.
1 /*
2  This file is part of KDSpdSetup.
3 
4  SPDX-FileCopyrightText: 2023 Klarälvdalens Datakonsult AB, a KDAB Group company <info@kdab.com>
5 
6  SPDX-License-Identifier: MIT
7 
8  Contact KDAB at <info@kdab.com> for commercial licensing options.
9 */
10 #pragma once
11 
12 #include <unordered_map>
13 #include <map>
14 #include <vector>
15 
16 #include <spdlog/async.h>
17 #include <spdlog/spdlog.h>
18 #include <spdlog/async_logger.h>
19 #include <spdlog/pattern_formatter.h>
20 #include <spdlog/sinks/basic_file_sink.h>
21 #include <spdlog/sinks/daily_file_sink.h>
22 #include <spdlog/sinks/null_sink.h>
23 #include <spdlog/sinks/rotating_file_sink.h>
24 #include <spdlog/sinks/stdout_color_sinks.h>
25 #include <spdlog/sinks/stdout_sinks.h>
26 #ifdef __linux__
27 #include <spdlog/sinks/syslog_sink.h>
28 #elif _WIN32
29 #include <spdlog/sinks/msvc_sink.h>
30 #endif
31 
32 #include <toml.hpp>
33 
41 namespace KDSPDSetup::details {
42 
48 static auto const stdStrs{ std::vector<std::string>{ "stdout_sink_st", "stdout_sink_mt", "stdout_color_sink_st",
49  "stdout_color_sink_mt", "color_stdout_sink_st",
50  "color_stdout_sink_mt", "stderr_sink_st", "stderr_sink_mt",
51  "stderr_color_sink_st", "stderr_color_sink_mt",
52  "color_stderr_sink_st", "color_stderr_sink_mt" } };
53 
59 static auto const fileStrs{ std::vector<std::string>{ "basic_file_sink_st", "basic_file_sink_mt" } };
60 
66 static auto const rotateStrs{ std::vector<std::string>{ "rotating_file_sink_st", "rotating_file_sink_mt" } };
67 
73 static auto const dailyStrs{ std::vector<std::string>{ "daily_file_sink_st", "daily_file_sink_mt" } };
74 
80 static auto const nullStrs{ std::vector<std::string>{ "null_sink_st", "null_sink_mt" } };
81 
87 static auto const linuxStrs{ std::vector<std::string>{ "syslog_sink_st", "syslog_sink_mt" } };
88 
94 static auto const winStrs{ std::vector<std::string>{ "msvc_sink_st", "msvc_sink_mt" } };
95 
101 static auto const levelMap{ std::unordered_map<std::string, spdlog::level::level_enum>{
102  { "critical", spdlog::level::level_enum::critical },
103  { "debug", spdlog::level::level_enum::debug },
104  { "err", spdlog::level::level_enum::err },
105  { "info", spdlog::level::level_enum::info },
106  { "n_levels", spdlog::level::level_enum::n_levels },
107  { "off", spdlog::level::level_enum::off },
108  { "trace", spdlog::level::level_enum::trace },
109  { "warn", spdlog::level::level_enum::warn },
110 } };
111 
118 static auto const overflowMap{ std::map<toml::string, spdlog::async_overflow_policy>{
119  { "overrun_oldest", spdlog::async_overflow_policy::overrun_oldest },
120  { "block", spdlog::async_overflow_policy::block },
121 } };
122 
129 class SPDMaps
130 {
131 public:
139  static auto sinkMap() -> std::map<toml::string, spdlog::sink_ptr> const &;
140 
149  static auto patternMap() -> std::map<toml::string, toml::string> const &;
150 
159  static auto threadPoolMap() -> std::map<toml::string, std::pair<std::size_t, std::size_t>> const &;
160 
169  static void emplaceSinkMap(std::pair<toml::string, spdlog::sink_ptr> &&_pr);
170 
181  static void emplacePatternMap(std::pair<toml::string, toml::string> &&_pr);
182 
192  static void emplaceThreadPoolMap(std::pair<toml::string, std::pair<std::size_t, std::size_t>> &&_pr);
193 
194 private:
201  static inline std::map<toml::string, spdlog::sink_ptr> mSinkMap{};
202 
209  static inline std::map<toml::string, toml::string> mPatternMap{};
210 
219  static inline std::map<toml::string, std::pair<std::size_t, std::size_t>> mThreadPoolMap{};
220 };
221 
226 #define createRotatingFileSinkStPtr createRotatingFileSinkPtr<spdlog::details::null_mutex>
227 
232 #define createRotatingFileSinkMtPtr createRotatingFileSinkPtr<std::mutex>
233 
238 #define createFileSinkStPtr createFileSinkPtr<spdlog::details::null_mutex>
239 
244 #define createFileSinkMtPtr createFileSinkPtr<std::mutex>
245 
250 #define createDailyFileSinkStPtr createDailyFileSinkPtr<spdlog::details::null_mutex>
251 
256 #define createDailyFileSinkMtPtr createDailyFileSinkPtr<std::mutex>
257 
262 #define createStdoutSinkStPtr createStdoutSinkPtr<spdlog::details::console_nullmutex>
263 
268 #define createStdoutSinkMtPtr createStdoutSinkPtr<spdlog::details::console_mutex>
269 
270 #define createStderrSinkStPtr createStderrSinkPtr<spdlog::details::console_nullmutex>
271 #define createStderrSinkMtPtr createStderrSinkPtr<spdlog::details::console_mutex>
272 
277 #define createStdoutColorSinkStPtr createStdoutColorSinkPtr<spdlog::details::console_nullmutex>
278 
283 #define createStdoutColorSinkMtPtr createStdoutColorSinkPtr<spdlog::details::console_mutex>
284 
285 #define createStderrColorSinkStPtr createStderrColorSinkPtr<spdlog::details::console_nullmutex>
286 #define createStderrColorSinkMtPtr createStderrColorSinkPtr<spdlog::details::console_mutex>
287 
288 #ifdef __linux__
293 #define createSyslogSinkStPtr createSyslogSinkPtr<spdlog::details::null_mutex>
294 
299 #define createSyslogSinkMtPtr createSyslogSinkPtr<std::mutex>
300 
301 #elif _WIN32
306 #define createMsvcSinkStPtr createMsvcSinkPtr<spdlog::details::null_mutex>
307 
312 #define createMsvcSinkMtPtr createMsvcSinkPtr<std::mutex>
313 #endif
314 
326 bool inTypelist(std::string const &typeStr, std::vector<std::string> const &strList);
327 
342 auto createRotatingFileSinkTuple(toml::table const &sinkTable, toml::string &&baseFilename,
343  std::size_t const &maxFiles)
344  -> std::tuple<toml::string const, std::size_t const, std::size_t const>;
345 
360 template<typename Mutex>
361 auto createRotatingFileSinkPtr(toml::table const &sinkTable, toml::string &&baseFilename,
362  std::size_t const &maxFiles)
363  -> std::shared_ptr<spdlog::sinks::rotating_file_sink<Mutex>>
364 {
365  auto tup = createRotatingFileSinkTuple(sinkTable, std::move(baseFilename), maxFiles);
366  return std::make_shared<spdlog::sinks::rotating_file_sink<Mutex>>(std::get<0>(tup), std::get<1>(tup),
367  std::get<2>(tup));
368 }
369 
380 auto createFileSinkTuple(toml::table const &sinkTable, bool const &truncate)
381  -> std::tuple<toml::string const, bool const>;
382 
396 template<typename Mutex>
397 auto createFileSinkPtr(toml::table const &sinkTable, bool const &truncate)
398  -> std::shared_ptr<spdlog::sinks::basic_file_sink<Mutex>>
399 {
400  auto tup = createFileSinkTuple(sinkTable, truncate);
401  return std::make_shared<spdlog::sinks::basic_file_sink<Mutex>>(std::get<0>(tup), std::get<1>(tup));
402 }
403 
419 auto createDailyFileSinkTuple(toml::table &&sinkTable, bool const &truncate, toml::string &&baseFilename,
420  uint16_t const &maxFiles)
421  -> std::tuple<toml::string const, int const, int const, bool const, uint16_t const>;
422 
438 template<typename Mutex>
439 auto createDailyFileSinkPtr(toml::table &&sinkTable, bool const &truncate, toml::string &&baseFilename,
440  uint16_t const &maxFiles) -> std::shared_ptr<spdlog::sinks::daily_file_sink<Mutex>>
441 {
442  auto tup = createDailyFileSinkTuple(std::move(sinkTable), truncate, std::move(baseFilename), maxFiles);
443  return std::make_shared<spdlog::sinks::daily_file_sink<Mutex>>(std::get<0>(tup), std::get<1>(tup), std::get<2>(tup),
444  std::get<3>(tup), std::get<4>(tup));
445 }
446 
453 auto createNullSinkPtr() -> std::shared_ptr<spdlog::sinks::null_sink<spdlog::details::null_mutex>>;
454 
464 template<typename Mutex>
465 auto createStdoutSinkPtr() -> std::shared_ptr<spdlog::sinks::stdout_sink<Mutex>>
466 {
467  return std::make_shared<spdlog::sinks::stdout_sink<Mutex>>();
468 }
469 
470 template<typename Mutex>
471 auto createStderrSinkPtr() -> std::shared_ptr<spdlog::sinks::stderr_sink<Mutex>>
472 {
473  return std::make_shared<spdlog::sinks::stderr_sink<Mutex>>();
474 }
475 
476 #ifdef _WIN32
487 template<typename Mutex>
488 auto createStdoutColorSinkPtr() -> std::shared_ptr<spdlog::sinks::wincolor_stdout_sink<Mutex>>
489 {
490  return std::make_shared<spdlog::sinks::wincolor_stdout_sink<Mutex>>();
491 }
492 
493 template<typename Mutex>
494 auto createStderrColorSinkPtr() -> std::shared_ptr<spdlog::sinks::wincolor_stderr_sink<Mutex>>
495 {
496  return std::make_shared<spdlog::sinks::wincolor_stderr_sink<Mutex>>();
497 }
498 
499 #else
510 template<typename Mutex>
511 auto createStdoutColorSinkPtr() -> std::shared_ptr<spdlog::sinks::ansicolor_stdout_sink<Mutex>>
512 {
513  return std::make_shared<spdlog::sinks::ansicolor_stdout_sink<Mutex>>();
514 }
515 
516 template<typename Mutex>
517 auto createStderrColorSinkPtr() -> std::shared_ptr<spdlog::sinks::ansicolor_stderr_sink<Mutex>>
518 {
519  return std::make_shared<spdlog::sinks::ansicolor_stderr_sink<Mutex>>();
520 }
521 
522 #endif
523 
524 #ifdef __linux__
537 auto createSyslogSinkTuple(toml::table const &sinkTable)
538  -> std::tuple<toml::string const, int const, int const, bool const>;
539 
552 template<typename Mutex>
553 auto createSyslogSinkPtr(toml::table &&sinkTable) -> std::shared_ptr<spdlog::sinks::syslog_sink<Mutex>>
554 {
555  auto tup = createSyslogSinkTuple(std::move(sinkTable));
556  return std::make_shared<spdlog::sinks::syslog_sink<Mutex>>(std::get<0>(tup), std::get<1>(tup), std::get<2>(tup),
557  std::get<3>(tup));
558 }
559 
560 #elif _WIN32
570 template<typename Mutex>
571 auto createMsvcSinkPtr() -> std::shared_ptr<spdlog::sinks::msvc_sink<Mutex>>
572 {
573  return std::make_shared<spdlog::sinks::msvc_sink<Mutex>>();
574 }
575 #endif
576 
587 auto genFromFileStr(toml::string &&typeStr, toml::table &&sinkTable, bool const &truncate) -> spdlog::sink_ptr;
588 
600 auto genFromRotateStr(toml::string &&typeStr, toml::table &&sinkTable, toml::string &&baseFilename,
601  std::size_t const &maxFiles) -> spdlog::sink_ptr;
602 
615 auto genFromDailyStr(toml::string &&typeStr, toml::table &&sinkTable, bool const &truncate,
616  toml::string &&baseFilename, uint16_t const &maxFiles) -> spdlog::sink_ptr;
617 
627 auto genFromNullOrStdStr(toml::string &&typeStr) -> spdlog::sink_ptr;
628 
629 #ifdef __linux__
639 auto genFromLinuxStr(toml::string &&typeStr, toml::table &&sinkTable) -> spdlog::sink_ptr;
640 
641 #elif _WIN32
650 auto genFromWinStr(toml::string &&typeStr) -> spdlog::sink_ptr;
651 
652 #endif
653 
654 } // namespace KDSPDSetup::details
This class contains maps that associate string names to sink pointers, format strings,...
Definition: details.h:130
static std::map< toml::string, spdlog::sink_ptr > mSinkMap
Map that associates string names to sink pointers. When sink pointers are created from sink tables in...
Definition: details.h:201
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 ...
Definition: details.cpp:26
static std::map< toml::string, std::pair< std::size_t, std::size_t > > mThreadPoolMap
Map that associates string names to pairs of thread pool fields. The pairs represent the queue size a...
Definition: details.h:219
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...
Definition: details.cpp:14
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...
Definition: details.cpp:18
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....
Definition: details.cpp:22
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....
Definition: details.cpp:34
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 ...
Definition: details.cpp:30
static std::map< toml::string, toml::string > mPatternMap
Map that associates string names to pattern strings. When pattern tables are read from a configuratio...
Definition: details.h:209
This namespace contains implementation details and helpers used by functions in KDSPDSetup::setup....
Definition: details.cpp:12
static auto const linuxStrs
Vector of strings of spdlog syslog sink typenames. Used when matching a type string from a TOML table...
Definition: details.h:87
static auto const dailyStrs
Vector of strings of spdlog daily file sink typenames. Used when matching a type string from a TOML t...
Definition: details.h:73
static auto const nullStrs
Vector of strings of spdlog null sink typenames. Used when matching a type string from a TOML table t...
Definition: details.h:80
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...
Definition: details.cpp:147
static auto const fileStrs
Vector of strings of spdlog basic file sink typenames. Used when matching a type string from a TOML t...
Definition: details.h:59
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....
Definition: details.cpp:39
static auto const rotateStrs
Vector of strings of spdlog rotating file sink typenames. Used when matching a type string from a TOM...
Definition: details.h:66
auto createStderrSinkPtr() -> std::shared_ptr< spdlog::sinks::stderr_sink< Mutex >>
Definition: details.h:471
static auto const winStrs
Vector of strings of spdlog MSVC sink typenames. Used when matching a type string from a TOML table t...
Definition: details.h:94
auto createNullSinkPtr() -> std::shared_ptr< spdlog::sinks::null_sink< spdlog::details::null_mutex >>
Create a null sink shared pointer and return it.
Definition: details.cpp:100
auto createFileSinkTuple(toml::table const &sinkTable, bool const &truncate) -> std::tuple< toml::string const, bool const >
Generates the argument list for basic file sink constructor as a tuple and returns it....
Definition: details.cpp:84
static auto const overflowMap
A simple map associating strings of spdlog::async_overflow_policy names to the enums themselves....
Definition: details.h:118
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...
Definition: details.cpp:122
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...
Definition: details.cpp:134
auto createStdoutColorSinkPtr() -> std::shared_ptr< spdlog::sinks::ansicolor_stdout_sink< Mutex >>
Create an color standard output sink shared pointer and return it. The macros createStdoutColorSinkSt...
Definition: details.h:511
auto createStderrColorSinkPtr() -> std::shared_ptr< spdlog::sinks::ansicolor_stderr_sink< Mutex >>
Definition: details.h:517
auto createStdoutSinkPtr() -> std::shared_ptr< spdlog::sinks::stdout_sink< Mutex >>
Create a standard output sink shared pointer and return it. The macros createStdoutSinkStPtr and crea...
Definition: details.h:465
auto createRotatingFileSinkTuple(toml::table const &sinkTable, toml::string &&baseFilename, std::size_t const &maxFiles) -> std::tuple< toml::string const, std::size_t const, std::size_t const >
Generates the argument list for rotating file sink constructor as a tuple and returns it....
Definition: details.cpp:44
static auto const levelMap
A simple map associating strings of spdlog::level::level_enum names to the enums themselves....
Definition: details.h:101
auto createDailyFileSinkTuple(toml::table &&sinkTable, bool const &truncate, toml::string &&baseFilename, uint16_t const &maxFiles) -> std::tuple< toml::string const, int const, int const, bool const, uint16_t const >
Generates the argument list for daily file sink constructor as a tuple and returns it....
Definition: details.cpp:90
auto createRotatingFileSinkPtr(toml::table const &sinkTable, toml::string &&baseFilename, std::size_t const &maxFiles) -> std::shared_ptr< spdlog::sinks::rotating_file_sink< Mutex >>
Create a rotating file sink shared pointer and return it. Calls KDSPDSetup::details::createRotatingFi...
Definition: details.h:361
auto createDailyFileSinkPtr(toml::table &&sinkTable, bool const &truncate, toml::string &&baseFilename, uint16_t const &maxFiles) -> std::shared_ptr< spdlog::sinks::daily_file_sink< Mutex >>
Create a daily file sink shared pointer and return it. Calls KDSPDSetup::details::createDailyFileSink...
Definition: details.h:439
auto createFileSinkPtr(toml::table const &sinkTable, bool const &truncate) -> std::shared_ptr< spdlog::sinks::basic_file_sink< Mutex >>
Create a basic file sink shared pointer and return it. Calls KDSPDSetup::details::createFileSinkTuple...
Definition: details.h:397
auto genFromNullOrStdStr(toml::string &&typeStr) -> spdlog::sink_ptr
Return the result of calling KDSPDSetup::details::createStdoutSinkPtr with the correct template argum...
Definition: details.cpp:160
static auto const stdStrs
Vector of strings of spdlog standard output sink typenames. Used when matching a type string from a T...
Definition: details.h:48