KDSPDSetup
Small library to initialize spdlog loggers from a toml configuration file.
Loading...
Searching...
No Matches
KDSPDSetup::setup Namespace Reference

This namespace contains the functions directly called by KDSPDSetup::setupFrom, along with helpers called by those functions. The file which contains the definitions for members of KDSPDSetup::setup, setup.h, is included in the implementation file kdspdsetup.cpp The functions defined in this namespace make use of the members of namespace KDSPDSetup::details, defined in details.h. More...

Functions

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_filename and max_files from the given table, which are needed by the constructors of these types of sinks. Based on the type specified in typeStr, either KDSPDSetup::details::genFromRotateStr or KDSPDSetup::details::genFromDailyStr are invoked with these values. This function is called by KDSPDSetup::setup::handleTruncatableSink when typeStr specifies a rotating or daily file sink.
 
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. Further narrows down the type after determining value of truncate and calls either KDSPDSetup::details::genFromFileStr or KDSPDSetup::setup::handleMultipleFileSink. This function is invoked by KDSPDSetup::setup::setupSink.
 
void setupSink (toml::table &&sinkTable)
 Given a table representation of a sink, creates an spdlog::sink_ptr, and evaluates the type of sink specified in the table. Based on the type, calls additional functions to parse type-specific fields and construct the sink with the correct constructor. Adds the resultant sink to KDSPDSetup::details::SPDMaps::mSinkMap, which is used to obtain sink pointers by name when constructing loggers.
 
void setupSinks (toml::value const &data)
 Using data read from a toml file, extract vector of tables representing every sink specified. Call KDSPDSetup::setup::setupSink on each table in the vector to create an spdlog sink for each. These sinks are put into KDSPDSetup::details::SPDMaps::mSinkMap, which is later indexed when constructing loggers to obtain the named sink pointers.
 
void setupPatterns (toml::value const &data)
 Using data read from a toml file, search for the key global_pattern. If it exists, use its string value as the global pattern for spdlog by passing it to spdlog::set_pattern. Search again for a table called pattern. If found, extract vector of tables representing every pattern specified. For each table in the vector, add the name and pattern format string specified to KDSPDSetup::details::SPDMaps::mPatternMap. This map is used when constructing loggers that will use non-global patterns to format messages, allowing indexing of format strings by name.
 
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 specified queue size and thread count fields by calling spdlog::init_thread_pool. Search again for a table thread_pool. If found, extract vector of tables representing every threadpool specified. For each table in the vector, add the name, queue size, and thread count specified to KDSPDSetup::details::SPDMaps::mThreadPoolMap. This map is used to index threadpool information by name when constructing asynchronous loggers.
 
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, a thread pool pointer, and an overflow policy. The thread pool pointer is constructed with values obtained by indexing KDSPDSetup::details::SPDMaps::mThreadPoolMap by the specified thread_pool name in loggerTable. If not specified, the thread pool is set to spdlog::thread_pool(). The overflow policy is searched for in loggerTable, and defaults to "block" if not found. The function also sets a formatting pattern if pattern is nonempty before registering the logger by calling spdlog::register_logger.
 
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, sets the logger's pattern if nonempty and level if specified in loggerTable, and registers the logger by calling spdlog::register_logger on the shared_ptr.
 
void setupLogger (toml::table const &loggerTable)
 Given a table representation of a logger whose name is not already registered with spdlog, evaluates whether the logger's specified type is asynchronous or not, and calls either KDSPDSetup::setup::registerAsynchronousLogger or KDSPDSetup::setup::registerSynchronousLogger accordingly. These functions are called with loggerTable, the specified name, a list of sink pointers, and the specified pattern, if found. The list of sink pointers is obtained by indexing KDSPDSetup::details::SPDMaps::mSinkMap with names specified in a list in the loggerTable.
 
void setupLoggers (toml::value const &data)
 Using data read from a toml file, extract vector of tables representing every logger specified. Call KDSPDSetup::setup::setupLogger on each table in the vector to create and register an spdlog logger for each. The loggers should then be available through spdlog::get.
 

