Skip to content

Commit 449e7c8

Browse files
committed
[Refactor]: Use init counter instead of boolean initialized
1 parent a922a61 commit 449e7c8

1 file changed

Lines changed: 18 additions & 19 deletions

File tree

src/logcoe.cpp

Lines changed: 18 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ namespace
1313
{
1414
class LoggerImpl
1515
{
16-
static bool s_initialized;
16+
static unsigned int s_initCounter;
1717
static LogLevel s_logLevel;
1818
static std::string s_defaultSource;
1919
static std::mutex s_mutex;
@@ -56,7 +56,7 @@ namespace
5656
static void flush();
5757
};
5858

59-
bool LoggerImpl::s_initialized = false;
59+
unsigned int LoggerImpl::s_initCounter = 0;
6060
LogLevel LoggerImpl::s_logLevel = LogLevel::INFO;
6161
std::string LoggerImpl::s_defaultSource = "";
6262
std::mutex LoggerImpl::s_mutex;
@@ -69,7 +69,7 @@ namespace
6969

7070
std::string LoggerImpl::getCurrentTimestamp()
7171
{
72-
if(!s_initialized) return "";
72+
if(s_initCounter == 0) return "";
7373

7474
auto now = std::chrono::system_clock::now();
7575
std::time_t time_t_now = std::chrono::system_clock::to_time_t(now);
@@ -89,7 +89,7 @@ namespace
8989

9090
std::string LoggerImpl::getLogLevelAsString(LogLevel level)
9191
{
92-
if(!s_initialized) return "";
92+
if(s_initCounter == 0) return "";
9393

9494
switch (level)
9595
{
@@ -108,7 +108,7 @@ namespace
108108

109109
void LoggerImpl::writeToOutputs(const std::string &formattedMessage, LogLevel level, bool flush)
110110
{
111-
if (!s_initialized || static_cast<int>(level) < static_cast<int>(s_logLevel))
111+
if (s_initCounter == 0 || static_cast<int>(level) < static_cast<int>(s_logLevel))
112112
return;
113113

114114
if (s_useConsole && s_consoleStream)
@@ -129,7 +129,7 @@ namespace
129129
void LoggerImpl::log(LogLevel level, const std::string &message, const std::string &source, bool flush)
130130
{
131131
std::lock_guard<std::mutex> lock(s_mutex);
132-
if(!s_initialized) return;
132+
if(s_initCounter == 0) return;
133133

134134
std::stringstream formattedMessage;
135135
formattedMessage << "[" << getCurrentTimestamp() << "] ";
@@ -144,10 +144,9 @@ namespace
144144
void LoggerImpl::initialize(LogLevel level, const std::string &defaultSource, bool enableConsole, bool enableFile, const std::string &filename)
145145
{
146146
std::lock_guard<std::mutex> lock(s_mutex);
147-
if(s_initialized)
148-
throw std::runtime_error("[logcoe] logcoe is already initialized, please shutdown before initializing again");
147+
if(s_initCounter++ > 0)
148+
return info("[logcoe] Already initialized, ignoring new configurations");
149149

150-
s_initialized = true;
151150
s_logLevel = level;
152151
s_defaultSource = defaultSource;
153152
s_useConsole = enableConsole;
@@ -188,7 +187,7 @@ namespace
188187
void LoggerImpl::shutdown()
189188
{
190189
std::lock_guard<std::mutex> lock(s_mutex);
191-
if(!s_initialized) return;
190+
if (--s_initCounter > 0) return;
192191

193192
std::string shutdownMessage = "[logcoe] shutting down";
194193
writeToOutputs(shutdownMessage);
@@ -202,21 +201,21 @@ namespace
202201
s_useFile = false;
203202
s_logLevel = LogLevel::NONE;
204203
s_filename = "logcoe.log";
205-
s_initialized = false;
204+
s_initCounter = 0;
206205
}
207206

208207
void LoggerImpl::setLogLevel(LogLevel level)
209208
{
210209
std::lock_guard<std::mutex> lock(s_mutex);
211-
if(!s_initialized) return;
210+
if(s_initCounter == 0) return;
212211

213212
s_logLevel = level;
214213
}
215214

216215
void LoggerImpl::setConsoleOutput(std::ostream &stream)
217216
{
218217
std::lock_guard<std::mutex> lock(s_mutex);
219-
if(!s_initialized) return;
218+
if(s_initCounter == 0) return;
220219

221220
if (s_useConsole && s_consoleStream)
222221
s_consoleStream->flush();
@@ -227,7 +226,7 @@ namespace
227226
bool LoggerImpl::setFileOutput(const std::string &filename)
228227
{
229228
std::lock_guard<std::mutex> lock(s_mutex);
230-
if(!s_initialized) return false;
229+
if(s_initCounter == 0) return false;
231230

232231
if (s_fileStream.is_open())
233232
{
@@ -252,7 +251,7 @@ namespace
252251
void LoggerImpl::disableConsoleOutput()
253252
{
254253
std::lock_guard<std::mutex> lock(s_mutex);
255-
if(!s_initialized) return;
254+
if(s_initCounter == 0) return;
256255

257256
if (s_useConsole && s_consoleStream)
258257
s_consoleStream->flush();
@@ -263,7 +262,7 @@ namespace
263262
void LoggerImpl::disableFileOutput()
264263
{
265264
std::lock_guard<std::mutex> lock(s_mutex);
266-
if(!s_initialized) return;
265+
if(s_initCounter == 0) return;
267266

268267
if (s_fileStream.is_open())
269268
{
@@ -279,7 +278,7 @@ namespace
279278
void LoggerImpl::setTimeFormat(const std::string &format)
280279
{
281280
std::lock_guard<std::mutex> lock(s_mutex);
282-
if(!s_initialized) return;
281+
if(s_initCounter == 0) return;
283282

284283
try
285284
{
@@ -314,7 +313,7 @@ namespace
314313
{
315314
std::lock_guard<std::mutex> lock(s_mutex);
316315

317-
return s_initialized;
316+
return s_initCounter > 0;
318317
}
319318

320319
LogLevel LoggerImpl::getLogLevel()
@@ -346,7 +345,7 @@ namespace
346345

347346
void LoggerImpl::flush()
348347
{
349-
if(!s_initialized) return;
348+
if(s_initCounter == 0) return;
350349
if (s_useConsole && s_consoleStream)
351350
s_consoleStream->flush();
352351
if (s_useFile && s_fileStream.is_open())

0 commit comments

Comments
 (0)