log: deduplicate category names and improve logging.cpp #29419

pull vasild wants to merge 2 commits into bitcoin:master from vasild:dedup_logging_categories changing 2 files +65 −134
  1. vasild commented at 2:28 pm on February 11, 2024: contributor

    The code in logging.cpp needs to:

    • Get the category name given the flag (e.g. BCLog::PRUNE -> "prune")
    • Get the flag given the category name (e.g. "prune" -> BCLog::PRUNE)
    • Get the list of category names sorted in alphabetical order

    Achieve this by using the proper std containers. The result is

    • less code (the diff of the first commit is +62 / -129)
    • faster code (to linear search and no copy+sort)
    • more maintainable code (the categories are no longer duplicated in LogCategories[] and LogCategoryToStr())

    This behavior is preserved: BCLog::NONE -> "" (lookup by LogCategoryToStr()) "" -> BCLog::ALL (lookup by GetLogCategory(""))


    Also remove unused BCLog::UTIL.


    These changes (modulo the BCLog::UTIL removal) are part of #29415 but they make sense on their own and would be good to have them, regardless of the fate of #29415. Also, if this is merged, that would reduce the size of #29415, thus the current standalone PR.

  2. log: deduplicate category names and improve logging.cpp
    The code in `logging.cpp` needs to:
    * Get the category name given the flag (e.g. `BCLog::PRUNE` -> `"prune"`)
    * Get the flag given the category name (e.g. `"prune"` -> `BCLog::PRUNE`)
    * Get the list of category names sorted in alphabetical order
    
    Achieve this by using the proper std containers. The result is
    * less code (this diff is +62 / -129)
    * faster code (to linear search and no copy+sort)
    * more maintainable code (the categories are no longer duplicated in
      `LogCategories[]` and `LogCategoryToStr()`)
    
    This behavior is preserved:
    `BCLog::NONE` -> `""` (lookup by `LogCategoryToStr()`)
    `""` -> `BCLog::ALL` (lookup by `GetLogCategory("")`)
    d3b3af9034
  3. logging: remove unused BCLog::UTIL
    Suggested by: David Gumberg (https://github.com/bitcoin/bitcoin/pull/29415#discussion_r1485310634)
    b0344c219a
  4. DrahtBot commented at 2:28 pm on February 11, 2024: contributor

    The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.

    Code Coverage

    For detailed information about the code coverage, see the test coverage report.

    Reviews

    See the guideline for information on the review process.

    Type Reviewers
    ACK davidgumberg, pinheadmz, ryanofsky

    If your review is incorrectly listed, please react with 👎 to this comment and the bot will ignore it on the next update.

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #29432 (Stratum v2 Template Provider (take 3) by Sjors)
    • #29415 (Broadcast own transactions only via short-lived Tor or I2P connections by vasild)
    • #29346 (Stratum v2 Noise Protocol by Sjors)
    • #26697 (logging: use bitset for categories by LarryRuane)

    If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first.

  5. DrahtBot added the label Utils/log/libs on Feb 11, 2024
  6. maflcko requested review from jonatack on Mar 11, 2024
  7. pinheadmz approved
  8. pinheadmz commented at 2:38 pm on March 12, 2024: member

    ACK b0344c219a641b759fb0cc4f53afebe675b8ca27

    Reviewed code as part of #29415 confirmed no diff in the first commit, Second commit looks good as well (confirmed no usage in code of “util” log category). Built on arm/macOS and ran all functional and unit tests.

     0-----BEGIN PGP SIGNED MESSAGE-----
     1Hash: SHA256
     2
     3ACK b0344c219a641b759fb0cc4f53afebe675b8ca27
     4-----BEGIN PGP SIGNATURE-----
     5
     6iQIzBAEBCAAdFiEE5hdzzW4BBA4vG9eM5+KYS2KJyToFAmXwZA4ACgkQ5+KYS2KJ
     7yTrJ1g/9Fz6E1A37SBb9FWrndC9xgTYLBzEBRtgy1+95yfmeLCpMUbY5Cjvp97I7
     8jhuSweOry0H5Z7SMtfvTMhd2AYxHuGs6Ygj8qt+tOhRj/vhI+mQ53Qu0Fm9nbLsp
     9AkGvN5kx3uGSYAgHkfqq+KsH3Q3KkeEjW9q3wX8aW5oW5XDGkTpTPOqMYCpuGhmk
    10Ih56P3D5I+NuI9e3hIrEPajnYvMz9f/aIeD6VEWGb9Jx19BHqX86sIJ3g38Ej7eI
    11gqH6l0RuGWnA10P7NGoSZBXdrvFAQ8hWNZUW87MjhfPodjjbRwTBwtYUWUnRc3cw
    126kE07moevRoEcjs7NcCmI91pMk5NYdZdyyPHCWODMCLTjDVg5J3806tEFt8N1Klc
    13stxv4atBvMfVK7fgnjVSZG2mrJo08XU+VmY1hx0vc3jMnGk3rtziExN+vsPxYUM9
    14YjD4O/Z3yx9c83fA7wcBE6jqS6EN+u7URgxwDAjTYyjH4ajnReetc5m0LAA0fC4Q
    15uYKNShC+DQpfsmZw74/ROiVcRyKXUvI5O9EnlUJNIh/jM9OkPcbJkmURqTLRbCOE
    16x3/GSbQN+4+e+J3XPGIL1sPGH5YLOr2HLtIgMJZhYzSr53nKyHgLYRtrCTE9F7bl
    17A7+REDbe6oW63GdZv+hA5c0W0kR+LOaOqB/Hl2hrss1fIUL6LuM=
    18=yCfD
    19-----END PGP SIGNATURE-----
    

    pinheadmz’s public key is on keybase

  9. in src/logging.cpp:191 in d3b3af9034 outdated
    227+        std::unordered_map<BCLog::LogFlags, std::string> out;
    228+        for (const auto& [k, v] : in) {
    229+            switch (v) {
    230+            case BCLog::NONE: out.emplace(BCLog::NONE, ""); break;
    231+            case BCLog::ALL: out.emplace(BCLog::ALL, "all"); break;
    232+            default: out.emplace(v, k);
    


    ryanofsky commented at 1:55 pm on April 2, 2024:

    In commit “logging: remove unused BCLog::UTIL” (b0344c219a641b759fb0cc4f53afebe675b8ca27)

    Would be good to assert that emplace succeeded, and the map is consistent.

  10. in src/logging.cpp:190 in d3b3af9034 outdated
    226+    [](const std::map<std::string, BCLog::LogFlags>& in) {
    227+        std::unordered_map<BCLog::LogFlags, std::string> out;
    228+        for (const auto& [k, v] : in) {
    229+            switch (v) {
    230+            case BCLog::NONE: out.emplace(BCLog::NONE, ""); break;
    231+            case BCLog::ALL: out.emplace(BCLog::ALL, "all"); break;
    


    ryanofsky commented at 2:20 pm on April 2, 2024:

    In commit “logging: remove unused BCLog::UTIL” (b0344c219a641b759fb0cc4f53afebe675b8ca27)

    I think it would make code more straightforward if we could drop these special cases here and avoid having discrepancies between LOG_CATEGORIES_BY_FLAG and LOG_CATEGORIES_BY_STR maps.

    There are already special cases in existing GetLogCategory and LogCategoriesList functions so I think it would make more sense to put new special cases there and not introduce them in a third place. Would suggest:

     0--- a/src/logging.cpp
     1+++ b/src/logging.cpp
     2@@ -142,7 +142,6 @@ bool BCLog::Logger::DefaultShrinkDebugFile() const
     3 }
     4 
     5 static const std::map<std::string, BCLog::LogFlags> LOG_CATEGORIES_BY_STR{
     6-    {"0", BCLog::NONE},
     7     {"", BCLog::NONE},
     8     {"net", BCLog::NET},
     9     {"tor", BCLog::TOR},
    10@@ -175,7 +174,6 @@ static const std::map<std::string, BCLog::LogFlags> LOG_CATEGORIES_BY_STR{
    11     {"txreconciliation", BCLog::TXRECONCILIATION},
    12     {"scan", BCLog::SCAN},
    13     {"txpackages", BCLog::TXPACKAGES},
    14-    {"1", BCLog::ALL},
    15     {"all", BCLog::ALL},
    16 };
    17 
    18@@ -184,11 +182,8 @@ static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_
    19     [](const std::map<std::string, BCLog::LogFlags>& in) {
    20         std::unordered_map<BCLog::LogFlags, std::string> out;
    21         for (const auto& [k, v] : in) {
    22-            switch (v) {
    23-            case BCLog::NONE: out.emplace(BCLog::NONE, ""); break;
    24-            case BCLog::ALL: out.emplace(BCLog::ALL, "all"); break;
    25-            default: out.emplace(v, k);
    26-            }
    27+            bool inserted{out.emplace(v, k).second};
    28+            assert(inserted);
    29         }
    30         return out;
    31     }(LOG_CATEGORIES_BY_STR)
    32@@ -196,9 +191,12 @@ static const std::unordered_map<BCLog::LogFlags, std::string> LOG_CATEGORIES_BY_
    33 
    34 bool GetLogCategory(BCLog::LogFlags& flag, const std::string& str)
    35 {
    36-    if (str.empty()) {
    37+    if (str.empty() || str == "1") {
    38         flag = BCLog::ALL;
    39         return true;
    40+    } else if (str == "0") {
    41+        flag = BCLog::NONE;
    42+        return true;
    43     }
    44     auto it = LOG_CATEGORIES_BY_STR.find(str);
    45     if (it != LOG_CATEGORIES_BY_STR.end()) {
    
  11. in src/logging.cpp:146 in d3b3af9034 outdated
    174-    {BCLog::VALIDATION, "validation"},
    175-    {BCLog::I2P, "i2p"},
    176-    {BCLog::IPC, "ipc"},
    177+static const std::map<std::string, BCLog::LogFlags> LOG_CATEGORIES_BY_STR{
    178+    {"0", BCLog::NONE},
    179+    {"", BCLog::NONE},
    


    ryanofsky commented at 2:35 pm on April 2, 2024:

    In commit “log: deduplicate category names and improve logging.cpp” (d3b3af90343b7671231afd7dff87e87ff86d31d7)

    I think it’s good to keep the "" to NONE map entry in this commit, just to make it obvious this commit is not changing behavior. But in a followup, I think it would be good to drop this entry because it doesn’t actually do anything useful, and it is confusing to have a map from "" to NONE in one place and ALL in another.

  12. ryanofsky approved
  13. ryanofsky commented at 2:46 pm on April 2, 2024: contributor

    Code review ACK b0344c219a641b759fb0cc4f53afebe675b8ca27. Nice cleanup! Having to maintain multiple copies of the same mapping seemed messy and a like a possible footgun. I checked old and new mappings in both directions and confirmed no behavior should be changing.

    I do suspect the previous code was probably more efficient, but the new code could be adopted to use string_view and constexpr arrays if needed.

  14. ryanofsky merged this on Apr 2, 2024
  15. ryanofsky closed this on Apr 2, 2024

  16. vasild deleted the branch on Apr 2, 2024
  17. vasild commented at 11:54 am on April 3, 2024: contributor
    @ryanofsky, thanks for the review! Addressed your pending suggestions in #29798.

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: 2024-12-22 06:12 UTC

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