Skip to content

kernel: make logging struct-based#34466

Closed
stickies-v wants to merge 10 commits intobitcoin:masterfrom
stickies-v:2026-01/kernel-struct-logging
Closed

kernel: make logging struct-based#34466
stickies-v wants to merge 10 commits intobitcoin:masterfrom
stickies-v:2026-01/kernel-struct-logging

Conversation

@stickies-v
Copy link
Contributor

The bitcoinkernel library (#27587) exposes functionality to interface with kernel logging. This includes registering callbacks for log statements, level/category filtering, string formatting options, and more.

This PR improves kernel logging by making it struct-based instead of string-based. Rather than adding this functionality onto an already large BCLog::Logger class, the PR first separates log generation (i.e. producing the log message via LogInfo(msg)) from log consumption (i.e. formatting, ratelimiting, printing the message) by moving all log generation code to util/log.h. While not strictly necessary for adding struct-based logging, it better separates concerns and is a prerequisite for follow-up work in #34374 that completely removes kernel's dependency on logging.cpp.

Approach:

  1. Separate log generation: Add a minimal util::log::Dispatcher that is used to generate logs, and forwards struct-based log entries to registered callbacks. No formatting, rate-limiting, or output handling - that's the consumer's responsibility.

  2. Register BCLog::Logger on Dispatcher: Register a callback on the global Dispatcher to receive entries and handle node-specific concerns (formatting, rate-limiting, file output) without changing most of its logic. Route logging macros through this global dispatcher.

  3. Update bitcoinkernel C API: Replace the string-based callback with btck_LogEntry containing full metadata and remove btck_LoggingOptions and related functions.

Note: with this change, bitcoinkernel logging callbacks are no longer routed through BCLog::Logger. This means that they are no longer buffered (i.e. log messages produced before a logging callback is registered) are dropped, and there is no longer a need for btck_logging_disable. In practice, this means users can no longer see the log message produced during the static context initialization.

Carve-out of #34374 to narrow the focus while maintaining tangible benefits (i.e. having struct-based logging).

This is preparatory work for a future commit so SourceLocation can
be used without requiring logging.h.
This is preparatory work for a future commit so Level can
be used without requiring logging.h.
Add a minimal, callback-based dispatcher that forwards struct-based log
entries to registered callbacks. This provides the foundation for
separating log generation from log consumption, enabling different
consumers (node sink, kernel C API) to receive the same log entries.
Preparatory work for a future commit where the logging macros are moved
to the util library. The macros require a single global entry point to
dispatch log entries.

Make BCLog::Logger aware of util::log::Dispatcher by giving it ownership
of a Dispatcher instance which it can register callbacks on.

Currently, only a single sink (i.e. BCLog::Logger) exists, but in future
commits this will change as kernel removes its dependency on logging.cpp
and introduces its own global logging sink.
Log macros now call util::log::g_dispatcher().Log() directly instead of
going through LogInstance(). BCLog::Logger registers a callback on its
Dispatcher to receive entries and handle formatting, rate limiting, and
output.
Expose btck_log_level_get_name and btck_log_category_get_name to allow
library users to get string representations of log levels and categories.

Use constexpr lookup tables as a single source of truth for mapping
btck types to both BCLog equivalents and string names.
Ensure all levels and categories that may be logged by kernel code
are well defined. Necessary preparation for future work to move
from string-based to struct-based logging.
Preparatory work for a future commit where the btck_LogCallback
exposes btck_LogEntry instead of a string.

Contains a lot of move-only changes, consider reviewing with
--color-moved=dimmed-zebra
Replace the string-based logging callback with btck_LogEntry containing
full metadata (message, source location, timestamp, level, category).
This lets consumers format and filter logs as needed.

Use the global log dispatcher instead of BCLog::Logger's buffered
callback system. Messages are discarded when no callbacks are registered.
As logging is struct-based, there is no more need for the user to
configure how logs are printed, so simplify the interface and remove
dead code.
@DrahtBot
Copy link
Contributor

DrahtBot commented Jan 30, 2026

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

Reviews

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

@stickies-v
Copy link
Contributor Author

Didn't see @ryanofsky already opened #34465 just before this, I'll close this for now since there's a lot of duplication.

@stickies-v stickies-v closed this Jan 30, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants