dash/src/batchedlogger.cpp
MarcoFalke 15f14806a8
Merge #15266: memory: Construct globals on first use
77777c5624 log: Construct global logger on first use (MarcoFalke)

Pull request description:

  The (de)initialization order is not well defined in C++, so generally it is not safe to use globals as the (de/con)structor of one global could use the (de/con)structor of another global before/after it has been (con/de)structed.

  Specifically this fixes:
  * `g_logger` might not be initialized on the first use, so do that. (Fixes #15111)

Tree-SHA512: eb9c22f4baf31ebc5b0b9ee6a51d1354bae1f0df186cc0ce818b4483c7b5a7f90268d2b549ee96b4c57f8ef36ab239dc6497f74f3e2ef166038f7437c368297d
2021-09-16 07:41:45 +05:30

26 lines
624 B
C++

// Copyright (c) 2018-2020 The Dash Core developers
// Distributed under the MIT/X11 software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <batchedlogger.h>
#include <util/system.h>
CBatchedLogger::CBatchedLogger(BCLog::LogFlags _category, const std::string& _header) :
accept(LogAcceptCategory(_category)), header(_header)
{
}
CBatchedLogger::~CBatchedLogger()
{
Flush();
}
void CBatchedLogger::Flush()
{
if (!accept || msg.empty()) {
return;
}
LogInstance().LogPrintStr(strprintf("%s:\n%s", header, msg));
msg.clear();
}