WIP: Use atomics for determining whether trace logging is enabled #35355

Draft
ajtowns wants to merge 14 commits from 202512-log-atomic into master AGit
ajtowns commented 2026-05-22 00:56:36 +00:00 (Migrated from github.com)

Followup of #28318, #34038, and #34806 to simplify and optimize the logging-disabled path for LogTrace().

Removes the global log level (m_log_level) and the unordered_map from category to level (m_category_log_levels) in favour of a single atomic<uint64_t> m_trace_categories, and reduces ShouldDebugLog and ShouldTraceLog to lock-free checks on a single atomic. Also makes Enabled() lock-free, to waste a little less time in the case where debugging is completely disabled.

Does a bit of clean up as well:

  • guards m_file_path and provides accessors
  • makes other configuration variables atomic, rather than "write-before-createing-threads-then-treat-as-read-only"
  • consolidates duplicated functions (SetCategoryLogLevel, Enable/Trace/DisableCategory, WillLogCategory)
Followup of #28318, #34038, and #34806 to simplify and optimize the logging-disabled path for `LogTrace()`. Removes the global log level (`m_log_level`) and the unordered_map from category to level (`m_category_log_levels`) in favour of a single `atomic<uint64_t> m_trace_categories`, and reduces `ShouldDebugLog` and `ShouldTraceLog` to lock-free checks on a single atomic. Also makes `Enabled()` lock-free, to waste a little less time in the case where debugging is completely disabled. Does a bit of clean up as well: * guards `m_file_path` and provides accessors * makes other configuration variables atomic, rather than "write-before-createing-threads-then-treat-as-read-only" * consolidates duplicated functions (SetCategoryLogLevel, Enable/Trace/DisableCategory, WillLogCategory)
DrahtBot commented 2026-05-22 00:56:44 +00:00 (Migrated from github.com)

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:

  • #37034 (init: avoid pointing to a disabled debug log on failure by l0rinc)
  • #36904 (log: Remove trailing and embedded newlines from log messages by maflcko)
  • #36768 (kernel: Dispatch log callbacks with btcsignals by ryanofsky)
  • #36434 (http: support unix sockets by pinheadmz)
  • #36257 (qa: assert_equal -> assert_true/assert_false by hodlinator)
  • #36187 (http: Restore requirement of IPv4 bind success by hodlinator)
  • #35911 (Warn on and add missing [[noreturn]] by fanquake)
  • #35387 (logging: make trace logging easily usable by ryanofsky)
  • #35322 (logging: streamline Logger state and drop redundant methods by ryanofsky)
  • #34778 (logging: rewrite macros to enforce restrictions at compile-time, improve efficiency and usability by ryanofsky)
  • #34775 (kernel: allow only one global logging connection by stickies-v)
  • #33847 (kernel: Attach logging connections to contexts, per-connection log levels by ryanofsky)
  • #29256 (log, refactor: Allow log macros to accept context arguments by ryanofsky)
  • #17783 (common: Disallow calling IsArgSet() on ALLOW_LIST options by ryanofsky)
  • #17580 (refactor: Add ALLOW_LIST flags and enforce usage in CheckArgFlags by ryanofsky)

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.

