From f720cfa824f1be863349e7016080f8fb1c3c76c2 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 22 Jul 2021 17:37:37 +0200 Subject: [PATCH 1/4] test: verify number of categories returned by logging RPC --- test/functional/rpc_misc.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/test/functional/rpc_misc.py b/test/functional/rpc_misc.py index 52c8fa883de..9ad7827c8a7 100755 --- a/test/functional/rpc_misc.py +++ b/test/functional/rpc_misc.py @@ -54,7 +54,12 @@ class RpcMiscTest(BitcoinTestFramework): assert_raises_rpc_error(-8, "unknown mode foobar", node.getmemoryinfo, mode="foobar") - self.log.info("test logging") + self.log.info("test logging rpc") + + # Test logging RPC returns the expected number of logging categories. + assert_equal(len(node.logging()), 24) + + # Test toggling a logging category on/off/on with the logging RPC. assert_equal(node.logging()['qt'], True) node.logging(exclude=['qt']) assert_equal(node.logging()['qt'], False) From 7c57297319bc386afaf06528778384fe58576ef9 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 22 Jul 2021 16:35:22 +0200 Subject: [PATCH 2/4] log: sort LogCategoriesList and LogCategoriesString alphabetically --- src/logging.cpp | 9 ++++++++- src/logging.h | 4 ++-- 2 files changed, 10 insertions(+), 3 deletions(-) diff --git a/src/logging.cpp b/src/logging.cpp index e5187fd5962..fcb46debf7a 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -8,6 +8,8 @@ #include #include +#include +#include #include const char * const DEFAULT_DEBUGLOGFILE = "debug.log"; @@ -179,8 +181,13 @@ bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str) std::vector BCLog::Logger::LogCategoriesList() const { + // Sort log categories by alphabetical order. + std::array categories; + std::copy(std::begin(LogCategories), std::end(LogCategories), categories.begin()); + std::sort(categories.begin(), categories.end(), [](auto a, auto b) { return a.category < b.category; }); + std::vector ret; - for (const CLogCategoryDesc& category_desc : LogCategories) { + for (const CLogCategoryDesc& category_desc : categories) { // Omit the special cases. if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL) { LogCategory catActive; diff --git a/src/logging.h b/src/logging.h index d04bc992680..38d73863e76 100644 --- a/src/logging.h +++ b/src/logging.h @@ -138,9 +138,9 @@ namespace BCLog { bool DisableCategory(const std::string& str); bool WillLogCategory(LogFlags category) const; - /** Returns a vector of the log categories */ + /** Returns a vector of the log categories in alphabetical order. */ std::vector LogCategoriesList() const; - /** Returns a string with the log categories */ + /** Returns a string with the log categories in alphabetical order. */ std::string LogCategoriesString() const { return Join(LogCategoriesList(), ", ", [&](const LogCategory& i) { return i.category; }); From 17bbff3b88132c0c95b29b59100456b85e26df75 Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 22 Jul 2021 12:01:21 +0200 Subject: [PATCH 3/4] log, refactor: use guard clause in LogCategoriesList() and minor formatting fixups --- src/logging.cpp | 17 +++++++---------- 1 file changed, 7 insertions(+), 10 deletions(-) diff --git a/src/logging.cpp b/src/logging.cpp index fcb46debf7a..b456108b61c 100644 --- a/src/logging.cpp +++ b/src/logging.cpp @@ -126,8 +126,7 @@ bool BCLog::Logger::DefaultShrinkDebugFile() const return m_categories == BCLog::NONE; } -struct CLogCategoryDesc -{ +struct CLogCategoryDesc { BCLog::LogFlags flag; std::string category; }; @@ -188,13 +187,11 @@ std::vector BCLog::Logger::LogCategoriesList() const std::vector ret; for (const CLogCategoryDesc& category_desc : categories) { - // Omit the special cases. - if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL) { - LogCategory catActive; - catActive.category = category_desc.category; - catActive.active = WillLogCategory(category_desc.flag); - ret.push_back(catActive); - } + if (category_desc.flag == BCLog::NONE || category_desc.flag == BCLog::ALL) continue; + LogCategory catActive; + catActive.category = category_desc.category; + catActive.active = WillLogCategory(category_desc.flag); + ret.push_back(catActive); } return ret; } @@ -244,7 +241,7 @@ namespace BCLog { } return ret; } -} +} // namespace BCLog void BCLog::Logger::LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, const int source_line) { From d596dba9877e7ead3fb5426cbe7e608fbcbfe3eb Mon Sep 17 00:00:00 2001 From: Jon Atack Date: Thu, 22 Jul 2021 13:29:39 +0200 Subject: [PATCH 4/4] test: assert logging categories are sorted in rpc and help --- test/functional/rpc_misc.py | 11 ++++++++++- 1 file changed, 10 insertions(+), 1 deletion(-) diff --git a/test/functional/rpc_misc.py b/test/functional/rpc_misc.py index 9ad7827c8a7..563f2ea43e0 100755 --- a/test/functional/rpc_misc.py +++ b/test/functional/rpc_misc.py @@ -54,7 +54,7 @@ class RpcMiscTest(BitcoinTestFramework): assert_raises_rpc_error(-8, "unknown mode foobar", node.getmemoryinfo, mode="foobar") - self.log.info("test logging rpc") + self.log.info("test logging rpc and help") # Test logging RPC returns the expected number of logging categories. assert_equal(len(node.logging()), 24) @@ -66,6 +66,15 @@ class RpcMiscTest(BitcoinTestFramework): node.logging(include=['qt']) assert_equal(node.logging()['qt'], True) + # Test logging RPC returns the logging categories in alphabetical order. + sorted_logging_categories = sorted(node.logging()) + assert_equal(list(node.logging()), sorted_logging_categories) + + # Test logging help returns the logging categories string in alphabetical order. + categories = ', '.join(sorted_logging_categories) + logging_help = self.nodes[0].help('logging') + assert f"valid logging categories are: {categories}" in logging_help + self.log.info("test echoipc (testing spawned process in multiprocess build)") assert_equal(node.echoipc("hello"), "hello")