Skip to content

Commit 5e21382

Browse files
author
MarcoFalke
committed
Merge bitcoin/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
2 parents 93878d2 + d596dba commit 5e21382

File tree

3 files changed

+32
-14
lines changed

3 files changed

+32
-14
lines changed

src/logging.cpp

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

11+
#include <algorithm>
12+
#include <array>
1113
#include <mutex>
1214

1315
const char * const DEFAULT_DEBUGLOGFILE = "debug.log";
@@ -124,8 +126,7 @@ bool BCLog::Logger::DefaultShrinkDebugFile() const
124126
return m_categories == BCLog::NONE;
125127
}
126128

127-
struct CLogCategoryDesc
128-
{
129+
struct CLogCategoryDesc {
129130
BCLog::LogFlags flag;
130131
std::string category;
131132
};
@@ -179,15 +180,18 @@ bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
179180

180181
std::vector<LogCategory> BCLog::Logger::LogCategoriesList() const
181182
{
183+
// Sort log categories by alphabetical order.
184+
std::array<CLogCategoryDesc, std::size(LogCategories)> categories;
185+
std::copy(std::begin(LogCategories), std::end(LogCategories), categories.begin());
186+
std::sort(categories.begin(), categories.end(), [](auto a, auto b) { return a.category < b.category; });
187+
182188
std::vector<LogCategory> ret;
183-
for (const CLogCategoryDesc& category_desc : LogCategories) {
184-
// Omit the special cases.
185-
if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL) {
186-
LogCategory catActive;
187-
catActive.category = category_desc.category;
188-
catActive.active = WillLogCategory(category_desc.flag);
189-
ret.push_back(catActive);
190-
}
189+
for (const CLogCategoryDesc& category_desc : categories) {
190+
if (category_desc.flag == BCLog::NONE || category_desc.flag == BCLog::ALL) continue;
191+
LogCategory catActive;
192+
catActive.category = category_desc.category;
193+
catActive.active = WillLogCategory(category_desc.flag);
194+
ret.push_back(catActive);
191195
}
192196
return ret;
193197
}
@@ -237,7 +241,7 @@ namespace BCLog {
237241
}
238242
return ret;
239243
}
240-
}
244+
} // namespace BCLog
241245

242246
void BCLog::Logger::LogPrintStr(const std::string& str, const std::string& logging_function, const std::string& source_file, const int source_line)
243247
{

src/logging.h

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

140140
bool WillLogCategory(LogFlags category) const;
141-
/** Returns a vector of the log categories */
141+
/** Returns a vector of the log categories in alphabetical order. */
142142
std::vector<LogCategory> LogCategoriesList() const;
143-
/** Returns a string with the log categories */
143+
/** Returns a string with the log categories in alphabetical order. */
144144
std::string LogCategoriesString() const
145145
{
146146
return Join(LogCategoriesList(), ", ", [&](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
@@ -54,13 +54,27 @@ def run_test(self):
5454

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

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

69+
# Test logging RPC returns the logging categories in alphabetical order.
70+
sorted_logging_categories = sorted(node.logging())
71+
assert_equal(list(node.logging()), sorted_logging_categories)
72+
73+
# Test logging help returns the logging categories string in alphabetical order.
74+
categories = ', '.join(sorted_logging_categories)
75+
logging_help = self.nodes[0].help('logging')
76+
assert f"valid logging categories are: {categories}" in logging_help
77+
6478
self.log.info("test echoipc (testing spawned process in multiprocess build)")
6579
assert_equal(node.echoipc("hello"), "hello")
6680

0 commit comments

Comments
 (0)