<!--e57a25ab6845829454e8d69fc972939a--> The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. <!--006a51241073e994b41acfe9ec718e94--> ### External sites * Code coverage & benchmarks: https://corecheck.dev/bitcoin/bitcoin/pulls/35355 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/35355.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/35355#author-ralph <!--021abf342d371248e50ceaed478a90ca--> ### Reviews See [the guideline](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#code-review) and [AI policy](https://github.com/bitcoin/bitcoin/blob/master/doc/AI_POLICY.md) for information on the review process. A summary of reviews will appear here. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#37034](https://github.com/bitcoin/bitcoin/pull/37034) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/37034.svg"></sub> (init: avoid pointing to a disabled debug log on failure by l0rinc) * [#36904](https://github.com/bitcoin/bitcoin/pull/36904) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36904.svg"></sub> (log: Remove trailing and embedded newlines from log messages by maflcko) * [#36768](https://github.com/bitcoin/bitcoin/pull/36768) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36768.svg"></sub> (kernel: Dispatch log callbacks with btcsignals by ryanofsky) * [#36434](https://github.com/bitcoin/bitcoin/pull/36434) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36434.svg"></sub> (http: support unix sockets by pinheadmz) * [#36257](https://github.com/bitcoin/bitcoin/pull/36257) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36257.svg"></sub> (qa: assert_equal -> assert_true/assert_false by hodlinator) * [#36187](https://github.com/bitcoin/bitcoin/pull/36187) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36187.svg"></sub> (http: Restore requirement of IPv4 bind success by hodlinator) * [#35911](https://github.com/bitcoin/bitcoin/pull/35911) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35911.svg"></sub> (Warn on and add missing `[[noreturn]]` by fanquake) * [#35387](https://github.com/bitcoin/bitcoin/pull/35387) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35387.svg"></sub> (logging: make trace logging easily usable by ryanofsky) * [#35322](https://github.com/bitcoin/bitcoin/pull/35322) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35322.svg"></sub> (logging: streamline Logger state and drop redundant methods by ryanofsky) * [#34778](https://github.com/bitcoin/bitcoin/pull/34778) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34778.svg"></sub> (logging: rewrite macros to enforce restrictions at compile-time, improve efficiency and usability by ryanofsky) * [#34775](https://github.com/bitcoin/bitcoin/pull/34775) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34775.svg"></sub> (kernel: allow only one global logging connection by stickies-v) * [#33847](https://github.com/bitcoin/bitcoin/pull/33847) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/33847.svg"></sub> (kernel: Attach logging connections to contexts, per-connection log levels by ryanofsky) * [#29256](https://github.com/bitcoin/bitcoin/pull/29256) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/29256.svg"></sub> (log, refactor: Allow log macros to accept context arguments by ryanofsky) * [#17783](https://github.com/bitcoin/bitcoin/pull/17783) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/17783.svg"></sub> (common: Disallow calling IsArgSet() on ALLOW_LIST options by ryanofsky) * [#17580](https://github.com/bitcoin/bitcoin/pull/17580) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/17580.svg"></sub> (refactor: Add ALLOW_LIST flags and enforce usage in CheckArgFlags by ryanofsky) 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. <!--5faf32d7da4f0f540f40219e4f7537a3-->
ajtowns commented 2026-05-22 01:00:37 +00:00 (Migrated from github.com)
I first mentioned this three years ago at https://github.com/bitcoin/bitcoin/pull/25203#issuecomment-1401126785 and had originally included it in https://github.com/bitcoin/bitcoin/pull/28318#issuecomment-1701211340 . This branch was also linked to in https://github.com/bitcoin/bitcoin/pull/34038#discussion_r2735011334 Obviously conflicts with #35322.
DrahtBot commented 2026-06-22 14:11:29 +00:00 (Migrated from github.com)

🐙 This pull request conflicts with the target branch and needs rebase.