Detailed Description

This namespace contains the functions directly called by KDSPDSetup::setupFrom, along with helpers called by those functions. The file which contains the definitions for members of KDSPDSetup::setup, setup.h, is included in the implementation file kdspdsetup.cpp The functions defined in this namespace make use of the members of namespace KDSPDSetup::details, defined in details.h.

Function Documentation

◆ handleMultipleFileSink()

void KDSPDSetup::setup::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_filename and max_files from the given table, which are needed by the constructors of these types of sinks. Based on the type specified in typeStr, either KDSPDSetup::details::genFromRotateStr or KDSPDSetup::details::genFromDailyStr are invoked with these values. This function is called by KDSPDSetup::setup::handleTruncatableSink when typeStr specifies a rotating or daily file sink.

Parameters
typeStrString of the sink's type, which is checked against string vectors in KDSPDSetup::details
sinkTabletoml11 table representation for a particular sink specified in a configuration file.
sinkPtrAn uninitialized sink pointer, which will be passed to functions that construct a sink. This pointer is eventually put into KDSPDSetup::details::SPDMaps::mSinkMap at the bottom of KDSPDSetup::setup::setupSink.
truncateThe value for truncate obtained in caller KDSPDSetup::setup::handleTruncatableSink.

Definition at line 18 of file setup.cpp.

19{
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 };
22
23 if (details::inTypelist(typeStr, details::rotateStrs)) {
24 sinkPtr = details::genFromRotateStr(std::move(typeStr), std::move(sinkTable), std::move(baseFilename), maxFiles);
25 }
26
27 else if (details::inTypelist(typeStr, details::dailyStrs)) {
28 sinkPtr = details::genFromDailyStr(std::move(typeStr), std::move(sinkTable), truncate, std::move(baseFilename), maxFiles);
29 }
30}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ handleTruncatableSink()

void KDSPDSetup::setup::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. Further narrows down the type after determining value of truncate and calls either KDSPDSetup::details::genFromFileStr or KDSPDSetup::setup::handleMultipleFileSink. This function is invoked by KDSPDSetup::setup::setupSink.

Parameters
typeStrString of the sink's type, which is checked against string vectors in KDSPDSetup::details
sinkTabletoml11 table representation for a particular sink specified in a configuration file.
sinkPtrAn uninitialized sink pointer, which will be passed to functions that construct a sink. This pointer is eventually put into KDSPDSetup::details::SPDMaps::mSinkMap at the bottom of KDSPDSetup::setup::setupSink.

Definition at line 32 of file setup.cpp.

33{
34 auto const truncate = (sinkTable.contains("truncate")) ? sinkTable.at("truncate").as_boolean() : false;
35
36 if (details::inTypelist(typeStr, details::fileStrs)) {
37 sinkPtr = details::genFromFileStr(std::move(typeStr), std::move(sinkTable), truncate);
38 } else if (details::inTypelist(typeStr, details::rotateStrs) || details::inTypelist(typeStr, details::dailyStrs)) {
39 handleMultipleFileSink(std::move(typeStr), std::move(sinkTable), sinkPtr, truncate);
40 }
41}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ registerAsynchronousLogger()

void KDSPDSetup::setup::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, a thread pool pointer, and an overflow policy. The thread pool pointer is constructed with values obtained by indexing KDSPDSetup::details::SPDMaps::mThreadPoolMap by the specified thread_pool name in loggerTable. If not specified, the thread pool is set to spdlog::thread_pool(). The overflow policy is searched for in loggerTable, and defaults to "block" if not found. The function also sets a formatting pattern if pattern is nonempty before registering the logger by calling spdlog::register_logger.

Parameters
loggerTabletoml11 table representation for a particular logger specified in a configuration file.
nameA string with the name of the logger. This name is used to construct the logger and access it with spdlog::get.
sinkListA vector of spdlog::sink_ptr objects to use as the sinks for the logger.
patternThe format string for messages the logger will output. If empty, the logger uses the global pattern set up earlier in KDSPDSetup::setup::setupPatterns, or spdlog's default pattern if the global pattern was not created.

Definition at line 150 of file setup.cpp.

152{
153 auto const threadPool = (loggerTable.contains("thread_pool")) ? loggerTable.at("thread_pool").as_string() : "";
154
155 static std::shared_ptr<spdlog::details::thread_pool> threadPoolPtr;
156
157 if (threadPool != "") {
158 if (!details::SPDMaps::threadPoolMap().contains(threadPool))
159 throw std::out_of_range("KDSPDSetup: threadpool "s + threadPool.str + " not found"s);
160
161 auto const threadPoolPair = details::SPDMaps::threadPoolMap().at(threadPool);
162 auto const queueSize = threadPoolPair.first;
163 auto const numThreads = threadPoolPair.second;
164 threadPoolPtr = std::make_shared<spdlog::details::thread_pool>(queueSize, numThreads);
165 } else {
166 threadPoolPtr = spdlog::thread_pool();
167 }
168
169 auto const overflowPolicy =
170 (loggerTable.contains("overflow_policy")) ? loggerTable.at("overflow_policy").as_string() : "block";
171
172 if (!details::overflowMap.contains(overflowPolicy))
173 throw std::out_of_range("KDSPDSetup: overflow policy "s + overflowPolicy.str + " not found"s);
174
175 auto logger = std::make_shared<spdlog::async_logger>(name, sinkList.begin(), sinkList.end(), std::move(threadPoolPtr),
176 details::overflowMap.at(overflowPolicy));
177
178 if (pattern != "") {
179 logger->set_pattern(pattern);
180 }
181
182 spdlog::register_logger(logger);
183}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ registerSynchronousLogger()

void KDSPDSetup::setup::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, sets the logger's pattern if nonempty and level if specified in loggerTable, and registers the logger by calling spdlog::register_logger on the shared_ptr.

Parameters
loggerTabletoml11 table representation for a particular logger specified in a configuration file.
nameA string with the name of the logger. This name is used to construct the logger and access it with spdlog::get.
sinkListA vector of spdlog::sink_ptr objects to use as the sinks for the logger.
patternThe format string for messages the logger will output. If empty, the logger uses the global pattern set up earlier in KDSPDSetup::setup::setupPatterns, or spdlog's default pattern if the global pattern was not created.

Definition at line 185 of file setup.cpp.

187{
188 auto logger = std::make_shared<spdlog::logger>(name, sinkList.cbegin(), sinkList.cend());
189 if (pattern != "") {
190 logger->set_pattern(static_cast<std::string>(pattern));
191 }
192
193 if (loggerTable.contains("level")) {
194 auto const level = loggerTable.at("level").as_string();
195
196 if (!details::levelMap.contains(level))
197 throw std::out_of_range("KDSPDSetup: level "s + level.str + " not found"s);
198
199 logger->set_level(details::levelMap.at(level));
200 }
201
202 spdlog::register_logger(logger);
203}
Here is the caller graph for this function:

◆ setupLogger()

void KDSPDSetup::setup::setupLogger ( toml::table const &  loggerTable)

Given a table representation of a logger whose name is not already registered with spdlog, evaluates whether the logger's specified type is asynchronous or not, and calls either KDSPDSetup::setup::registerAsynchronousLogger or KDSPDSetup::setup::registerSynchronousLogger accordingly. These functions are called with loggerTable, the specified name, a list of sink pointers, and the specified pattern, if found. The list of sink pointers is obtained by indexing KDSPDSetup::details::SPDMaps::mSinkMap with names specified in a list in the loggerTable.

Parameters
loggerTabletoml11 table representation for a particular logger specified in a configuration file. Passed in by KDSPDSetup::setup::setupLoggers.

Definition at line 205 of file setup.cpp.

