Use atomics for determining whether trace logging is enabled #35355

pull ajtowns wants to merge 14 commits into bitcoin:master from ajtowns:202512-log-atomic changing 24 files +413 −347
  1. ajtowns commented at 12:56 AM on May 22, 2026: 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)
  2. DrahtBot commented at 12:56 AM on May 22, 2026: contributor

    <!--e57a25ab6845829454e8d69fc972939a-->

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

    <!--006a51241073e994b41acfe9ec718e94-->

    External sites

    <!--021abf342d371248e50ceaed478a90ca-->

    Reviews

    See the guideline and AI policy for information on the review process. A summary of reviews will appear here.

    <!--174a7506f384e20aa4161008e828411d-->

    Conflicts

    Reviewers, this pull request conflicts with the following ones:

    • #36268 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36268.svg"></sub> (refactor: prune unused semi-colons by fanquake)
    • #36257 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36257.svg"></sub> (qa: assert_equals -> assert_true/assert_false by hodlinator)
    • #36074 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36074.svg"></sub> (scripted-diff: [test] Add util/check.h includes for assertions by maflcko)
    • #35911 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35911.svg"></sub> (Warn on and add missing [[noreturn]] by fanquake)
    • #35387 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35387.svg"></sub> (logging: make trace logging easily usable by ryanofsky)
    • #34775 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34775.svg"></sub> (kernel: allow only one global logging connection by stickies-v)
    • #10102 <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/10102.svg"></sub> (Multiprocess bitcoin 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-->

  3. ajtowns commented at 1:00 AM on May 22, 2026: contributor

    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 (review) Obviously conflicts with #35322.

  4. ajtowns force-pushed on May 22, 2026
  5. DrahtBot added the label CI failed on May 22, 2026
  6. DrahtBot removed the label CI failed on May 22, 2026
  7. DrahtBot added the label Needs rebase on Jun 22, 2026
  8. logging: replace -loglevel arg with -trace
    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.
    2708138613
  9. rpc: Update logging RPC to support trace level debugging
    Also changes the output format, to distinguish debug/trace levels.
    Provides `-deprecatedrpc=logging` if old output format is needed.
    
    Co-Authored-By: stickies-v <stickies-v@protonmail.com>
    730154e28c
  10. [doc] Release notes for user-visible logging changes 649b0503ad
  11. logging: replace global and category log levels with atomic int
    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).
    0572c28fe3
  12. logging: Make sure BCLog::Logger members are atomic or guarded b57b2169e0
  13. logging: Make Enabled() lock-free
    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).
    9ff433e935
  14. [move-only] logging: Move Logger member variables together eadc695e7f
  15. logging: Replace Enable/Trace/DisableCategory(str) with SetCategoryLogLevel(str, level) 91c03acd98
  16. logging: Drop unused SetCategoryLogLevel(str,str) a59b95ba3c
  17. logging: Drop Enable/Trace/DisableCategory aliases 79c1d41159
  18. logging: Drop WillLogCategory* in favour of Logger::ShouldDebugLog and ShouldTraceLog 3691d4302d
  19. util/log.h: Move internal functions into detail namespace a9006aa17e
  20. logging: Make some functions static
    LogCategoriesString and LogLevelsString don't need access to an object,
    so make them static.
    2d4996e20e
  21. util/log.h: Make debug/trace enablement global and atomic
    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.
    36586ec1b3
  22. ajtowns force-pushed on Oct 23, 2026
  23. DrahtBot removed the label Needs rebase on Oct 23, 2026

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-10-30 09:40 UTC