DotNetDupe 4.0.6
C++17/20 Implementation of the .NET Base Class Library (BCL)
Loading...
Searching...
No Matches
LogManager.cpp
Go to the documentation of this file.
1#include "pch.h"
8#include <mutex>
9#include <unordered_map>
10#include <string>
11
12namespace DotNetDupe {
13 namespace Extensions {
14 namespace Logging {
15
16 namespace {
17 static std::mutex s_mutex;
18 static LoggerConfiguration s_globalConfig;
19 static DotNetDupe::System::SmartPointer<LoggerFactory> s_pFactory = nullptr;
20 static DotNetDupe::System::SmartPointer<ConsoleLoggerProvider> s_pConsoleProvider = nullptr;
21 static DotNetDupe::System::SmartPointer<FileLoggerProvider> s_pFileProvider = nullptr;
22 static std::unordered_map<std::string, DotNetDupe::System::SmartPointer<ILogger>> s_pLoggerCache;
23 static std::unordered_map<std::string, DotNetDupe::System::SmartPointer<ILogger>> s_pConsoleLoggerCache;
24 static std::unordered_map<std::string, DotNetDupe::System::SmartPointer<ILogger>> s_pFileLoggerCache;
25
26 void EnsureFactoryInitializedLocked() {
28 if (!s_pFactory) {
32 s_pFactory->AddProvider(s_pConsoleProvider);
33 s_pFactory->AddProvider(s_pFileProvider);
34 }
35 }
36 }
37
40 std::lock_guard<std::mutex> lk(s_mutex);
41 std::string sKey = sCategoryName.GetRawString();
42
44 auto it = s_pLoggerCache.find(sKey);
45 if (it != s_pLoggerCache.end()) {
46 return it->second;
47 }
48
50 EnsureFactoryInitializedLocked();
51 auto pLogger = s_pFactory->CreateLogger(sCategoryName);
52 s_pLoggerCache[sKey] = pLogger;
53 return pLogger;
54 }
55
58 std::lock_guard<std::mutex> lk(s_mutex);
59 std::string sKey = sCategoryName.GetRawString();
60
62 auto it = s_pConsoleLoggerCache.find(sKey);
63 if (it != s_pConsoleLoggerCache.end()) {
64 return it->second;
65 }
66
68 EnsureFactoryInitializedLocked();
69 auto pLogger = s_pConsoleProvider->CreateLogger(sCategoryName);
70 s_pConsoleLoggerCache[sKey] = pLogger;
71 return pLogger;
72 }
73
76 std::lock_guard<std::mutex> lk(s_mutex);
77 std::string sKey = sCategoryName.GetRawString();
78
80 auto it = s_pFileLoggerCache.find(sKey);
81 if (it != s_pFileLoggerCache.end()) {
82 return it->second;
83 }
84
86 EnsureFactoryInitializedLocked();
87 auto pLogger = s_pFileProvider->CreateLogger(sCategoryName);
88 s_pFileLoggerCache[sKey] = pLogger;
89 return pLogger;
90 }
91
94 std::lock_guard<std::mutex> lk(s_mutex);
95 s_globalConfig = config;
96 s_pLoggerCache.clear();
97 s_pConsoleLoggerCache.clear();
98 s_pFileLoggerCache.clear();
99
104 s_pFactory->AddProvider(s_pConsoleProvider);
105 s_pFactory->AddProvider(s_pFileProvider);
106 }
107
110 std::lock_guard<std::mutex> lk(s_mutex);
111 return s_globalConfig;
112 }
113
116 if (pProvider.IsNull()) return;
117
119 std::lock_guard<std::mutex> lk(s_mutex);
120 EnsureFactoryInitializedLocked();
121 s_pFactory->AddProvider(pProvider);
122 s_pLoggerCache.clear();
123 }
124
127 std::lock_guard<std::mutex> lk(s_mutex);
128 s_globalConfig = LoggerConfiguration();
129 s_pLoggerCache.clear();
130 s_pConsoleLoggerCache.clear();
131 s_pFileLoggerCache.clear();
132 s_pConsoleProvider = nullptr;
133 s_pFileProvider = nullptr;
134 s_pFactory = nullptr;
135 }
136
137 }
138 }
139}
static DotNetDupe::System::SmartPointer< ILogger > GetConsoleLogger(const DotNetDupe::System::String &sCategoryName)
Creates a dedicated ConsoleLogger instance for the specified category.
static void Reset()
Resets the LogManager state, releasing all cached loggers and providers.
static DotNetDupe::System::SmartPointer< ILogger > GetFileLogger(const DotNetDupe::System::String &sCategoryName)
Creates a dedicated FileLogger instance for the specified category.
static const LoggerConfiguration & GetConfiguration()
Gets the active global LoggerConfiguration.
static void AddProvider(const DotNetDupe::System::SmartPointer< ILoggerProvider > &pProvider)
Adds an ILoggerProvider to the internal global LoggerFactory.
static DotNetDupe::System::SmartPointer< ILoggerOf< T > > GetLogger()
Retrieves a strongly-typed ILoggerOf<T> where the category name is derived from type T.
Definition LogManager.h:29
static void Configure(const LoggerConfiguration &config)
Sets the global LoggerConfiguration applied across created loggers.
A unified smart pointer that supports both unique and shared ownership semantics.
static SmartPointer< T > NewShared()
Creates a Shared SmartPointer, default constructing T.
bool IsNull() const noexcept
Checks if the SmartPointer is null.
Represents text as a sequence of UTF-8 code units with culture-invariant operations.
Definition String.h:74
const char * GetRawString() const
Definition String.cpp:230
Encapsulates logging system configuration options.