206{
207 auto const name = loggerTable.at("name").as_string();
208 if (spdlog::get(name) != nullptr) {
209 return;
210 }
211
212 auto const sinks = loggerTable.at("sinks").as_array();
213
214 auto sinkList = std::vector<spdlog::sink_ptr>{};
215 for (auto &&sink : sinks) {
216 if (details::SPDMaps::sinkMap().contains(sink.as_string())) {
217 sinkList.emplace_back(details::SPDMaps::sinkMap().at(sink.as_string()));
218 } else {
219 // ignore sink instead of throwing, but notify that this happens
220 std::cerr << "KDSPDSetup: setting up logger " << name
221 << " - skipped sink " << sink.as_string().str << " as it was not found";
222 }
223 }
224
225 std::string pattern = "";
226
227 if (loggerTable.contains("pattern")) {
228 if (!details::SPDMaps::patternMap().contains(loggerTable.at("pattern").as_string()))
229 throw std::out_of_range("KDSPDSetup: pattern "s + loggerTable.at("pattern").as_string().str + " not found"s);
230
231 pattern = details::SPDMaps::patternMap().at(loggerTable.at("pattern").as_string());
232 }
233
234 auto const type = (loggerTable.contains("type")) ? loggerTable.at("type").as_string() : "";
235
236 if (type == "async") {
237 registerAsynchronousLogger(loggerTable, name, sinkList, pattern);
238 } else {
239 registerSynchronousLogger(loggerTable, name, sinkList, pattern);
240 }
241}
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,...
Definition setup.cpp:185
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...
Definition setup.cpp:150
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setupLoggers()

void KDSPDSetup::setup::setupLoggers ( toml::value const &  data)

Using data read from a toml file, extract vector of tables representing every logger specified. Call KDSPDSetup::setup::setupLogger on each table in the vector to create and register an spdlog logger for each. The loggers should then be available through spdlog::get.

Parameters
datatoml11 representation of the configuration file.

Definition at line 243 of file setup.cpp.

244{
245 auto const loggers = toml::find<std::vector<toml::table>>(data, "logger");
246 for (auto &&loggerTable : loggers) {
247 setupLogger(loggerTable);
248 }
249}
void setupLogger(toml::table const &loggerTable)
Given a table representation of a logger whose name is not already registered with spdlog,...
Definition setup.cpp:205
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setupPatterns()

void KDSPDSetup::setup::setupPatterns ( toml::value const &  data)

Using data read from a toml file, search for the key global_pattern. If it exists, use its string value as the global pattern for spdlog by passing it to spdlog::set_pattern. Search again for a table called pattern. If found, extract vector of tables representing every pattern specified. For each table in the vector, add the name and pattern format string specified to KDSPDSetup::details::SPDMaps::mPatternMap. This map is used when constructing loggers that will use non-global patterns to format messages, allowing indexing of format strings by name.

Parameters
datatoml11 representation of the configuration file.

Definition at line 108 of file setup.cpp.

109{
110 if (data.contains("global_pattern")) {
111 auto const globalPattern = toml::find<toml::string>(data, "global_pattern");
112 spdlog::set_pattern(globalPattern);
113 }
114
115 if (data.contains("pattern")) {
116 auto const patterns = toml::find<std::vector<toml::table>>(data, "pattern");
117
118 for (auto &&patternTable : patterns) {
119 auto const name = patternTable.at("name").as_string();
120 auto const value = patternTable.at("value").as_string();
121
122 details::SPDMaps::emplacePatternMap(std::make_pair(name, value));
123 }
124 }
125}
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setupSink()

void KDSPDSetup::setup::setupSink ( toml::table &&  sinkTable)

Given a table representation of a sink, creates an spdlog::sink_ptr, and evaluates the type of sink specified in the table. Based on the type, calls additional functions to parse type-specific fields and construct the sink with the correct constructor. Adds the resultant sink to KDSPDSetup::details::SPDMaps::mSinkMap, which is used to obtain sink pointers by name when constructing loggers.

Parameters
sinkTabletoml11 table representation for a particular sink specified in a configuration file. Passed in by KDSPDSetup::setup::setupSinks.

Definition at line 43 of file setup.cpp.

