diff --git a/libdevcore/Log.cpp b/libdevcore/Log.cpp index 3dd2b3879..f28a2c6b9 100644 --- a/libdevcore/Log.cpp +++ b/libdevcore/Log.cpp @@ -33,7 +33,29 @@ using namespace dev; // Logging int dev::g_logVerbosity = 5; -map dev::g_logOverride; +mutex x_logOverride; + +/// Map of Log Channel types to bool, false forces the channel to be disabled, true forces it to be enabled. +/// If a channel has no entry, then it will output as long as its verbosity (LogChannel::verbosity) is less than +/// or equal to the currently output verbosity (g_logVerbosity). +static map s_logOverride; + +LogOverrideAux::LogOverrideAux(std::type_info const* _ch, bool _value): + m_ch(_ch) +{ + Guard l(x_logOverride); + m_old = s_logOverride.count(_ch) ? (int)s_logOverride[_ch] : c_null; + s_logOverride[m_ch] = _value; +} + +LogOverrideAux::~LogOverrideAux() +{ + Guard l(x_logOverride); + if (m_old == c_null) + s_logOverride.erase(m_ch); + else + s_logOverride[m_ch] = (bool)m_old; +} #ifdef _WIN32 const char* LogChannel::name() { return EthGray "..."; } @@ -55,8 +77,9 @@ LogOutputStreamBase::LogOutputStreamBase(char const* _id, std::type_info const* m_autospacing(_autospacing), m_verbosity(_v) { - auto it = g_logOverride.find(_info); - if ((it != g_logOverride.end() && it->second == true) || (it == g_logOverride.end() && (int)_v <= g_logVerbosity)) + Guard l(x_logOverride); + auto it = s_logOverride.find(_info); + if ((it != s_logOverride.end() && it->second == true) || (it == s_logOverride.end() && (int)_v <= g_logVerbosity)) { time_t rawTime = std::chrono::system_clock::to_time_t(std::chrono::system_clock::now()); char buf[24]; diff --git a/libdevcore/Log.h b/libdevcore/Log.h index 57d8cd349..ce0db17fe 100644 --- a/libdevcore/Log.h +++ b/libdevcore/Log.h @@ -54,10 +54,24 @@ extern int g_logVerbosity; /// The current method that the logging system uses to output the log messages. Defaults to simpleDebugOut(). extern std::function g_logPost; -/// Map of Log Channel types to bool, false forces the channel to be disabled, true forces it to be enabled. -/// If a channel has no entry, then it will output as long as its verbosity (LogChannel::verbosity) is less than -/// or equal to the currently output verbosity (g_logVerbosity). -extern std::map g_logOverride; +class LogOverrideAux +{ +protected: + LogOverrideAux(std::type_info const* _ch, bool _value); + ~LogOverrideAux(); + +private: + std::type_info const* m_ch; + static const int c_null = -1; + int m_old; +}; + +template +class LogOverride: LogOverrideAux +{ +public: + LogOverride(bool _value): LogOverrideAux(&typeid(Channel), _value) {} +}; /// Temporary changes system's verbosity for specific function. Restores the old verbosity when function returns. /// Not thread-safe, use with caution! diff --git a/libethereum/State.cpp b/libethereum/State.cpp index 09765d9ee..f84ee819f 100644 --- a/libethereum/State.cpp +++ b/libethereum/State.cpp @@ -592,24 +592,6 @@ string State::vmTrace(bytesConstRef _block, BlockChain const& _bc, ImportRequire return ss.str(); } -template -class LogOverride -{ -public: - LogOverride(bool _value): m_old(g_logOverride.count(&typeid(Channel)) ? (int)g_logOverride[&typeid(Channel)] : c_null) { g_logOverride[&typeid(Channel)] = _value; } - ~LogOverride() - { - if (m_old == c_null) - g_logOverride.erase(&typeid(Channel)); - else - g_logOverride[&typeid(Channel)] = (bool)m_old; - } - -private: - static const int c_null = -1; - int m_old; -}; - u256 State::enact(bytesConstRef _block, BlockChain const& _bc, ImportRequirements::value _ir) { // m_currentBlock is assumed to be prepopulated and reset.