diff --git a/src/logging.cpp b/src/logging.cpp --- a/src/logging.cpp +++ b/src/logging.cpp @@ -207,6 +207,14 @@ } bool BCLog::Logger::WillLogCategory(LogFlags category) const { + // ALL is not meant to be used as a logging category, but only as a mask + // representing all categories. + if (category == BCLog::NONE || category == BCLog::ALL) { + LogPrintf("Error trying to log using a category mask instead of an " + "explicit category."); + return false; + } + return (logCategories.load(std::memory_order_relaxed) & category) != 0; } diff --git a/src/test/logging_tests.cpp b/src/test/logging_tests.cpp new file mode 100644 --- /dev/null +++ b/src/test/logging_tests.cpp @@ -0,0 +1,24 @@ +// Copyright (c) 2018 The Bitcoin developers +// Distributed under the MIT software license, see the accompanying +// file COPYING or http://www.opensource.org/licenses/mit-license.php. + +#include "logging.h" + +#include + +BOOST_FIXTURE_TEST_SUITE(logging_tests, BasicTestingSetup) + +BOOST_AUTO_TEST_CASE(logging_categories) { + BCLog::Logger logger = GetLogger(); + + // Should return true for explicit categories + for (logFlag = 1; logFlag <= 1 << 20; logFlag++) { + BOOST_CHECK(logger.WillLogCategory(logFlag)); + } + + // Category masks should not log + BOOST_CHECK(!logger.WillLogCategory(BCLog::NONE)); + BOOST_CHECK(!logger.WillLogCategory(BCLog::ALL)); +} + +BOOST_AUTO_TEST_SUITE_END()