log: sort logging categories alphabetically #22530

pull jonatack wants to merge 4 commits into bitcoin:master from jonatack:sort-log-categories changing 3 files +32 −14
  1. jonatack commented at 3:57 PM on July 22, 2021: member

    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
    }
    
  2. test: verify number of categories returned by logging RPC f720cfa824
  3. DrahtBot added the label Utils/log/libs on Jul 22, 2021
  4. sipa commented at 5:53 PM on July 22, 2021: member

    utACK 1131bf993fa88e796bdb950c051add722a29ebfe

  5. in src/logging.cpp:161 in 1131bf993f outdated
     157 | @@ -160,7 +158,7 @@ const CLogCategoryDesc LogCategories[] =
     158 |      {BCLog::IPC, "ipc"},
     159 |      {BCLog::ALL, "1"},
     160 |      {BCLog::ALL, "all"},
     161 | -};
     162 | +    }};
    


    LarryRuane commented at 5:59 PM on July 22, 2021:
    }};
    

    (clang-format-diff.py suggests this)


    jonatack commented at 9:15 PM on July 22, 2021:

    Thanks. No longer touched.

  6. theStack commented at 6:05 PM on July 22, 2021: member

    Concept ACK

  7. in src/logging.cpp:133 in ca62690bab outdated
     129 | @@ -130,8 +130,7 @@ struct CLogCategoryDesc
     130 |      std::string category;
     131 |  };
     132 |  
     133 | -const CLogCategoryDesc LogCategories[] =
     134 | -{
     135 | +const std::array<CLogCategoryDesc, 28> LogCategories{{
    


    MarcoFalke commented at 6:05 PM on July 22, 2021:

    Is there a reason to change this? The commit doesn't provide a motivation, and it is actually dangerous because it opens the door for silent merge conflicts. Someone removing an element will not result in a compile failure. See also this diff, which compiles:

    diff --git a/src/logging.cpp b/src/logging.cpp
    index c604444acc..b193b77da3 100644
    --- a/src/logging.cpp
    +++ b/src/logging.cpp
    @@ -130,7 +130,7 @@ struct CLogCategoryDesc
         std::string category;
     };
     
    -const std::array<CLogCategoryDesc, 28> LogCategories{{
    +const std::array<CLogCategoryDesc, 29> LogCategories{{
         {BCLog::NONE, "0"},
         {BCLog::NONE, "none"},
         {BCLog::NET, "net"},
    

    jonatack commented at 9:15 PM on July 22, 2021:

    Good point. Dropped this change.

  8. LarryRuane commented at 6:56 PM on July 22, 2021: contributor

    I agree with @MarcoFalke's comment, and I think it's undesirable to hard-code the number of items in any list (24 or 28 in this case, depending on whether the ALL and NONE categories are included) unless really necessary.

    Did you consider simply manually sorting the static LogCategories list instead of soting at runtime? A comment can be added just before its declaration that the list should remain sorted when new entries are added.

    Also, could you please provide a brief motivation for this PR in the description? Unless it's just me and it's obvious to everyone else. My impression is that it allows readers of this list to more quickly find a particular category (to get the exact naming, knowing only roughly what the name is), but are there other reasons? Perhaps just a few words would help (this suggestion is optional).

  9. log: sort LogCategoriesList and LogCategoriesString alphabetically 7c57297319
  10. log, refactor: use guard clause in LogCategoriesList()
    and minor formatting fixups
    17bbff3b88
  11. jonatack force-pushed on Jul 22, 2021
  12. jonatack commented at 9:21 PM on July 22, 2021: member

    Did you consider simply manually sorting the static LogCategories list instead of soting at runtime? A comment can be added just before its declaration that the list should remain sorted when new entries are added.

    I did; it seems preferable not to rely on manually maintaining the sort throughout future changes when it can be automated with non-perceptible cost in a non-hotspot. Edit: the test does verify the sort, so it could be manually sorted if people prefer that. Performance-wise, there shouldn't be a distinguishable difference.

    My impression is that it allows readers of this list to more quickly find a particular category (to get the exact naming, knowing only roughly what the name is), but are there other reasons? Perhaps just a few words would help (this suggestion is optional).

    Oh sorry. User-friendliness for CLI users, exactly as you describe. Updated the pull description.

  13. kristapsk commented at 10:55 PM on July 22, 2021: contributor

    Did you consider simply manually sorting the static LogCategories list instead of soting at runtime? A comment can be added just before its declaration that the list should remain sorted when new entries are added.

    I did; it seems preferable not to rely on manually maintaining the sort throughout future changes when it can be automated with relatively non-perceptible cost in a non-hotspot.

    Sounds like something that can be checked by some linter script.

  14. ghost commented at 12:31 AM on July 23, 2021: none

    Concept ACK. Tried and it works as expected (returns debug categories in alphabetical order)

    <details><summary>bitcoind -h | grep -A8 "debug=<category>"</summary>

    image</details>

    <details><summary>bitcoin-cli help logging</summary>

    image</details>

    <details><summary>bitcoin-cli logging [] '["addrman"]'</summary>

    image</details>

    I think it's undesirable to hard-code the number of items in any list (24 or 28 in this case, depending on whether the ALL and NONE categories are included) unless really necessary.

    Not sure about this. Maybe there is a better approach for test in https://github.com/bitcoin/bitcoin/pull/22530/commits/f720cfa824f1be863349e7016080f8fb1c3c76c2 or this test will fail if we add new category in future and it can be incremented accordingly.

  15. unknown approved
  16. practicalswift commented at 7:51 PM on July 24, 2021: contributor

    cr ACK a744e79e4d07358d331d8e2f732511e2eca0e3f0

    Nice small usability improvement!

  17. in test/functional/rpc_misc.py:60 in a744e79e4d outdated
      53 | @@ -54,13 +54,27 @@ def run_test(self):
      54 |  
      55 |          assert_raises_rpc_error(-8, "unknown mode foobar", node.getmemoryinfo, mode="foobar")
      56 |  
      57 | -        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()), 24)
    


    LarryRuane commented at 12:49 PM on July 25, 2021:

    Is this needed? It makes the test fragile, but it does catch bugs where logging() returns an empty list. But such a bug would be caught just below (enabling and disabling qt logging).


    jonatack commented at 5:14 PM on July 27, 2021:

    It gave me a sanity check that the number of elements returned before the change is the same as after. That assertion could be dropped in the last commit if people prefer, but incrementing the value when a category is added seems fine to me.

  18. LarryRuane commented at 1:21 PM on July 25, 2021: contributor

    the test does verify the sort, so it could be manually sorted if people prefer that.

    I'd prefer that. I'm -0 on the PR as it stands. Even though the runtime cost is small (in CPU and memory for the additional code), on principle, I prefer not to do things at runtime that can easily be done at compile time (or, I guess in this case, pre-compile time!). I like the idea of keeping the test, or a linter as @kristapsk suggested. The test is probably better since it verifies actual behavior.

    If we do go with the manual sort idea, let's add a comment before the declaration of LogCategories[], something like "This list should be sorted" or "Please keep this list sorted."

  19. theStack approved
  20. theStack commented at 9:58 PM on July 26, 2021: member

    For me, both discussed approaches of either manually maintining the sort order of the categories at (pre-)compile-time or doing it at runtime (the current PR approach) are fine. Though like LarryRuane, I'd have a slight preference to the static approach; OTOH, since this part is really not performance-critical (as argued by (jonatack)) the dynamic approach seems acceptable. Most importantly, the user-friendliness is improved and we also have a test that ensures that.

    Tested ACK a744e79e4d07358d331d8e2f732511e2eca0e3f0 (also tested the test by checking that it fails on the master branch :))

  21. jonatack commented at 5:02 PM on July 27, 2021: member

    Thanks for the feedback. As to the approach of automated versus manual sorting, I don't have a strong opinion. In this context of an inexpensive operation not in a hotspot, on a project with many contributors, the avoidance of future maintainance + "keep this manually sorted"-style documentation that automating the sort enables seems slightly more compelling to me, but either way seems fine.

  22. jarolrod commented at 3:01 AM on July 28, 2021: member

    tACK a744e79e4d07358d331d8e2f732511e2eca0e3f0

    This is an improvement. If someone wants to coordinate and make a policy of manual sorting, it can be done in a followup.

  23. in test/functional/rpc_misc.py:74 in a744e79e4d outdated
      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 | +        first_3_categories = ', '.join(sorted_logging_categories[0:3])
    


    vasild commented at 9:58 AM on July 28, 2021:

    Why only the first 3? The following works as well:

            first_3_categories = ', '.join(sorted_logging_categories)
    

    jonatack commented at 10:38 AM on July 28, 2021:

    Good call--the line breaks I saw while using pprint to check the values had me needlessly worried. Done.

  24. vasild approved
  25. vasild commented at 10:03 AM on July 28, 2021: member

    ACK a744e79e4d07358d331d8e2f732511e2eca0e3f0

  26. test: assert logging categories are sorted in rpc and help d596dba987
  27. jonatack force-pushed on Jul 28, 2021
  28. jonatack commented at 10:42 AM on July 28, 2021: member

    Thanks for the reviewing and feedback! Updated per @vasild's #22530 (review)

    <details><summary><code>git diff a744e79 d596dba</code></summary><p>

    diff --git a/test/functional/rpc_misc.py b/test/functional/rpc_misc.py
    index b6117b9e7b..563f2ea43e 100755
    --- a/test/functional/rpc_misc.py
    +++ b/test/functional/rpc_misc.py
    @@ -71,9 +71,9 @@ class RpcMiscTest(BitcoinTestFramework):
             assert_equal(list(node.logging()), sorted_logging_categories)
     
             # Test logging help returns the logging categories string in alphabetical order.
    -        first_3_categories = ', '.join(sorted_logging_categories[0:3])
    +        categories = ', '.join(sorted_logging_categories)
             logging_help = self.nodes[0].help('logging')
    -        assert f"valid logging categories are: {first_3_categories}" in logging_help
    +        assert f"valid logging categories are: {categories}" in logging_help
    

    </p></details>

  29. theStack approved
  30. theStack commented at 11:55 AM on July 28, 2021: member

    re-ACK d596dba9877e7ead3fb5426cbe7e608fbcbfe3eb (CI error is obviously unrelated)

  31. MarcoFalke merged this on Jul 28, 2021
  32. MarcoFalke closed this on Jul 28, 2021

  33. vasild commented at 12:34 PM on July 28, 2021: member

    ACK d596dba9877e7ead3fb5426cbe7e608fbcbfe3eb

  34. jonatack deleted the branch on Jul 28, 2021
  35. sidhujag referenced this in commit 65b39b9134 on Jul 28, 2021
  36. DrahtBot locked this on Aug 18, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin/bitcoin. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2026-04-13 15:14 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me