<!--cf906140f33d8803c4a75a2196329ecb--> 🐙 This pull request conflicts with the target branch and [needs rebase](https://github.com/bitcoin/bitcoin/blob/master/CONTRIBUTING.md#rebasing-changes).
DrahtBot commented 2026-09-19 02:07:30 +00:00 (Migrated from github.com)

⌛ There hasn't been much activity lately and the patch still needs rebase. What is the status here?

  • Is it still relevant? ➡️ Please solve the conflicts to make it ready for review and to ensure the CI passes.
  • Is it no longer relevant? ➡️ Please close.
  • Did the author lose interest or time to work on this? ➡️ Please close it and mark it with one of the labels 'Up for grabs' or 'Insufficient Review', so that it can be picked up in the future.
<!--13523179cfe9479db18ec6c5d236f789--> ⌛ There hasn't been much activity lately and the patch still needs rebase. What is the status here? * Is it still relevant? ➡️ Please solve the conflicts to make it ready for review and to ensure the CI passes. * Is it no longer relevant? ➡️ Please close. * Did the author lose interest or time to work on this? ➡️ Please close it and mark it with one of the labels 'Up for grabs' or 'Insufficient Review', so that it can be picked up in the future.
First-time contributor

2 additional verified findings in the full report.

🟠 Minor · Enabling walletdb tracing with the new RPC argument does not trace wallets that are already open

The RPC reports walletdb as traced, but wallets that were already loaded produce no SQL trace output until they are reloaded.

Location: src/rpc/node.cpp:282 (head).

🟠 Minor · MakeNoLogFileContext leaves debug and trace logging on for all categories

Fuzz targets and benchmarks built on it now format and discard every debug/trace message and run debug-only bookkeeping that base skipped.

Location: src/test/util/setup_common.cpp:167 (head).

🟠 Minor · Kernel log masks can be left stale when SetMinLevel races with connection create/destroy

A libbitcoinkernel client calling these from different threads can lose Debug/Trace entries for an active subscriber until the next configuration change.

Location: src/kernel/bitcoinkernel.cpp:548 (head).

🟠 Minor · Concurrent logging RPC updates can leave a category traced but not debug-logged

Overlapping updates to the same category can leave it reported as trace while its debug messages are dropped, until it is reconfigured.

Location: src/logging.cpp:565 (head).

🔵 Suggestion · logging include/debug now removes trace logging from categories that were at trace

Callers that list an already-traced category to enable debug logging silently lose its trace output, and neither the help text nor the release note says so.

Location: src/rpc/node.cpp:279 (head).

🔵 Suggestion · Release note says wallet SQL is the only supported trace logging

cmpctblock and ipc also emit trace logs, so the note understates what -trace turns on.

Location: doc/release-notes-34038.md:4 (head).


Reviewed: 36586ec1b397 · Full review report · Previous reports

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:6f232c871bca65facadc248ad7ecd634f87b0b5f6936bc30593af890589888f1:0cfbbc826fdd30e42f35c9a5b7d389c0902089c94f2400654bb32123e69823f6 --> <!-- ralph-base:6ed5aad06111177c39f9b9f07a52634476380ae1 --> <!-- ralph-head:36586ec1b397890a78cfaeda2bee7543131b569e --> <!-- ralph-case:1f9a0769cfd7f180c110b6bf6952a204a448beb4b88e6aa3cc4b6360924af4be --> [2 additional verified findings in the full report](<https://review.fish.foo/traces/35355-36586ec1b397890a78cfaeda2bee7543131b569e-1689-3.html>). **🟠 Minor · Enabling walletdb tracing with the new RPC argument does not trace wallets that are already open** The RPC reports walletdb as traced, but wallets that were already loaded produce no SQL trace output until they are reloaded. Location: [`src/rpc/node.cpp:282`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/36586ec1b397890a78cfaeda2bee7543131b569e/src/rpc/node.cpp#L282>) (head). **🟠 Minor · MakeNoLogFileContext leaves debug and trace logging on for all categories** Fuzz targets and benchmarks built on it now format and discard every debug/trace message and run debug-only bookkeeping that base skipped. Location: [`src/test/util/setup_common.cpp:167`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/36586ec1b397890a78cfaeda2bee7543131b569e/src/test/util/setup_common.cpp#L167>) (head). **🟠 Minor · Kernel log masks can be left stale when SetMinLevel races with connection create/destroy** A libbitcoinkernel client calling these from different threads can lose Debug/Trace entries for an active subscriber until the next configuration change. Location: [`src/kernel/bitcoinkernel.cpp:548`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/36586ec1b397890a78cfaeda2bee7543131b569e/src/kernel/bitcoinkernel.cpp#L548>) (head). **🟠 Minor · Concurrent logging RPC updates can leave a category traced but not debug-logged** Overlapping updates to the same category can leave it reported as trace while its debug messages are dropped, until it is reconfigured. Location: [`src/logging.cpp:565`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/36586ec1b397890a78cfaeda2bee7543131b569e/src/logging.cpp#L565>) (head). **🔵 Suggestion · `logging` include/debug now removes trace logging from categories that were at trace** Callers that list an already-traced category to enable debug logging silently lose its trace output, and neither the help text nor the release note says so. Location: [`src/rpc/node.cpp:279`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/36586ec1b397890a78cfaeda2bee7543131b569e/src/rpc/node.cpp#L279>) (head). **🔵 Suggestion · Release note says wallet SQL is the only supported trace logging** cmpctblock and ipc also emit trace logs, so the note understates what -trace turns on. Location: [`doc/release-notes-34038.md:4`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/36586ec1b397890a78cfaeda2bee7543131b569e/doc/release-notes-34038.md#L4>) (head). <!-- Review tier: sensitive --> --- Reviewed: [`36586ec1b397`](<https://git.fish.foo/bitcoin/bitcoin/commit/36586ec1b397890a78cfaeda2bee7543131b569e>) · [Full review report](<https://review.fish.foo/traces/35355-36586ec1b397890a78cfaeda2bee7543131b569e-1689-3.html>) · [Previous reports](<https://review.fish.foo/traces/35355/>)
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!35355
No description provided.