aboutsummaryrefslogtreecommitdiff
path: root/src/zencore/logging.cpp
diff options
context:
space:
mode:
authorStefan Boberg <[email protected]>2025-09-29 10:07:26 +0200
committerGitHub Enterprise <[email protected]>2025-09-29 10:07:26 +0200
commit8bf93f864168eba632d41f58e844dad2ff286b3f (patch)
treef9081167f1b9ab8e86a51e954077b4bd299f8038 /src/zencore/logging.cpp
parentremove spurious cpr.h includes (#520) (diff)
downloadzen-8bf93f864168eba632d41f58e844dad2ff286b3f.tar.xz
zen-8bf93f864168eba632d41f58e844dad2ff286b3f.zip
fixed race condition in zen::logging::Get (#519)
if two requests for the same logger came in from different threads they could end up creating the same logger twice which causes an exception on registration
Diffstat (limited to 'src/zencore/logging.cpp')
-rw-r--r--src/zencore/logging.cpp15
1 files changed, 12 insertions, 3 deletions
diff --git a/src/zencore/logging.cpp b/src/zencore/logging.cpp
index 685c79d82..a6697c443 100644
--- a/src/zencore/logging.cpp
+++ b/src/zencore/logging.cpp
@@ -346,6 +346,8 @@ SetErrorLog(std::string_view NewErrorLoggerId)
}
}
+RwLock g_LoggerMutex;
+
LoggerRef
Get(std::string_view Name)
{
@@ -355,9 +357,16 @@ Get(std::string_view Name)
if (!Logger)
{
- Logger = Default().SpdLogger->clone(std::string(Name));
- spdlog::apply_logger_env_levels(Logger);
- spdlog::register_logger(Logger);
+ g_LoggerMutex.WithExclusiveLock([&] {
+ Logger = spdlog::get(std::string(Name));
+
+ if (!Logger)
+ {
+ Logger = Default().SpdLogger->clone(std::string(Name));
+ spdlog::apply_logger_env_levels(Logger);
+ spdlog::register_logger(Logger);
+ }
+ });
}
return *Logger;