Skip to content

Commit aa92048

Browse files
author
Rafael Stahl
committed
Logging: fix potential initialization order issue with Logging config setup
1 parent 7ef836f commit aa92048

1 file changed

Lines changed: 16 additions & 9 deletions

File tree

src/hacklib/src/Logging.cpp

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
#include <fstream>
88

99

10-
static hl::LogConfig g_cfg;
10+
hl::LogConfig& getGlobalConfig()
11+
{
12+
static hl::LogConfig cfg;
13+
return cfg;
14+
}
1115

1216

1317
class FormatStr
@@ -65,32 +69,35 @@ static std::string GetCodeStr(const char* file, const char* func, int line)
6569

6670
static void LogString(const std::string& str, bool raw)
6771
{
72+
const auto& cfg = getGlobalConfig();
73+
6874
std::string logStr = str;
69-
if (!raw && g_cfg.logTime)
75+
if (!raw && cfg.logTime)
7076
{
7177
logStr = GetTimeStr() + " " + str;
7278
}
7379

74-
if (g_cfg.logFunc)
80+
if (cfg.logFunc)
7581
{
76-
g_cfg.logFunc(logStr);
82+
cfg.logFunc(logStr);
7783
}
7884

79-
if (g_cfg.logToFile)
85+
if (cfg.logToFile)
8086
{
81-
std::ofstream logfile(g_cfg.fileName, std::ios::out | std::ios::app);
87+
std::ofstream logfile(cfg.fileName, std::ios::out | std::ios::app);
8288
logfile << logStr;
8389
}
8490
}
8591

8692

8793
void hl::ConfigLog(const LogConfig& config)
8894
{
89-
g_cfg = config;
95+
auto& cfg = getGlobalConfig();
96+
cfg = config;
9097

91-
if (g_cfg.fileName == "")
98+
if (cfg.fileName == "")
9299
{
93-
g_cfg.fileName = hl::GetCurrentModulePath() + "_log.txt";
100+
cfg.fileName = hl::GetCurrentModulePath() + "_log.txt";
94101
}
95102
}
96103

0 commit comments

Comments
 (0)