KDSPDSetup
Small library to initialize spdlog loggers from a toml configuration file.
details.cpp
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 #include "details.h"
11 
13 
14 auto SPDMaps::sinkMap() -> std::map<toml::string, spdlog::sink_ptr> const &
15 {
16  return mSinkMap;
17 }
18 auto SPDMaps::patternMap() -> std::map<toml::string, toml::string> const &
19 {
20  return mPatternMap;
21 }
22 auto SPDMaps::threadPoolMap() -> std::map<toml::string, std::pair<std::size_t, std::size_t>> const &
23 {
24  return mThreadPoolMap;
25 }
26 void SPDMaps::emplaceSinkMap(std::pair<toml::string, spdlog::sink_ptr> &&_pr)
27 {
28  mSinkMap.emplace(_pr);
29 }
30 void SPDMaps::emplacePatternMap(std::pair<toml::string, toml::string> &&_pr)
31 {
32  mPatternMap.emplace(_pr);
33 }
34 void SPDMaps::emplaceThreadPoolMap(std::pair<toml::string, std::pair<std::size_t, std::size_t>> &&_pr)
35 {
36  mThreadPoolMap.emplace(_pr);
37 }
38 
39 bool inTypelist(std::string const &typeStr, std::vector<std::string> const &strList)
40 {
41  return std::find(strList.cbegin(), strList.cend(), typeStr) != std::cend(strList);
42 }
43 
44 auto createRotatingFileSinkTuple(toml::table const &sinkTable, toml::string &&baseFilename,
45  std::size_t const &maxFiles)
46  -> std::tuple<toml::string const, std::size_t const, std::size_t const>
47 {
48  auto const maxSize = static_cast<std::string>(sinkTable.at("max_size").as_string());
49  auto const maxSizeStrBack = maxSize.back();
50  std::size_t maxSizeInt{};
51 
52  if (std::string{ "TGMK" }.find(maxSizeStrBack) != std::string::npos) {
53  auto const prefixStr = maxSize.substr(0, maxSize.size() - 1);
54  maxSizeInt = std::stoi(prefixStr);
55 
56  std::size_t constexpr kilo = 1024;
57  std::size_t constexpr mega = kilo * kilo;
58  std::size_t constexpr giga = mega * kilo;
59  std::size_t constexpr tera = giga * kilo;
60 
61  switch (maxSizeStrBack) {
62  case 'T':
63  maxSizeInt *= tera;
64  break;
65  case 'G':
66  maxSizeInt *= giga;
67  break;
68  case 'M':
69  maxSizeInt *= mega;
70  break;
71  case 'K':
72  maxSizeInt *= kilo;
73  break;
74  default:
75  break;
76  }
77  } else {
78  maxSizeInt = std::stoi(maxSize);
79  }
80 
81  return std::make_tuple(std::move(baseFilename), maxSizeInt, maxFiles);
82 }
83 
84 auto createFileSinkTuple(toml::table const &sinkTable, bool const &truncate) -> std::tuple<toml::string const, bool const>
85 {
86  auto fileName = sinkTable.at("filename").as_string();
87  return std::make_tuple(std::move(fileName), truncate);
88 }
89 
90 auto createDailyFileSinkTuple(toml::table &&sinkTable, bool const &truncate, toml::string &&baseFilename,
91  uint16_t const &maxFiles)
92  -> std::tuple<toml::string const, int const, int const, bool const, uint16_t const>
93 {
94  auto rotationHour = static_cast<int>(sinkTable.at("rotation_hour").as_integer());
95  auto rotationMinute = static_cast<int>(sinkTable.at("rotation_minute").as_integer());
96 
97  return std::make_tuple(std::move(baseFilename), rotationHour, rotationMinute, truncate, maxFiles);
98 }
99 
100 auto createNullSinkPtr() -> std::shared_ptr<spdlog::sinks::null_sink<spdlog::details::null_mutex>>
101 {
102  return std::make_shared<spdlog::sinks::null_sink<spdlog::details::null_mutex>>();
103 }
104 
105 #ifdef __linux__
106 auto createSyslogSinkTuple(toml::table const &sinkTable)
107  -> std::tuple<toml::string const, int const, int const, bool const>
108 {
109  auto ident = (sinkTable.contains("ident")) ? sinkTable.at("ident").as_string() : "";
110 
111  auto syslogOption = (sinkTable.contains("syslog_option")) ? static_cast<int>(sinkTable.at("syslog_option").as_integer()) : int{ 0 };
112 
113  auto syslogFacility =
114  (sinkTable.contains("syslog_facility")) ? static_cast<int>(sinkTable.at("syslog_facility").as_integer()) : LOG_USER; // macro
115 
116  bool enableFormatting = true;
117 
118  return std::make_tuple(std::move(ident), syslogOption, syslogFacility, enableFormatting);
119 }
120 #endif
121 
122 auto genFromFileStr(toml::string &&typeStr, toml::table &&sinkTable, bool const &truncate) -> spdlog::sink_ptr
123 {
124  if (typeStr == "basic_file_sink_st") {
125  return createFileSinkStPtr(sinkTable, truncate);
126  }
127  if (typeStr == "basic_file_sink_mt") {
128  return createFileSinkMtPtr(sinkTable, truncate);
129  }
130 
131  return nullptr;
132 }
133 
134 auto genFromRotateStr(toml::string &&typeStr, toml::table &&sinkTable, toml::string &&baseFilename,
135  std::size_t const &maxFiles) -> spdlog::sink_ptr
136 {
137  if (typeStr == "rotating_file_sink_st") {
138  return createRotatingFileSinkStPtr(std::move(sinkTable), std::move(baseFilename), maxFiles);
139  }
140  if (typeStr == "rotating_file_sink_mt") {
141  return createRotatingFileSinkMtPtr(std::move(sinkTable), std::move(baseFilename), maxFiles);
142  }
143 
144  return nullptr;
145 }
146 
147 auto genFromDailyStr(toml::string &&typeStr, toml::table &&sinkTable, bool const &truncate,
148  toml::string &&baseFilename, uint16_t const &maxFiles) -> spdlog::sink_ptr
149 {
150  if (typeStr == "daily_file_sink_st") {
151  return createDailyFileSinkStPtr(std::move(sinkTable), truncate, std::move(baseFilename), maxFiles);
152  }
153  if (typeStr == "daily_file_sink_mt") {
154  return createDailyFileSinkMtPtr(std::move(sinkTable), truncate, std::move(baseFilename), maxFiles);
155  }
156 
157  return nullptr;
158 }
159 
160 auto genFromNullOrStdStr(toml::string &&typeStr) -> spdlog::sink_ptr
161 {
162  if (typeStr == "null_sink_st" || typeStr == "null_sink_mt") {
163  return createNullSinkPtr();
164  }
165  if (typeStr == "stdout_sink_st") {
166  return createStdoutSinkStPtr();
167  }
168  if (typeStr == "stdout_sink_mt") {
169  return createStdoutSinkMtPtr();
170  }
171  if (typeStr == "stdout_color_sink_st" || typeStr == "color_stdout_sink_st") {
173  }
174  if (typeStr == "stdout_color_sink_mt" || typeStr == "color_stdout_sink_mt") {
176  }
177  if (typeStr == "stderr_sink_st") {
178  return createStderrSinkStPtr();
179  }
180  if (typeStr == "stderr_sink_mt") {
181  return createStderrSinkMtPtr();
182  }
183  if (typeStr == "stderr_color_sink_st" || typeStr == "color_stderr_sink_st") {
185  }
186  if (typeStr == "stderr_color_sink_mt" || typeStr == "color_stderr_sink_mt") {
188  }
189 
190  return nullptr;
191 }
192 
193 #ifdef __linux__
194 auto genFromLinuxStr(toml::string &&typeStr, toml::table &&sinkTable) -> spdlog::sink_ptr
195 {
196  if (typeStr == "syslog_sink_st") {
197  return createSyslogSinkStPtr(std::move(sinkTable));
198  }
199  if (typeStr == "syslog_sink_mt") {
200  return createSyslogSinkMtPtr(std::move(sinkTable));
201  }
202 
203  return nullptr;
204 }
205 #elif _WIN32
206 auto genFromWinStr(toml::string &&typeStr) -> spdlog::sink_ptr
207 {
208  if (typeStr == "msvc_sink_st") {
209  return createMsvcSinkStPtr();
210  }
211  if (typeStr == "msvc_sink_mt") {
212  return createMsvcSinkMtPtr();
213  }
214 
215  return nullptr;
216 }
217 #endif
218 
219 } // namespace KDSPDSetup::details
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
#define createRotatingFileSinkStPtr
A macro alias to improve readability of calls to KDSPDSetup::details::createRotatingFileSinkPtr.
Definition: details.h:226
#define createRotatingFileSinkMtPtr
A macro alias to improve readability of calls to KDSPDSetup::details::createRotatingFileSinkPtr.
Definition: details.h:232
#define createStderrColorSinkStPtr
Definition: details.h:285
#define createFileSinkMtPtr
A macro alias to improve readability of calls to KDSPDSetup::details::createFileSinkPtr.
Definition: details.h:244
#define createStdoutSinkMtPtr
A macro alias to improve readability of calls to KDSPDSetup::details::createStdoutSinkPtr.
Definition: details.h:268
#define createStdoutColorSinkStPtr
A macro alias to improve readability of calls to KDSPDSetup::details::createStdoutColorSinkPtr.
Definition: details.h:277
#define createStderrSinkStPtr
Definition: details.h:270
#define createStdoutColorSinkMtPtr
A macro alias to improve readability of calls to KDSPDSetup::details::createStdoutColorSinkPtr.
Definition: details.h:283
#define createDailyFileSinkStPtr
A macro alias to improve readability of calls to KDSPDSetup::details::createDailyFileSinkPtr.
Definition: details.h:250
#define createFileSinkStPtr
A macro alias to improve readability of calls to KDSPDSetup::details::createFileSinkPtr.
Definition: details.h:238
#define createStderrSinkMtPtr
Definition: details.h:271
#define createStderrColorSinkMtPtr
Definition: details.h:286
#define createStdoutSinkStPtr
A macro alias to improve readability of calls to KDSPDSetup::details::createStdoutSinkPtr.
Definition: details.h:262
#define createDailyFileSinkMtPtr
A macro alias to improve readability of calls to KDSPDSetup::details::createDailyFileSinkPtr.
Definition: details.h:256
This namespace contains implementation details and helpers used by functions in KDSPDSetup::setup....
Definition: details.cpp:12
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
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
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
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 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
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 genFromNullOrStdStr(toml::string &&typeStr) -> spdlog::sink_ptr
Return the result of calling KDSPDSetup::details::createStdoutSinkPtr with the correct template argum...
Definition: details.cpp:160