Skip to content

Commit 42aea04

Browse files
MarcoFalkePastaPastaPasta
authored andcommitted
Merge bitcoin#22530: log: sort logging categories alphabetically
d596dba test: assert logging categories are sorted in rpc and help (Jon Atack) 17bbff3 log, refactor: use guard clause in LogCategoriesList() (Jon Atack) 7c57297 log: sort LogCategoriesList and LogCategoriesString alphabetically (Jon Atack) f720cfa test: verify number of categories returned by logging RPC (Jon Atack) Pull request description: Sorting the logging categories seems more user-friendly with the number of categories we now have, allowing CLI users to more quickly find a particular category. before ``` $ bitcoin-cli help logging ... The valid logging categories are: net, tor, mempool, http, bench, zmq, walletdb, rpc, estimatefee, addrman, selectcoins, reindex, cmpctblock, rand, prune, proxy, mempoolrej, libevent, coindb, qt, leveldb, validation, i2p, ipc $ bitcoind -h | grep -A8 "debug=<category>" -debug=<category> ... output all debugging information. <category> can be: net, tor, mempool, http, bench, zmq, walletdb, rpc, estimatefee, addrman, selectcoins, reindex, cmpctblock, rand, prune, proxy, mempoolrej, libevent, coindb, qt, leveldb, validation, i2p, ipc. $ bitcoin-cli logging [] '["addrman"]' { "net": false, "tor": true, "mempool": false, "http": false, "bench": false, "zmq": false, "walletdb": false, "rpc": false, "estimatefee": false, "addrman": false, "selectcoins": false, "reindex": false, "cmpctblock": false, "rand": false, "prune": false, "proxy": true, "mempoolrej": false, "libevent": false, "coindb": false, "qt": false, "leveldb": false, "validation": false, "i2p": true, "ipc": false } ``` after ``` $ bitcoin-cli help logging ... The valid logging categories are: addrman, bench, cmpctblock, coindb, estimatefee, http, i2p, ipc, leveldb, libevent, mempool, mempoolrej, net, proxy, prune, qt, rand, reindex, rpc, selectcoins, tor, validation, walletdb, zmq $ bitcoind -h | grep -A8 "debug=<category>" -debug=<category> ... output all debugging information. <category> can be: addrman, bench, cmpctblock, coindb, estimatefee, http, i2p, ipc, leveldb, libevent, mempool, mempoolrej, net, proxy, prune, qt, rand, reindex, rpc, selectcoins, tor, validation, walletdb, zmq. $ bitcoin-cli logging [] '["addrman"]' { "addrman": false, "bench": false, "cmpctblock": false, "coindb": false, "estimatefee": false, "http": false, "i2p": false, "ipc": false, "leveldb": false, "libevent": false, "mempool": false, "mempoolrej": false, "net": false, "proxy": false, "prune": false, "qt": false, "rand": false, "reindex": false, "rpc": false, "selectcoins": false, "tor": false, "validation": false, "walletdb": false, "zmq": false } ``` ACKs for top commit: theStack: re-ACK d596dba Tree-SHA512: d546257f562b0a288d1b19a028f1a510aaf21bd21da058e7c84653d305ea8662ecb4647ebefd2b97411f845fe5b0b841d40d3fe6814eefcb8ce82df341dfce22
1 parent cefa2eb commit 42aea04

File tree

3 files changed

+34
-15
lines changed

3 files changed

+34
-15
lines changed

src/logging.cpp

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,9 @@
88
#include <util/threadnames.h>
99
#include <util/time.h>
1010

11+
#include <algorithm>
12+
#include <array>
13+
1114
const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
1215

1316
BCLog::Logger& LogInstance()
@@ -130,8 +133,7 @@ bool BCLog::Logger::DefaultShrinkDebugFile() const
130133
return m_categories == BCLog::NONE;
131134
}
132135

