mirror of
https://github.com/bitcoin/bitcoin
synced 2026-12-04 09:11:37 +00:00
logging: streamline Logger state and drop redundant methods #35322
No reviewers
Labels
No labels
Android
Backport
Block storage
Brainstorming
Bug
Build system
CI failed
Consensus
Data corruption
Descriptors
Docs
DrahtBot Guix build requested
Feature
Fuzzing
GUI
Good First Review
IPC
Insufficient Review
Kernel
Linux/Unix
Mempool
Mining
Needs Backport (31.x)
Needs Backport (32.x)
Needs Benchmark
Needs Conceptual Review
Needs backport (30.x)
Needs rebase
Needs release note
Net
P2P
PSBT
Privacy
Private Broadcast
Questions and Help
RPC/REST/ZMQ
Refactoring
Resource usage
Review club
Scripts and tools
Settings
TX fees and policy
Tests
Tracking Issue
UTXO Db and Indexes
Up for grabs
Upstream
Utils/log/libs
Validation
Waiting for author
Wallet
Windows
good first issue
interfaces
macOS
No milestone
No project
No assignees
2 participants
Notifications
Due date
No due date set.
Dependencies
No dependencies set
Reference
bitcoin/bitcoin!35322
Loading…
Reference in a new issue
No description provided.
Delete branch "pr/levels"
Deleting a branch is permanent. Although the deleted branch may continue to exist for a short time before it actually gets removed, it CANNOT be undone in most cases. Continue?
Problem: The
Loggerclass currently tracks enabled log messages using three separate fields:m_categories— bitmask of enabled log categoriesm_log_level— global log level thresholdm_category_log_levels— hash map of per-category level overridesThis representation is confusing and redundant. Bitcoin Core has only ~30 log categories and 2 conditional log levels, so a mutex-protected hash map, a bitset, and a separate global level are unnecessary. The added complexity is also harmful: it makes the code harder to follow, and basic operations like setting a category's level require coordinating multiple fields and method calls.
Solution: This PR streamlines the
Loggerclass without changing behavior, replacing the three fields with two atomics: one bitmask for each conditional log level (DebugandTrace). Each bitmask records which categories are enabled at that level.This allows the following methods to be dropped:
EnableCategory,DisableCategory,WillLogCategory,LogLevel,SetLogLevel,GetCategoryMask,AddCategoryLogLevel. The remainingSetCategoryLogLevelandWillLogCategoryLevelmethods are now self-contained.Behavior note: This is a behavior-preserving refactor with one exception: categories dynamically enabled at runtime through the
loggingRPC or the kernelbtck_logging_*API are now always enabled at Debug level, even if-loglevel=traceor its kernel equivalent was set at startup. This is unlikely to matter in practice. Very littleLogTracelogging currently exists, and both APIs are being reworked in bitcoin/bitcoin#34038 and bitcoin/bitcoin#34374. The-logleveloption otherwise continues to work as before when categories are not changed at runtime.The following sections might be updated with supplementary metadata relevant to reviewers and maintainers.
External sites
Reviews
See the guideline and AI policy for information on the review process.
A summary of reviews will appear here.
Conflicts
Reviewers, this pull request conflicts with the following ones:
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.
There's a single commit here with a lot of changes that don't look closely related.
I don't have a strong opinion about logging, but it's obviously important to others so I don't mind reviewing it - though in this current state I can't do that meaningfully.
If you think it's worth it, could you please split this into smaller, more focused commits (maybe multiple PRs, since the title already suggests it's hard to properly describe the goal of the changes)?
re: https://github.com/bitcoin/bitcoin/pull/35322#discussion_r3272563675
Thanks for looking at this! This PR is a fairly small change: only +77/-124 lines excluding tests. But I appreciate it could be pretty difficult to understand if you are coming in cold and don't know where to begin.
I think it should be possible to split the PR up and I'll experiment with this. (Here is a branch claude made which splits the PR into two commits. pr/levels-explain. It looks ok but I want to verify.)
If I can give a review tip for the current PR, it would be to first look at the new
SetCategoryLogLevel & WillLogCategoryLevelimplementations and understand how they use the m_levels array to determine what to log and not log.After you understand those two methods, you basically understand the whole PR because it is just updating the rest of the code to call those methods.
Let me know if I can help with this, I do hope the description is clear.
I tried splitting this locally to make review easier. I still think the current version mixes several separate concerns, and I have questions about whether grouping levels and categories this way is the right model, since it seems to apply a general rule to behavior that is currently quite specific and ad hoc.
Left comments on splitting the commit into smaller reviewable steps, clarifying or preserving behavior changes, and adding focused tests for intentional changes.
Main concerns are startup/kernel logging semantics, runtime-enable behavior coverage, and making the new
m_levelsmodel easier to reason about.This looks similar to the earlier concern. Isn't this a behavior change as well? Previously, setting a category level did not enable the category. Now it looks like setting the level also enables it. Should we adjust this to match the old behavior?
I see the tests don't always keep the enable-first-set-level after, but maybe we can do that:
This loop has dual behavior that needed extra explanation in the header. Maybe we could make it more obvious via code instead:
But even this seems excessive if indeed the only phase transitions are:
This seems like a code smell (even though it was similar before). Wouldn't the categories already return the same value here - Is this an optimization? So if
infohasBCLog::LogFlags::ALLit will take this branch, but ifdebughas the same it will take the next line?This looks like save/restore plumbing for tests, not part of the core representation swap. Could it be split into the commit where the test fixtures first need to snapshot the level masks?
Could this naming cleanup be split before the atomic-mask change?
The current code already has one threshold that means "levels at or above this always log". Giving that threshold a shared name first would make the later mask change smaller, and the startup parsing checks could use the same name instead of hardcoding
Level::Info.And we could probably use this new constant in a few more places:
Could the comment say explicitly that
m_levelsonly stores conditional levels (TraceandDebug), whileInfoand above bypass this array?I initially read the threshold wording as trying to represent all severities, but the array deliberately stops at
UNCONDITIONAL_LOG_LEVEL.Seems we have two fundamentally different ways of logging that we're forcing together here. Do you anticipate category-based filtering for all levels in the future?
I find this design confusing for multiple reasons:
debug/tracecan also be turned on byBCLog::ALL, and they would take a different route than unconditionalinfo/warn/error.Could we add a test that goes through this runtime-enable path after startup configured the category at Trace?
The PR description notes that runtime enables now use Debug even if startup used Trace. A test through the RPC/kernel entry point would make that intentional behavior change easy to verify.
We can probably deduplicate this:
This string-parsing overload no longer has production callers after startup parsing moves out of Logger. Could we drop it and have the test use the typed setter directly?
I would prefer peeling off each method separately to have some progress during review.
For example,
getLogCategories()is only used in a single place to decide whether the GUI should show debug information, and that can be split out before the category-mask refactor.Reviewing whether the GUI should check for any enabled category mask or for actual Debug-level logging is already non-trivial and worthy of separation.
isAnyDebugLoggingEnabled
Isn’t this a behavior change?
Previously, setting a category level at startup stored the override, but it had no visible effect unless the category was also enabled with -debug.
Here, setting
-loglevel=net:traceappears to enable net by itself.Rebased ccd90a75125586f4f5a03fda5477738b10745d20 -> a76b13796a59e0c91548882c1a1970943a200153 (
pr/levels.1->pr/levels.2, compare) due to conflict with #34806🚧 At least one of the CI tasks failed.
Task
Windows-cross to x86_64, msvcrt: https://github.com/bitcoin/bitcoin/actions/runs/26472785758/job/77960548589LLM reason (✨ experimental): CI failed due to a C++ build error in
src/init/common.cpp:GetLogCategoryis not declared (missing/renamed logging helper).Hints
Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:
Possibly due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the
affected test.
An intermittent issue.
Leave a comment here, if you need help tracking down a confusing failure.
Thanks for the review! PR is now split up instead of being a single commit and I implemented most suggestions, but need to follow up on the ideas for adding more tests. (EDIT: tests are added now)
Updated a76b13796a59e0c91548882c1a1970943a200153 -> 30cb9ee48027f4b32ca1c98e2cafc1a6e4388e53 (
pr/levels.2->pr/levels.3, compare) with review suggestionsUpdated 30cb9ee48027f4b32ca1c98e2cafc1a6e4388e53 -> dd4b33e01ca2441fcb875e0ecb084643ecc50cd8 (
pr/levels.3->pr/levels.4, compare) adding new tests commits before changes to cover more behaviors, also making other small cleanupsUpdated dd4b33e01ca2441fcb875e0ecb084643ecc50cd8 -> 70b4bee3cc7392a8189a98d35f382e210ae7c2cc (
pr/levels.4->pr/levels.5, compare) to fix CI link errors: LogInstance() hidden by REDUCE_EXPORTS=ON; override visibility for logging.cpp in kernel build https://github.com/bitcoin/bitcoin/actions/runs/26643504769/job/78546254795Updated 70b4bee3cc7392a8189a98d35f382e210ae7c2cc -> be5e736f4976686c3fa0200f35e10ce0b656c351 (
pr/levels.5->pr/levels.6, compare) to fix Windows DLL link errors in test_kernel https://github.com/bitcoin/bitcoin/actions/runs/26787798064/job/78967518054Updated be5e736f4976686c3fa0200f35e10ce0b656c351 -> 395473b97ad229d4c80c45bd5dcfa4c885acf09d (
pr/levels.6->pr/levels.7, compare) to fix Windows cross CI: --export-dynamic-symbol and -fvisibility=default are ELF-only; use .def file to export logging symbols from DLL https://github.com/bitcoin/bitcoin/actions/runs/27141219495/job/80118211985re: https://github.com/bitcoin/bitcoin/pull/35322#discussion_r3289129041
Yes this analogous to the other change you pointed out https://github.com/bitcoin/bitcoin/pull/35322#discussion_r3281289661.
In this case I think the new behavior is probably better as there is no point to kernel code setting a level for a category that is ignored because the category is enabled. (For
bitcoindit might have made sense previously becauseloggingRPC is more limited than the kernel API.)But this behavior change should be documented and tested, so I will try to add your test suggestion and follow up.
re: https://github.com/bitcoin/bitcoin/pull/35322#discussion_r3288806782
I added a comment here to be clearer, and I'm open to feedback on different ways to write this, but I feel like the current version is actually simpler than the suggestions (of using two
forloops or hardcoding severity constants). The current code just loops through each level and setting category bits to 1 if the level is enabled, 0 if disabled.re: https://github.com/bitcoin/bitcoin/pull/35322#discussion_r3288138282
Yeah agree this could be better, should be improved now but let me know.
re: https://github.com/bitcoin/bitcoin/pull/35322#discussion_r3280651475
Yes, this should be done now.
re: https://github.com/bitcoin/bitcoin/pull/35322#discussion_r3279807731
Following your other suggestion https://github.com/bitcoin/bitcoin/pull/35322#discussion_r3279693120 this -loglevel parsing code is now dropped from the logger class.
This is probably worth improving still. I'm happy to follow any preferences expressed in this PR or #35387 which improves the
-loglevelimplementation specifically. For now the code is using Info just to be explicit about the behavior it is trying to preserve.re: https://github.com/bitcoin/bitcoin/pull/35322#discussion_r3288013785
Yes, changed array declaration and added comment to make this clearer.
I don't think that's right.
Loggeraccepts a category and severity level for each log message and it provides aSetCategoryLogLevel(category, level)method to set a minimum severity level for each category and control what is logged.The fact that
Loggeralways logs the highest severity messages seems like an internal implementation detail, not a reason to complicate its API. Or if the API should be more complicated, it would be good to know why, or what the goal would be.No, I think it makes sense to have some levels that can't be turned off or filtered by category. (IMO it would best if to introduce a new critical level for this purpose, so lower-severity errors, warnings, and status messages could be distinguished from each other. But this is just a weak preference and should be unrelated to this PR.)
I don't think I have an opinion about this. Having something like separate enums for conditional and unconditional levels could be reasonable, but I'd want to know what the goal would be, or what types of bugs this could prevent.
I'm not sure I understand the concern. Inside the
Loggerclass, all messages are taking the same route. It is intentional to provide a way to turn on debug and tracing for all categories. The decision to make it difficult to turn off info/warning/error logs doesn't seem like it should affect this.re: https://github.com/bitcoin/bitcoin/pull/35322#discussion_r3289031399
Yes this is a good idea. Current PR doesn't add new tests but it should and I will follow up with this.
re: https://github.com/bitcoin/bitcoin/pull/35322#discussion_r3289092797
Make sense, appied suggestion
re: https://github.com/bitcoin/bitcoin/pull/35322#discussion_r3279693120
Nice find! This lets another logger method to be dropped. Implemented this change.
re: https://github.com/bitcoin/bitcoin/pull/35322#discussion_r3279624744
Makes sense, and latest push removes each method in separate commits. Splitting this up does add some churn to the PR, because there is now temporary code added in the first commit that is removed in later commits. So some reviewers may prefer to just look at the overall diff, but probably splitting is an improvement.
re: https://github.com/bitcoin/bitcoin/pull/35322#discussion_r3281289661
Yes, nice catch! I do think this behavior change is good, but it wasn't intended to be part of this PR. It's fixed by making
-loglevelonly update the levels of previously enabled categories.Edit: To follow up, I should add a test to make sure a standalone
-logleveloption without-debugcontinues to be ignored.🚧 At least one of the CI tasks failed.
Task
Windows-cross to x86_64, ucrt: https://github.com/bitcoin/bitcoin/actions/runs/26643504769/job/78546254615LLM reason (✨ experimental): Link step for
test_kernel.exefailed with undefined references toLogInstance()/BCLog::Logger::WillLogCategoryLevel().Hints
Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:
Possibly due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the
affected test.
An intermittent issue.
Leave a comment here, if you need help tracking down a confusing failure.
🐙 This pull request conflicts with the target branch and needs rebase.
🚧 At least one of the CI tasks failed.
Task
test ancestor commits: https://github.com/bitcoin/bitcoin/actions/runs/28106965454/job/83223705517LLM reason (✨ experimental): CI failed because the
logging_testsctest suite had 1 failing test (254 - logging_tests (Failed)), causingctestto exit with non-zero status.Hints
Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:
Possibly due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the
affected test.
An intermittent issue.
Leave a comment here, if you need help tracking down a confusing failure.
🐙 This pull request conflicts with the target branch and needs rebase.
🐙 This pull request conflicts with the target branch and needs rebase.
🐙 This pull request conflicts with the target branch and needs rebase.
🟠 Minor ·
-loglevelresults depend on option orderMixing global and per-category
-loglevelvalues can silently lose or add debug/trace output, depending on option order.Location:
src/init/common.cpp:73(head).🔵 Suggestion · Including an active category lowers its threshold from trace to debug
Including an already-active category now drops its trace output, although the release note describes only re-enabling categories.
Location:
src/rpc/node.cpp:225(head).🔵 Suggestion · An
infothreshold changes category status and startup defaultsWith
-debug=<cat> -loglevel=info, category status, debug-log shrinking, and GUI details change at startup, despite unchanged log output.Location:
src/init/common.cpp:129(head).🔵 Suggestion ·
logging.husesstd::unordered_mapwithout including its headerLogRateLimiterstill declares anstd::unordered_mapmember, sologging.hnow relies on a transitive include.Location:
src/logging.h:27(base).🔵 Suggestion · Test category-specific debug selection with global log levels
logging_tests.cpp:256 still tests the global level only with bare -debug. No case checks -debug=net with a global trace level, or that a -debugexclude category stays disabled. The no-debug cases only partly cover the disabled-category path.
Location:
src/test/logging_tests.cpp:254(head).Review coverage was incomplete.
Reviewed:
d5df90e17fd1· Full review report · Previous reportspermalink
🚧 At least one of the CI tasks failed.
Task
iwyu: https://github.com/bitcoin/bitcoin/actions/runs/36794620285/job/110155093409LLM reason (✨ experimental): CI failed because
iwyureported an include-issues failure (it edited files and the script intentionally returned non-zero: “Failure generated from IWYU”).Hints
Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:
Possibly due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the
affected test.
An intermittent issue.
Leave a comment here, if you need help tracking down a confusing failure.
🐙 This pull request conflicts with the target branch and needs rebase.
🚧 At least one of the CI tasks failed.
Task
32 bit ARM: https://github.com/bitcoin/bitcoin/actions/runs/41053854619/job/124056320251LLM reason (✨ experimental): CI failed because CTest tried to execute ARM-built test binaries on the host, producing shell syntax errors in four tests.
Hints
Try to run the tests locally, according to the documentation. However, a CI failure may still
happen due to a number of reasons, for example:
Possibly due to a silent merge conflict (the changes in this pull request being
incompatible with the current code in the target branch). If so, make sure to rebase on the latest
commit of the target branch.
A sanitizer issue, which can only be found by compiling with the sanitizer and running the
affected test.
An intermittent issue.
Leave a comment here, if you need help tracking down a confusing failure.