From c3b12def3a97a792389d07aa0a7aa7b495fc28f7 Mon Sep 17 00:00:00 2001 From: Stephen Webb Date: Thu, 9 Nov 2023 17:42:14 +1100 Subject: [PATCH] Prevent undefined behaviour during process exit --- src/main/cpp/loglog.cpp | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/src/main/cpp/loglog.cpp b/src/main/cpp/loglog.cpp index 0d847bf58..9d4ce5952 100644 --- a/src/main/cpp/loglog.cpp +++ b/src/main/cpp/loglog.cpp @@ -36,6 +36,11 @@ struct LogLog::LogLogPrivate { debugEnabled(false), quietMode(false){} + ~LogLogPrivate() + { + quietMode = true; + } + bool debugEnabled; /** @@ -63,14 +68,16 @@ LogLog& LogLog::getInstance() void LogLog::setInternalDebugging(bool debugEnabled1) { - std::unique_lock lock(getInstance().m_priv->mutex); + auto p = getInstance().m_priv.get(); + std::unique_lock lock(p->mutex); - getInstance().m_priv->debugEnabled = debugEnabled1; + p->debugEnabled = debugEnabled1; } void LogLog::debug(const LogString& msg) { - if (auto p = getInstance().m_priv.get()) // Not deleted by onexit processing? + auto p = getInstance().m_priv.get(); + if (p && !p->quietMode) // Not deleted by onexit processing? { if (!p->debugEnabled) { @@ -85,7 +92,8 @@ void LogLog::debug(const LogString& msg) void LogLog::debug(const LogString& msg, const std::exception& e) { - if (auto p = getInstance().m_priv.get()) // Not deleted by onexit processing? + auto p = getInstance().m_priv.get(); + if (p && !p->quietMode) // Not deleted by onexit processing? { if (!p->debugEnabled) return; @@ -99,7 +107,8 @@ void LogLog::debug(const LogString& msg, const std::exception& e) void LogLog::error(const LogString& msg) { - if (auto p = getInstance().m_priv.get()) // Not deleted by onexit processing? + auto p = getInstance().m_priv.get(); + if (p && !p->quietMode) // Not deleted by onexit processing? { std::unique_lock lock(p->mutex); @@ -109,7 +118,8 @@ void LogLog::error(const LogString& msg) void LogLog::error(const LogString& msg, const std::exception& e) { - if (auto p = getInstance().m_priv.get()) // Not deleted by onexit processing? + auto p = getInstance().m_priv.get(); + if (p && !p->quietMode) // Not deleted by onexit processing? { std::unique_lock lock(p->mutex); emit(msg); @@ -119,14 +129,16 @@ void LogLog::error(const LogString& msg, const std::exception& e) void LogLog::setQuietMode(bool quietMode1) { - std::unique_lock lock(getInstance().m_priv->mutex); + auto p = getInstance().m_priv.get(); + std::unique_lock lock(p->mutex); - getInstance().m_priv->quietMode = quietMode1; + p->quietMode = quietMode1; } void LogLog::warn(const LogString& msg) { - if (auto p = getInstance().m_priv.get()) // Not deleted by onexit processing? + auto p = getInstance().m_priv.get(); + if (p && !p->quietMode) // Not deleted by onexit processing? { std::unique_lock lock(p->mutex); emit(msg); @@ -135,7 +147,8 @@ void LogLog::warn(const LogString& msg) void LogLog::warn(const LogString& msg, const std::exception& e) { - if (auto p = getInstance().m_priv.get()) // Not deleted by onexit processing? + auto p = getInstance().m_priv.get(); + if (p && !p->quietMode) // Not deleted by onexit processing? { std::unique_lock lock(p->mutex); emit(msg); @@ -145,7 +158,8 @@ void LogLog::warn(const LogString& msg, const std::exception& e) void LogLog::emit(const LogString& msg) { - if (getInstance().m_priv->quietMode) + auto p = getInstance().m_priv.get(); + if (p->quietMode) { return; } @@ -160,7 +174,8 @@ void LogLog::emit(const LogString& msg) void LogLog::emit(const std::exception& ex) { - if (getInstance().m_priv->quietMode) + auto p = getInstance().m_priv.get(); + if (p->quietMode) { return; }