44{
45 spdlog::sink_ptr sinkPtr;
46
47 auto const name = sinkTable.at("name").as_string();
48 auto typeStr = sinkTable.at("type").as_string();
49
50 bool ok = false;
51 for (const auto &typeList : { details::fileStrs, details::rotateStrs, details::dailyStrs, details::nullStrs, details::stdStrs, details::linuxStrs, details::winStrs }) {
52 if (details::inTypelist(typeStr, typeList)) {
53 ok = true;
54 break;
55 }
56 }
57
58 if (!ok)
59 throw std::out_of_range("KDSPDSetup: sink type "s + typeStr.str + " is not valid"s);
60
61 toml::string level = "";
62 if (sinkTable.contains("level")) {
63 level = sinkTable.at("level").as_string();
64 }
65
66 if (details::inTypelist(typeStr, details::fileStrs) || details::inTypelist(typeStr, details::rotateStrs) ||
67 details::inTypelist(typeStr, details::dailyStrs)) {
68 handleTruncatableSink(std::move(typeStr), std::move(sinkTable), sinkPtr);
69 }
70
71 else if (details::inTypelist(typeStr, details::nullStrs) || details::inTypelist(typeStr, details::stdStrs)) {
72 sinkPtr = details::genFromNullOrStdStr(std::move(typeStr));
73 }
74
75 else if (details::inTypelist(typeStr, details::linuxStrs)) {
76#ifdef __linux__
77 sinkPtr = details::genFromLinuxStr(std::move(typeStr), std::move(sinkTable));
78#else
79 return;
80#endif
81 } else if (details::inTypelist(typeStr, details::winStrs)) {
82#ifdef _WIN32
83 sinkPtr = details::genFromWinStr(std::move(typeStr));
84#else
85 return;
86#endif
87 }
88
89 if (level != "") {
90 if (!details::levelMap.contains(level))
91 throw std::out_of_range("KDSPDSetup: level "s + level.str + " not found"s);
92
93 sinkPtr->set_level(details::levelMap.at(level));
94 }
95
96 details::SPDMaps::emplaceSinkMap(std::make_pair(name, sinkPtr));
97}
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...
Definition setup.cpp:32
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setupSinks()

void KDSPDSetup::setup::setupSinks ( toml::value const &  data)

Using data read from a toml file, extract vector of tables representing every sink specified. Call KDSPDSetup::setup::setupSink on each table in the vector to create an spdlog sink for each. These sinks are put into KDSPDSetup::details::SPDMaps::mSinkMap, which is later indexed when constructing loggers to obtain the named sink pointers.

Parameters
datatoml11 representation of the configuration file.

Definition at line 99 of file setup.cpp.

100{
101 auto sinks = toml::find<std::vector<toml::table>>(data, "sink");
102
103 for (auto &&sinkTable : sinks) {
104 setupSink(std::move(sinkTable));
105 }
106}
void setupSink(toml::table &&sinkTable)
Given a table representation of a sink, creates an spdlog::sink_ptr, and evaluates the type of sink s...
Definition setup.cpp:43
Here is the call graph for this function:
Here is the caller graph for this function:

◆ setupThreadPools()

void KDSPDSetup::setup::setupThreadPools ( toml::value const &  data)

Using data read from a toml file, search for a table global_thread_pool, and initialize it with the specified queue size and thread count fields by calling spdlog::init_thread_pool. Search again for a table thread_pool. If found, extract vector of tables representing every threadpool specified. For each table in the vector, add the name, queue size, and thread count specified to KDSPDSetup::details::SPDMaps::mThreadPoolMap. This map is used to index threadpool information by name when constructing asynchronous loggers.

Parameters
datatoml11 representation of the configuration file.

Definition at line 127 of file setup.cpp.

128{
129 if (data.contains("global_thread_pool")) {
130 auto const globalThreadPool = toml::find(data, "global_thread_pool");
131
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);
135 }
136
137 if (data.contains("thread_pool")) {
138 auto const threadPools = toml::find<std::vector<toml::table>>(data, "thread_pool");
139
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());
144
145 details::SPDMaps::emplaceThreadPoolMap(std::make_pair(name, std::make_pair(queueSize, numThreads)));
146 }
147 }
148}
Here is the call graph for this function:
Here is the caller graph for this function: