Skip to content

Use atomics for determining whether trace logging is enabled - #35355

Draft
ajtowns wants to merge 14 commits into
bitcoin:masterfrom
ajtowns:202512-log-atomic
Draft

ajtowns wants to merge 14 commits into
bitcoin:masterfrom
ajtowns:202512-log-atomic

Conversation

@ajtowns

@ajtowns ajtowns commented May 22, 2026

Copy link
Copy Markdown
Contributor

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

DrahtBot commented May 22, 2026 •

Copy link
Copy Markdown
Contributor

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.

@ajtowns

ajtowns commented May 22, 2026 •

Copy link
Copy Markdown
Contributor Author

I first mentioned this three years ago at #25203 (comment) and had originally included it in #28318 (comment) . This branch was also linked to in #34038 (comment) Obviously conflicts with #35322.

@ajtowns
ajtowns force-pushed the 202512-log-atomic branch from b9a62d3 to 10e63db Compare May 22, 2026 03:12
ajtowns and others added 5 commits October 23, 2026 15:55
Previously, trace logging for a category was enabled by saying "-debug=net
-loglevel=net:trace" and saying "-loglevel=net:trace" (without "-debug"
or "-debug=net") would silently do nothing. Simplify this so that
"-debug=net" enables debug logging for net and "-trace=net" enables
both debug and trace logging for net.

Note that "-nodebug" and "-notrace" only disable previous "-debug=xxx" and
"-trace=yyy" arguments, so "-debug=mempool -trace=net -nodebug" will cancel
"-debug=mempool", but will still include both debug and trace logs for net.
Also changes the output format, to distinguish debug/trace levels.
Provides `-deprecatedrpc=logging` if old output format is needed.

Co-Authored-By: stickies-v <[email protected]>
Since we have reduced how much log levels can be configured (error,
warning and info are always logged; and only debug and trace levels can
be disabled), we only need two bits per category to store the information.
We can do this with two atomic ints, one for whether -debug is enabled,
and the other for whether -trace is enabled.

This allows `WillLogCategoryLevel` to avoid taking a mutex, potentially
allowing for better performance in the event that multiple threads are
running many LogDebug(..) statements over categories that do not have
-debug enabled (or LogTrace/-trace).
Add m_any_print_callbacks as a cache for !m_print_callbacks.empty(),
and make m_buffered atomic. Makes the Log functions almost completely
free when logging is completely disabled (though unlike for LogDebug
when the category is disabled, arguments are still evaluated).
LogCategoriesString and LogLevelsString don't need access to an object,
so make them static.
Simplifies the util/log.h API so that log enablement does not require
indirection and `ShouldDebugLog` and `ShouldTraceLog` can be fully
inlined at logging call sites.

The category bitmasks become constinit atomic globals, so any usage is
valid from any context, including dynamic initialization of other static
variables, and avoids any dependency on the exact implementation of
`util::log::Log()`.

The redundant `BCLog::Logger::Should*Log` functions are removed.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants