Skip to content

Commit

Permalink
Prevent undefined behaviour during process exit
Browse files Browse the repository at this point in the history
  • Loading branch information
stephen-webb committed Nov 9, 2023
1 parent 2045457 commit c3b12de
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions src/main/cpp/loglog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@ struct LogLog::LogLogPrivate {
debugEnabled(false),
quietMode(false){}

~LogLogPrivate()
{
quietMode = true;
}

bool debugEnabled;

/**
Expand Down Expand Up @@ -63,14 +68,16 @@ LogLog& LogLog::getInstance()

void LogLog::setInternalDebugging(bool debugEnabled1)
{
std::unique_lock<std::mutex> lock(getInstance().m_priv->mutex);
auto p = getInstance().m_priv.get();
std::unique_lock<std::mutex> 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)
{
Expand All @@ -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;
Expand All @@ -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<std::mutex> lock(p->mutex);

Expand All @@ -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<std::mutex> lock(p->mutex);
emit(msg);
Expand All @@ -119,14 +129,16 @@ void LogLog::error(const LogString& msg, const std::exception& e)

void LogLog::setQuietMode(bool quietMode1)
{
std::unique_lock<std::mutex> lock(getInstance().m_priv->mutex);
auto p = getInstance().m_priv.get();
std::unique_lock<std::mutex> 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<std::mutex> lock(p->mutex);
emit(msg);
Expand All @@ -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<std::mutex> lock(p->mutex);
emit(msg);
Expand All @@ -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;
}
Expand All @@ -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;
}
Expand Down

0 comments on commit c3b12de

Please sign in to comment.