133-
struct CLogCategoryDesc
134-
{
136+
struct CLogCategoryDesc {
135137
BCLog::LogFlags flag;
136138
std::string category;
137139
};
@@ -201,16 +203,19 @@ bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
201203

202204
std::vector<LogCategory> BCLog::Logger::LogCategoriesList(bool enabled_only) const
203205
{
206+
// Sort log categories by alphabetical order.
207+
std::array<CLogCategoryDesc, std::size(LogCategories)> categories;
208+
std::copy(std::begin(LogCategories), std::end(LogCategories), categories.begin());
209+
std::sort(categories.begin(), categories.end(), [](auto a, auto b) { return a.category < b.category; });
210+
204211
std::vector<LogCategory> ret;
205-
for (const CLogCategoryDesc& category_desc : LogCategories) {
206-
// Omit the special cases.
207-
if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL && category_desc.flag != BCLog::DASH) {
208-
LogCategory catActive;
209-
catActive.category = category_desc.category;
210-
catActive.active = WillLogCategory(category_desc.flag);
211-
if (!enabled_only || catActive.active) {
212-
ret.push_back(catActive);
213-
}
212+
for (const CLogCategoryDesc& category_desc : categories) {
213+
if (category_desc.flag == BCLog::NONE || category_desc.flag == BCLog::ALL || category_desc.flag == BCLog::DASH) continue;
214+
LogCategory catActive;
215+
catActive.category = category_desc.category;
216+
catActive.active = WillLogCategory(category_desc.flag);
217+
if (!enabled_only || catActive.active) {
218+
ret.push_back(catActive);
214219
}
215220
}
216221
return ret;
@@ -261,7 +266,7 @@ namespace BCLog {
261266
}
262267
return ret;
263268
}
264-
}
269+
} // namespace BCLog
265270

266271
void BCLog::Logger::LogPrintStr(const std::string& str)
267272
{

src/logging.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -160,9 +160,9 @@ namespace BCLog {
160160
bool DisableCategory(const std::string& str);
161161

162162
bool WillLogCategory(LogFlags category) const;
163-
/** Returns a vector of the log categories */
163+
/** Returns a vector of the log categories in alphabetical order. */
164164
std::vector<LogCategory> LogCategoriesList(bool enabled_only = false) const;
165-
/** Returns a string with the log categories */
165+
/** Returns a string with the log categories in alphabetical order. */
166166
std::string LogCategoriesString(bool enabled_only = false) const
167167
{
168168
return Join(LogCategoriesList(enabled_only), ", ", [&](const LogCategory& i) { return i.category; });

test/functional/rpc_misc.py

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,13 +55,27 @@ def run_test(self):
5555

5656
assert_raises_rpc_error(-8, "unknown mode foobar", node.getmemoryinfo, mode="foobar")
5757

58-
self.log.info("test logging")
58+
self.log.info("test logging rpc and help")
59+
60+
# Test logging RPC returns the expected number of logging categories.
61+
assert_equal(len(node.logging()), 36)
62+
63+
# Test toggling a logging category on/off/on with the logging RPC.
5964
assert_equal(node.logging()['qt'], True)
6065
node.logging(exclude=['qt'])
6166
assert_equal(node.logging()['qt'], False)
6267
node.logging(include=['qt'])
6368
assert_equal(node.logging()['qt'], True)
6469

70+
# Test logging RPC returns the logging categories in alphabetical order.
71+
sorted_logging_categories = sorted(node.logging())
72+
assert_equal(list(node.logging()), sorted_logging_categories)
73+
74+
# Test logging help returns the logging categories string in alphabetical order.
75+
categories = ', '.join(sorted_logging_categories)
76+
logging_help = self.nodes[0].help('logging')
77+
assert f"valid logging categories are: {categories}" in logging_help
78+
6579
self.log.info("test getindexinfo")
6680
self.restart_node(0, ["-txindex=0"])
6781
# Without any indices running the RPC returns an empty object

0 commit comments

Comments
 (0)