Skip to content

Commit

Permalink
Merge bitcoin#22530: log: sort logging categories alphabetically
Browse files Browse the repository at this point in the history
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
  • Loading branch information
MarcoFalke authored and vijaydasmp committed Nov 7, 2023
1 parent 3db0aeb commit 70ec0dc
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 15 deletions.
29 changes: 17 additions & 12 deletions src/logging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#include <util/threadnames.h>
#include <util/time.h>

#include <algorithm>
#include <array>

const char * const DEFAULT_DEBUGLOGFILE = "debug.log";

BCLog::Logger& LogInstance()
Expand Down Expand Up @@ -130,8 +133,7 @@ bool BCLog::Logger::DefaultShrinkDebugFile() const
return m_categories == BCLog::NONE;
}

struct CLogCategoryDesc
{
struct CLogCategoryDesc {
BCLog::LogFlags flag;
std::string category;
};
Expand Down Expand Up @@ -199,16 +201,19 @@ bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)

std::vector<LogCategory> BCLog::Logger::LogCategoriesList(bool enabled_only) const
{
// Sort log categories by alphabetical order.
std::array<CLogCategoryDesc, std::size(LogCategories)> 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<LogCategory> ret;
for (const CLogCategoryDesc& category_desc : LogCategories) {
// Omit the special cases.
if (category_desc.flag != BCLog::NONE && category_desc.flag != BCLog::ALL && category_desc.flag != BCLog::DASH) {
LogCategory catActive;
catActive.category = category_desc.category;
catActive.active = WillLogCategory(category_desc.flag);
if (!enabled_only || catActive.active) {
ret.push_back(catActive);
}
for (const CLogCategoryDesc& category_desc : categories) {
if (category_desc.flag == BCLog::NONE || category_desc.flag == BCLog::ALL && category_desc.flag != BCLog::DASH) continue;
LogCategory catActive;
catActive.category = category_desc.category;
catActive.active = WillLogCategory(category_desc.flag);
if (!enabled_only || catActive.active) {
ret.push_back(catActive);
}
}
return ret;
Expand Down Expand Up @@ -259,7 +264,7 @@ namespace BCLog {
}
return ret;
}
}
} // namespace BCLog

void BCLog::Logger::LogPrintStr(const std::string& str)
{
Expand Down
4 changes: 2 additions & 2 deletions src/logging.h
Original file line number Diff line number Diff line change
Expand Up @@ -160,9 +160,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<LogCategory> LogCategoriesList(bool enabled_only = false) const;
/** Returns a string with the log categories */
/** Returns a string with the log categories in alphabetical order. */
std::string LogCategoriesString(bool enabled_only = false) const
{
return Join(LogCategoriesList(enabled_only), ", ", [&](const LogCategory& i) { return i.category; });
Expand Down
7 changes: 6 additions & 1 deletion test/functional/rpc_misc.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,12 @@ def run_test(self):

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()), 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)
Expand Down

0 comments on commit 70ec0dc

Please sign in to comment.