IRSOL
C++ code implementing socket server for interacting with Baumer camera.
irsol::internal::NamedLoggerRegistry Class Reference

Manages a registry of named loggers. More...

#include <logging.hpp>

Static Public Member Functions

static spdlog::logger * getLogger (const std::string &name)
 Retrieves a logger by name from the registry.
 

Static Private Attributes

static std::unordered_map< std::string, LoggerInfom_loggers
 Internal logger registry.
 

Detailed Description

Manages a registry of named loggers.

Provides access to named logger instances and stores metadata for reuse and caching.

Definition at line 116 of file logging.hpp.

Member Function Documentation

◆ getLogger()

spdlog::logger * irsol::internal::NamedLoggerRegistry::getLogger ( const std::string &  name)
static

Retrieves a logger by name from the registry.

If the logger does not exist yet, it is created and registered.

Parameters
nameThe name of the logger.
Returns
Pointer to the associated spdlog logger.

Definition at line 12 of file logging.cpp.

13{
14 spdlog::logger* result;
15 auto it = m_loggers.find(name);
16 if(it != m_loggers.end()) {
17 result = it->second.logger.get();
18 it->second.lastRetrieved = irsol::types::clock_t::now();
19 } else {
20 auto newLogger = spdlog::default_logger()->clone(name);
21
22#ifdef DEBUG
23 // Extract the current logFilePath from the default logger
24 auto& all_sinks = newLogger->sinks();
25 // Find a logger of type rotating_file_sink_mt and determine the logFilePath
26 // for the named logger.
27 bool fileSinkFound = false;
28 std::string newLogFilePath;
29 for(auto& existing_sink : all_sinks) {
30 if(
31 spdlog::sinks::rotating_file_sink_mt* rotating_sink =
32 dynamic_cast<spdlog::sinks::rotating_file_sink_mt*>(existing_sink.get())) {
33 std::string logFilePath = rotating_sink->filename();
34 // strip the suffix from the logFilePath
35 size_t pos = logFilePath.rfind('.');
36 if(pos != std::string::npos) {
37 logFilePath = logFilePath.substr(0, pos);
38 }
39 newLogFilePath = logFilePath + "_" + name + ".log";
40 fileSinkFound = true;
41 break;
42 }
43 }
44 if(!fileSinkFound) {
45 newLogFilePath = name + ".log";
46 }
47
48 // Also create a new named sink for debug mode
49 const auto maxFileSize = 1024 * 1024 * 5; // 5 MB
50 const auto maxFiles = 24; // Keep 24 rotated files
51 auto fileSink = std::make_shared<spdlog::sinks::rotating_file_sink_mt>(
52 newLogFilePath, maxFileSize, maxFiles, false);
53
54 // Add the dedicated file sink to the named logger
55 newLogger->sinks().push_back(fileSink);
56#endif
57 LoggerInfo info = {newLogger, irsol::types::clock_t::now()};
58 m_loggers[name] = info;
59 result = newLogger.get();
60 }
61
62 // If there's more than 256 loggers, delete the oldest one
63 if(m_loggers.size() > 256) {
64 IRSOL_LOG_INFO("Automatic deletion of old named loggers.");
65 auto oldestLogger =
66 std::min_element(m_loggers.begin(), m_loggers.end(), [](const auto& a, const auto& b) {
67 return a.second.lastRetrieved < b.second.lastRetrieved;
68 });
69 m_loggers.erase(oldestLogger);
70 }
71 return result;
72}
static std::unordered_map< std::string, LoggerInfo > m_loggers
Internal logger registry.
Definition logging.hpp:130
#define IRSOL_LOG_INFO(...)
Logs an info-level message using the default logger.
Definition logging.hpp:92
auto result

Member Data Documentation

◆ m_loggers

std::unordered_map< std::string, LoggerInfo > irsol::internal::NamedLoggerRegistry::m_loggers
staticprivate

Internal logger registry.

Definition at line 130 of file logging.hpp.


The documentation for this class was generated from the following files: