diff --git a/src/logging.h b/src/logging.h --- a/src/logging.h +++ b/src/logging.h @@ -150,9 +150,9 @@ /** Return true if log accepts specified category */ 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; }); diff --git a/src/logging.cpp b/src/logging.cpp --- a/src/logging.cpp +++ b/src/logging.cpp @@ -10,6 +10,9 @@ #include #include +#include +#include + bool fLogIPs = DEFAULT_LOGIPS; const char *const DEFAULT_DEBUGLOGFILE = "debug.log"; @@ -144,16 +147,23 @@ } 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) { - // 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); + for (const CLogCategoryDesc &category_desc : categories) { + 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; } diff --git a/test/functional/rpc_misc.py b/test/functional/rpc_misc.py --- a/test/functional/rpc_misc.py +++ b/test/functional/rpc_misc.py @@ -56,13 +56,29 @@ assert_raises_rpc_error(-8, "unknown mode foobar", node.getmemoryinfo, mode="foobar") - self.log.info("test logging") + self.log.info("test logging rpc and help") + + # Test logging RPC returns the expected number of logging categories. + assert_equal(len(node.logging()), 25) + + # 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) 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 getindexinfo") # Without any indices running the RPC returns an empty object assert_equal(node.getindexinfo(), {})