Skip to content

Commit

Permalink
Merge pull request #26 from syyyr/fix-msg-handler-overwrite
Browse files Browse the repository at this point in the history
Move messageHandler out of globalOptions
  • Loading branch information
fvacek authored Jun 25, 2024
2 parents a5e1a72 + 19dc99f commit d38b4d5
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
11 changes: 7 additions & 4 deletions libnecrolog/necrolog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,10 @@
#include <unistd.h>
#endif

namespace {
NecroLog::MessageHandler messageHandler = NecroLog::defaultMessageHandler; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)
}

NecroLog::Options &NecroLog::globalOptions()
{
static Options global_options;
Expand Down Expand Up @@ -101,7 +105,7 @@ bool NecroLog::shouldLog(Level level, const LogContext &context)

NecroLog::MessageHandler NecroLog::setMessageHandler(NecroLog::MessageHandler h)
{
return std::exchange(globalOptions().messageHandler, h);
return std::exchange(messageHandler, h);
}

namespace {
Expand Down Expand Up @@ -327,9 +331,8 @@ NecroLog::NecroLogSharedData::NecroLogSharedData(NecroLog::Level level, const Ne

NecroLog::NecroLogSharedData::~NecroLogSharedData()
{
auto h = globalOptions().messageHandler;
if(h)
h(m_level, m_logContext, m_os.str());
if(messageHandler)
messageHandler(m_level, m_logContext, m_os.str());
}

int NecroLog::moduleNameStart(const char *file_name)
Expand Down
1 change: 0 additions & 1 deletion libnecrolog/necrolog.h
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class NECROLOG_DECL_EXPORT NecroLog
bool logLongFileNames = false;
bool caseInsensitiveTopicMatch = false;
ColorizedOutputMode colorizedOutput = ColorizedOutputMode::IfTty;
MessageHandler messageHandler = defaultMessageHandler;
};

static Options& globalOptions();
Expand Down

0 comments on commit d38b4d5

Please sign in to comment.