kernel: use struct-based logging and simplify logging interface #34374

Manually merged
stickies-v merged 6 commits from 2026-01/kernel-logging-layering-predicate into master 2026-10-08 15:08:19 +00:00 AGit
stickies-v commented 2026-01-21 19:56:44 +00:00 (Migrated from github.com)

tl;dr: kernel logging is cumbersome. This PR delivers log entries as a struct instead of a formatted string, and simplifies the kernel logging interface. Closes #34062.

Motivation

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

Kernel logging has a few problems:

  • callbacks operate on formatted strings, so users need to parse the string to get the timestamp, category, level, ... based on which options are set. This is cumbersome, brittle, and inefficient.
  • the filtering interface is not really intuitive, requiring users to call combinations of btck_logging_set_level_category and btck_logging_enable_category when they want to produce debug or trace logs. The level/category system makes sense for node, because it directly controls what gets written to disk and stdout, and there are quite a lot more categories producing logs. Kernel doesn't really need this - users control what happens to the logs, and can do any filtering/manipulation in the callback they provide.
  • the node logging infrastructure has quite a bit more functionality than is necessary for a library, including ratelimiting, log formatting, outputting, buffering, ... This introduces unnecessary code and interface complexity.

Approach

Log generation (util/log.h: macros, util::log::Entry, and the ShouldDebugLog/ShouldTraceLog/Log hooks an application must provide) was already separated from log handling (logging.h) in #34465 and subsequent PRs. This PR gives bitcoinkernel its own implementation of those hooks, so it no longer depends on logging.cpp, and upgrades the C API to deliver struct-based entries. Node logging is not changed.

  1. Preparatory work: expose the missing levels and categories (WARNING, ERROR, TXPACKAGES, LOCK) and the btck_LogEntry struct, add Name() helpers to the C++ wrapper, and remove btck_logging_set_options and btck_logging_disable (with struct-based entries there is no kernel-side format to configure, and without buffering there is nothing to disable).
  2. Add KernelLogger: a kernel-owned backend that holds the registered callbacks and the minimum level, and converts each util::log::Entry into a btck_LogEntry. Introduced in a separate commit to keep the scope of the behaviour-changing commit smaller.
  3. Update the bitcoinkernel C API: btck_LogCallback receives a btck_LogEntry instead of a string, the util::log hooks are implemented on KernelLogger, logging.cpp is dropped from the kernel build, and the logging configuration interface is reduced to btck_logging_set_min_level().

Behaviour changes for consumers (also described in each commit message):

  • Logging callbacks deliver structs instead of formatted strings, and the entire logging interface is simplified.
  • Filtering is levels-based only and can also filter Info and above. Debug/Trace apply to all categories, consumers can filter on btck_LogEntry::category (but string formatting is done for the entire level, instead of per enabled category).
  • Entries logged before the first connection are dropped instead of buffered.
  • Messages are delivered unescaped and without a trailing newline.

Appendix

bitcoinkernel C logging interface

typedef struct {
    const char* message;    //! Log message text.
    size_t message_len;
    const char* thread_name;    //! Name of the thread that produced the log message.
    size_t thread_name_len; 
    int64_t timestamp_ns;    //! Wall-clock time when the entry was logged, in nanoseconds since the Unix epoch.
    int64_t mocktime_s;    //! Mock time in effect when the entry was logged, in seconds since the Unix epoch, or 0 if no mock time is set (see btck_set_mock_time).
    const char* file_name;    //! Name of the source file that produced the log statement.
    size_t file_name_len;
    const char* function_name;    //! Name of the function that produced the log message.
    size_t function_name_len;
    uint32_t line;    //! Line number of the source code that produced the log statement.
    btck_LogLevel level;    //! Log severity level.
    btck_LogCategory category;    //! Log category.
} btck_LogEntry;

typedef void (*btck_LogCallback)(void* user_data, const btck_LogEntry* entry);

BITCOINKERNEL_API void btck_logging_set_min_level(btck_LogLevel level);

BITCOINKERNEL_API btck_LoggingConnection* BITCOINKERNEL_WARN_UNUSED_RESULT btck_logging_connection_create(
    btck_LogCallback log_callback,
    void* user_data,
    btck_DestroyCallback user_data_destroy_callback) BITCOINKERNEL_ARG_NONNULL(1);

BITCOINKERNEL_API void btck_logging_connection_destroy(btck_LoggingConnection* logging_connection);
tl;dr: kernel logging is cumbersome. This PR delivers log entries as a struct instead of a formatted string, and simplifies the kernel logging interface. Closes #34062. ## Motivation The `bitcoinkernel` library (#27587) [exposes](https://github.com/bitcoin/bitcoin/blob/b26762bdcb941096ccc6f53c3fb5a7786ad735e7/src/kernel/bitcoinkernel.h#L698-L778) functionality to interface with the kernel logging. This includes registering callbacks for log statements, level/category filtering, string formatting options, and more. Kernel logging has a few problems: - [callbacks](https://github.com/bitcoin/bitcoin/blob/b26762bdcb941096ccc6f53c3fb5a7786ad735e7/src/kernel/bitcoinkernel.h#L304) operate on formatted strings, so users need to parse the string to get the timestamp, category, level, ... based on which options are set. This is cumbersome, brittle, and inefficient. - the [filtering interface](https://github.com/bitcoin/bitcoin/blob/b26762bdcb941096ccc6f53c3fb5a7786ad735e7/src/kernel/bitcoinkernel.h#L737) is not really intuitive, requiring users to call combinations of `btck_logging_set_level_category` and `btck_logging_enable_category` when they want to produce `debug` or `trace` logs. The level/category system makes sense for node, because it directly controls what gets written to disk and stdout, and there are quite a lot more categories producing logs. Kernel doesn't really need this - users control what happens to the logs, and can do any filtering/manipulation in the callback they provide. - the node logging infrastructure has quite a bit more functionality than is necessary for a library, including ratelimiting, log formatting, outputting, buffering, ... This introduces unnecessary code and interface complexity. ## Approach Log generation (`util/log.h`: macros, `util::log::Entry`, and the `ShouldDebugLog`/`ShouldTraceLog`/`Log` hooks an application must provide) was already separated from log handling (`logging.h`) in #34465 and subsequent PRs. This PR gives `bitcoinkernel` its own implementation of those hooks, so it no longer depends on `logging.cpp`, and upgrades the C API to deliver struct-based entries. Node logging is not changed. 1. **Preparatory work**: expose the missing levels and categories (`WARNING`, `ERROR`, `TXPACKAGES`, `LOCK`) and the `btck_LogEntry` struct, add `Name()` helpers to the C++ wrapper, and remove `btck_logging_set_options` and `btck_logging_disable` (with struct-based entries there is no kernel-side format to configure, and without buffering there is nothing to disable). 2. **Add `KernelLogger`**: a kernel-owned backend that holds the registered callbacks and the minimum level, and converts each `util::log::Entry` into a `btck_LogEntry`. Introduced in a separate commit to keep the scope of the behaviour-changing commit smaller. 3. **Update the bitcoinkernel C API**: `btck_LogCallback` receives a `btck_LogEntry` instead of a string, the `util::log` hooks are implemented on `KernelLogger`, `logging.cpp` is dropped from the kernel build, and the logging configuration interface is reduced to `btck_logging_set_min_level()`. Behaviour changes for consumers (also described in each commit message): - Logging callbacks deliver structs instead of formatted strings, and the entire logging interface is simplified. - Filtering is levels-based only and can also filter `Info` and above. `Debug`/`Trace` apply to all categories, consumers can filter on `btck_LogEntry::category` (but string formatting is done for the entire level, instead of per enabled category). - Entries logged before the first connection are dropped instead of buffered. - Messages are delivered unescaped and without a trailing newline. ## Appendix ### `bitcoinkernel` C logging interface ```c typedef struct { const char* message; //! Log message text. size_t message_len; const char* thread_name; //! Name of the thread that produced the log message. size_t thread_name_len; int64_t timestamp_ns; //! Wall-clock time when the entry was logged, in nanoseconds since the Unix epoch. int64_t mocktime_s; //! Mock time in effect when the entry was logged, in seconds since the Unix epoch, or 0 if no mock time is set (see btck_set_mock_time). const char* file_name; //! Name of the source file that produced the log statement. size_t file_name_len; const char* function_name; //! Name of the function that produced the log message. size_t function_name_len; uint32_t line; //! Line number of the source code that produced the log statement. btck_LogLevel level; //! Log severity level. btck_LogCategory category; //! Log category. } btck_LogEntry; typedef void (*btck_LogCallback)(void* user_data, const btck_LogEntry* entry); BITCOINKERNEL_API void btck_logging_set_min_level(btck_LogLevel level); BITCOINKERNEL_API btck_LoggingConnection* BITCOINKERNEL_WARN_UNUSED_RESULT btck_logging_connection_create( btck_LogCallback log_callback, void* user_data, btck_DestroyCallback user_data_destroy_callback) BITCOINKERNEL_ARG_NONNULL(1); BITCOINKERNEL_API void btck_logging_connection_destroy(btck_LoggingConnection* logging_connection); ```
DrahtBot commented 2026-01-21 19:56:55 +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.

Type Reviewers
ACK w0xlt, musaHaruna, sedited
Concept ACK yuvicc
Stale ACK ryanofsky

If your review is incorrectly listed, please copy-paste <!--meta-tag:bot-skip--> into the comment that the bot should ignore.

Conflicts

Reviewers, this pull request conflicts with the following ones:

  • #36353 (kernel: don't reuse cached CheckBlock results across params by FlashWayne)
  • #36326 (kernel: Use typed errors for fatal and flush error notifications by arejula27)
  • #36000 (validation: prefetch blocks while connecting by l0rinc)
  • #35641 (kernel: Add script evaluation tracer by sedited)
  • #35322 (logging: streamline Logger state and drop redundant methods by ryanofsky)
  • #35187 (kernel: Block validation without a complete UTXO set by sedited)
  • #33847 (kernel: Attach logging connections to contexts, per-connection log levels by ryanofsky)
  • #28690 (build: Introduce internal kernel library by sedited)

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/34374 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/34374.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/34374#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. | Type | Reviewers | | ---- | --------- | | ACK | [w0xlt](https://github.com/bitcoin/bitcoin/pull/34374#pullrequestreview-5397632298), [musaHaruna](https://github.com/bitcoin/bitcoin/pull/34374#pullrequestreview-5400382743), [sedited](https://github.com/bitcoin/bitcoin/pull/34374#pullrequestreview-5442970908) | | Concept ACK | [yuvicc](https://github.com/bitcoin/bitcoin/pull/34374#issuecomment-6035772271) | | Stale ACK | [ryanofsky](https://github.com/bitcoin/bitcoin/pull/34374#pullrequestreview-5359228031) | If your review is incorrectly listed, please copy-paste <code>&lt;!--meta-tag:bot-skip--&gt;</code> into the comment that the bot should ignore. <!--174a7506f384e20aa4161008e828411d--> ### Conflicts Reviewers, this pull request conflicts with the following ones: * [#36353](https://github.com/bitcoin/bitcoin/pull/36353) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36353.svg"></sub> (kernel: don't reuse cached CheckBlock results across params by FlashWayne) * [#36326](https://github.com/bitcoin/bitcoin/pull/36326) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36326.svg"></sub> (kernel: Use typed errors for fatal and flush error notifications by arejula27) * [#36000](https://github.com/bitcoin/bitcoin/pull/36000) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36000.svg"></sub> (validation: prefetch blocks while connecting by l0rinc) * [#35641](https://github.com/bitcoin/bitcoin/pull/35641) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35641.svg"></sub> (kernel: Add script evaluation tracer by sedited) * [#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) * [#35187](https://github.com/bitcoin/bitcoin/pull/35187) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35187.svg"></sub> (kernel: Block validation without a complete UTXO set by sedited) * [#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) * [#28690](https://github.com/bitcoin/bitcoin/pull/28690) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/28690.svg"></sub> (build: Introduce internal kernel library by sedited) 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-->
DrahtBot commented 2026-01-21 20:16:21 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task tidy: https://github.com/bitcoin/bitcoin/actions/runs/21223722631/job/61065673932
LLM reason (✨ experimental): Linker errors due to unresolved detail::InitLogger() from logging.cpp.o, causing the build/test script to fail.

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.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `tidy`: https://github.com/bitcoin/bitcoin/actions/runs/21223722631/job/61065673932</sub> <sub>LLM reason (✨ experimental): Linker errors due to unresolved detail::InitLogger() from logging.cpp.o, causing the build/test script to fail.</sub> <details><summary>Hints</summary> 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. </details>
ajtowns (Migrated from github.com) reviewed 2026-01-23 10:50:29 +00:00
ajtowns (Migrated from github.com) commented 2026-01-23 10:48:40 +00:00

This seems like a regression. With this approach you can't assume LogInfo called during global initialization won't be called prior to InitLogger(), as it now only needs to pass through GetDispatcher() and g_dispatcher().

As far as I can see, g_dispatcher() should just have independent implementations for bitcoin-kernel vs bitcoind, bitcoin-qt, bitcoin-cli etc.

This seems like a regression. With this approach you can't assume `LogInfo` called during global initialization won't be called prior to `InitLogger()`, as it now only needs to pass through `GetDispatcher()` and `g_dispatcher()`. As far as I can see, `g_dispatcher()` should just have independent implementations for bitcoin-kernel vs bitcoind, bitcoin-qt, bitcoin-cli etc.
ajtowns (Migrated from github.com) commented 2026-01-23 10:50:03 +00:00

Jumping through a std::function when we could just be checking an atomic bitfield doesn't seem like a good approach.

Jumping through a `std::function` when we could just be checking an atomic bitfield doesn't seem like a good approach.
stickies-v (Migrated from github.com) reviewed 2026-01-23 14:03:28 +00:00
stickies-v (Migrated from github.com) commented 2026-01-23 14:03:28 +00:00

when we could just be checking an atomic bitfield

I think that's only possible if we either 1) have util::log::Dispatcher expose all of BCLog::Logger's category/level setting logic, or 2) make Dispatcher::WillLog virtual and let each logging sink create its own Dispatcher subtype. Were you thinking of another approach?

I don't think either approach is warranted to justify the minimal overhead that std::function incurs. 1) would just make Dispatcher's scope way too big in my view, and 2) would require inheritance and still have some (although probably less) overhead from the virtual table lookup.

> when we could just be checking an atomic bitfield I think that's only possible if we either 1) have `util::log::Dispatcher` expose all of `BCLog::Logger`'s category/level setting logic, or 2) make `Dispatcher::WillLog` `virtual` and let each logging sink create its own `Dispatcher` subtype. Were you thinking of another approach? I don't think either approach is warranted to justify the minimal overhead that `std::function` incurs. 1) would just make `Dispatcher`'s scope way too big in my view, and 2) would require inheritance and still have some (although probably less) overhead from the virtual table lookup.
stickies-v (Migrated from github.com) reviewed 2026-01-23 16:37:22 +00:00
stickies-v (Migrated from github.com) commented 2026-01-23 16:37:22 +00:00

Yes, I think that's a much better approach, thanks for the suggestion. I'll incorpocate that and update shortly.

As far as I can see, g_dispatcher() should just have independent implementations for bitcoin-kernel vs bitcoind, bitcoin-qt, bitcoin-cli etc.

I think it should suffice to declare g_dispatcher() in util/log.h, and have one implementation in bitcoinkernel.cpp, and one in logging.cpp (which just calls LogInstance().GetDispatcher()). All non-kernel binaries can (at least for now) just use the same implementation, I think? Lmk if I'm misunderstanding something.

Yes, I think that's a much better approach, thanks for the suggestion. I'll incorpocate that and update shortly. > As far as I can see, `g_dispatcher()` should just have independent implementations for bitcoin-kernel vs bitcoind, bitcoin-qt, bitcoin-cli etc. I think it should suffice to declare `g_dispatcher()` in `util/log.h`, and have one implementation in `bitcoinkernel.cpp`, and one in `logging.cpp` (which just calls `LogInstance().GetDispatcher()`). All non-kernel binaries can (at least for now) just use the same implementation, I think? Lmk if I'm misunderstanding something.
DrahtBot commented 2026-01-26 18:51:36 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/21368395725/job/61506234805
LLM reason (✨ experimental): IWYU reported include-what-you-use changes and caused the CI step to fail.

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.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `iwyu`: https://github.com/bitcoin/bitcoin/actions/runs/21368395725/job/61506234805</sub> <sub>LLM reason (✨ experimental): IWYU reported include-what-you-use changes and caused the CI step to fail.</sub> <details><summary>Hints</summary> 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. </details>
stickies-v commented 2026-01-26 20:36:45 +00:00 (Migrated from github.com)

Force-pushed to:

  • address merge conflict from https://github.com/bitcoin/bitcoin/pull/33822
  • address @ajtowns comment: removed SetDispatcher and GetDispatcher and instead implemented different g_dispatcher functions for logging.cpp and bitcoinkernel.cpp. This required quite a bit of code re-organization, including splitting out the kernel API changes into multiple commits (one for structured logging, one for levels-based filtering) - which I think improve the overall commit organization, but will make range-diff reviews pretty hard for this force-push.
  • improved logging callback handling: checking for nullptr and catching exceptions.
  • use BCLog::Logger::Enabled() in the filter function
  • address drahtbot linter suggestions, incl typo and named args, and a couple of other small touch-ups here and there.
Force-pushed to: - address merge conflict from https://github.com/bitcoin/bitcoin/pull/33822 - address @ajtowns [comment](https://github.com/bitcoin/bitcoin/pull/34374#discussion_r2720705368): removed `SetDispatcher` and `GetDispatcher` and instead implemented different `g_dispatcher` functions for `logging.cpp` and `bitcoinkernel.cpp`. This required quite a bit of code re-organization, including splitting out the kernel API changes into multiple commits (one for structured logging, one for levels-based filtering) - which I think improve the overall commit organization, but will make range-diff reviews pretty hard for this force-push. - improved logging callback handling: checking for nullptr and catching exceptions. - use `BCLog::Logger::Enabled()` in the `filter` function - address drahtbot linter suggestions, incl typo and named args, and a couple of other small touch-ups here and there.
stickies-v (Migrated from github.com) reviewed 2026-01-26 20:37:11 +00:00
stickies-v (Migrated from github.com) commented 2026-01-26 20:37:11 +00:00

I've changed the approach to your suggestion, so marking this as resolved.

I've changed the approach to your suggestion, so marking this as resolved.
purpleKarrot commented 2026-01-27 05:32:52 +00:00 (Migrated from github.com)

I doubt that there is an actual use case for multiple logging connections. The logger is global internally and can be handled as global by clients as well (no need for userdata). Consider modelling the logging API after set_terminate in the C++ standard library:

typedef void (*btck_LogHandler)(btck_LogEntry const* entry);

BITCOINKERNEL_API btck_LogHandler btck_set_log(btck_LogHandler handler);

I also think that clients are able to apply their own filtering, so the btck_logging_set_min_level function can be removed. It is worth noting that this function was never designed to filter per-connection, which was leaking the implementation detail that the logger is global.

I doubt that there is an actual use case for multiple logging connections. The logger is global internally and can be handled as global by clients as well (no need for userdata). Consider modelling the logging API after [set_terminate](https://en.cppreference.com/w/cpp/error/set_terminate.html) in the C++ standard library: ```c typedef void (*btck_LogHandler)(btck_LogEntry const* entry); BITCOINKERNEL_API btck_LogHandler btck_set_log(btck_LogHandler handler); ``` I also think that clients are able to apply their own filtering, so the `btck_logging_set_min_level` function can be removed. It is worth noting that this function was never designed to filter per-connection, which was leaking the implementation detail that the logger is global.
purpleKarrot (Migrated from github.com) reviewed 2026-01-27 05:47:59 +00:00
purpleKarrot (Migrated from github.com) commented 2026-01-27 05:47:59 +00:00

I interpret that as "We have to use a singleton here because of other singletons".
Now everybody should understand why singletons are viral and therefore should be forbidden. I have zero tolerance for such code.

I interpret that as "We have to use a singleton here because of other singletons". Now everybody should understand why singletons are *viral* and therefore should be forbidden. I have zero tolerance for such code.
stickies-v (Migrated from github.com) reviewed 2026-01-27 11:24:59 +00:00
stickies-v (Migrated from github.com) commented 2026-01-27 11:24:59 +00:00

I have zero tolerance for such code.

I'm sorry to hear. What do you suggest as an alternative?

> I have zero tolerance for such code. I'm sorry to hear. What do you suggest as an alternative?
stickies-v commented 2026-01-27 11:28:56 +00:00 (Migrated from github.com)

I doubt that there is an actual use case for multiple logging connections.

I think this concern is orthogonal to the changes proposed in this PR, so I think that conversation might be better had e.g. in #30342 ?

I also think that clients are able to apply their own filtering, so the btck_logging_set_min_level function can be removed.

They are, but I think it's reasonable to offer a (minimal) way for clients to avoid paying for overhead (mostly string formatting, lambda evaluation, and expensive operations like memory usage calculations) for logs from levels that will be dropped anyway. Keeping this as-is for now, happy to reconsider if this is a general point of contention.

> I doubt that there is an actual use case for multiple logging connections. I think this concern is orthogonal to the changes proposed in this PR, so I think that conversation might be better had e.g. in #30342 ? > I also think that clients are able to apply their own filtering, so the `btck_logging_set_min_level` function can be removed. They are, but I think it's reasonable to offer a (minimal) way for clients to avoid paying for overhead (mostly string formatting, lambda evaluation, and expensive operations like memory usage calculations) for logs from levels that will be dropped anyway. Keeping this as-is for now, happy to reconsider if this is a general point of contention.
ajtowns (Migrated from github.com) reviewed 2026-01-28 04:22:15 +00:00
ajtowns (Migrated from github.com) commented 2026-01-28 04:22:15 +00:00

I don't know that Dispatcher needs to make the category/levels modifiable -- that seems like something the g_dispatcher implementation could handle via marking them as protected in Dispatcher. They should be directly testable via Dispatcher, so that disabled debug/trace categories are trivial to test, without needing indirection.

I think your latest implementation (a4e8f3c8d6763a95c1804fc40781b379e6127b66) is dangerously wrong, btw -- LogInfo() etc currently always evaluate their arguments, whereas you're making that conditional on LogAcceptCategory which calls WillLog which is conditional on Enabled().

I don't know that `Dispatcher` needs to make the category/levels modifiable -- that seems like something the `g_dispatcher` implementation could handle via marking them as `protected` in `Dispatcher`. They should be directly testable via Dispatcher, so that disabled debug/trace categories are trivial to test, without needing indirection. I think your latest implementation (a4e8f3c8d6763a95c1804fc40781b379e6127b66) is dangerously wrong, btw -- `LogInfo()` etc currently always evaluate their arguments, whereas you're making that conditional on `LogAcceptCategory` which calls `WillLog` which is conditional on `Enabled()`.
stickies-v (Migrated from github.com) reviewed 2026-01-28 12:36:21 +00:00
stickies-v (Migrated from github.com) commented 2026-01-28 12:36:21 +00:00

I think your latest implementation (a4e8f3c) is dangerously wrong, btw -- LogInfo() etc currently always evaluate their arguments, whereas you're making that conditional on LogAcceptCategory which calls WillLog which is conditional on Enabled().

I didn't realize developer-notes.md says to only avoid relying on side-effects for LogDebug and LogTrace, I thought this was true for logging in general. I think it's very brittle that program execution potentially relies on side-effects in logging statements, and I think we should move away from that completely. A cursory search didn't show me any {Info,Warning,Error} statements that have such side-effects, but I agree that it's possible and that makes reviewing this PR much more difficult, so I'll revert that behaviour to minimize scope and leave room for a follow-up. Thanks for pointing this out.

They should be directly testable via Dispatcher, so that disabled debug/trace categories are trivial to test, without needing indirection.

I'm not sure I understand your concern here. They are testable via Dispatcher::WillLog(). Is it purely the indirection performance overhead you're worried about? When running the benches on main and on a4e8f3c8d6763a95c1804fc40781b379e6127b66 (with small change to run this->Enabled() after this->WillLogCategoryLevel(static_cast<BCLog::LogFlags>(cat), level) which I'll include in next force-push, it doens't look like we have anything to worry about?

On master (34a5ecadd7):

|               ns/op |                op/s |    err% |     total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
|            1,837.04 |          544,353.35 |    2.6% |      0.01 | `LogWithDebug`
|            1,926.63 |          519,041.49 |    0.2% |      0.01 | `LogWithThreadNames`
|                2.81 |      355,468,404.18 |    3.1% |      0.01 | `LogWithoutDebug`
|            1,828.42 |          546,919.86 |    1.1% |      0.01 | `LogWithoutThreadNames`
|               18.31 |       54,600,726.18 |    1.1% |      0.01 | `LogWithoutWriteToFile`

On a4e8f3c8d6763a95c1804fc40781b379e6127b66 (with small unpushed modification):

|               ns/op |                op/s |    err% |     total | benchmark
|--------------------:|--------------------:|--------:|----------:|:----------
|            1,826.40 |          547,524.98 |    0.7% |      0.01 | `LogWithDebug`
|            1,863.31 |          536,680.71 |    1.0% |      0.01 | `LogWithThreadNames`
|                4.23 |      236,235,773.60 |    0.4% |      0.01 | `LogWithoutDebug`
|            1,800.52 |          555,395.22 |    1.3% |      0.01 | `LogWithoutThreadNames`
|               19.92 |       50,188,600.76 |    1.0% |      0.01 | `LogWithoutWriteToFile`
> I think your latest implementation ([a4e8f3c](https://github.com/bitcoin/bitcoin/commit/a4e8f3c8d6763a95c1804fc40781b379e6127b66)) is dangerously wrong, btw -- `LogInfo()` etc currently always evaluate their arguments, whereas you're making that conditional on `LogAcceptCategory` which calls `WillLog` which is conditional on `Enabled()`. I didn't realize `developer-notes.md` says to only avoid relying on side-effects for `LogDebug` and `LogTrace`, I thought this was true for logging in general. I think it's very brittle that program execution potentially relies on side-effects in logging statements, and I think we should move away from that completely. A cursory search didn't show me any `{Info,Warning,Error}` statements that have such side-effects, but I agree that it's possible and that makes reviewing this PR much more difficult, so I'll revert that behaviour to minimize scope and leave room for a follow-up. Thanks for pointing this out. > They should be directly testable via Dispatcher, so that disabled debug/trace categories are trivial to test, without needing indirection. I'm not sure I understand your concern here. They are testable via `Dispatcher::WillLog()`. Is it purely the indirection performance overhead you're worried about? When running the benches on main and on a4e8f3c8d6763a95c1804fc40781b379e6127b66 (with small change to run `this->Enabled()` _after_ `this->WillLogCategoryLevel(static_cast<BCLog::LogFlags>(cat), level)` which I'll include in next force-push, it doens't look like we have anything to worry about? On master (34a5ecadd7203121b03ac692d22a752d2a364111): ``` | ns/op | op/s | err% | total | benchmark |--------------------:|--------------------:|--------:|----------:|:---------- | 1,837.04 | 544,353.35 | 2.6% | 0.01 | `LogWithDebug` | 1,926.63 | 519,041.49 | 0.2% | 0.01 | `LogWithThreadNames` | 2.81 | 355,468,404.18 | 3.1% | 0.01 | `LogWithoutDebug` | 1,828.42 | 546,919.86 | 1.1% | 0.01 | `LogWithoutThreadNames` | 18.31 | 54,600,726.18 | 1.1% | 0.01 | `LogWithoutWriteToFile` ``` On a4e8f3c8d6763a95c1804fc40781b379e6127b66 (with small unpushed modification): ``` | ns/op | op/s | err% | total | benchmark |--------------------:|--------------------:|--------:|----------:|:---------- | 1,826.40 | 547,524.98 | 0.7% | 0.01 | `LogWithDebug` | 1,863.31 | 536,680.71 | 1.0% | 0.01 | `LogWithThreadNames` | 4.23 | 236,235,773.60 | 0.4% | 0.01 | `LogWithoutDebug` | 1,800.52 | 555,395.22 | 1.3% | 0.01 | `LogWithoutThreadNames` | 19.92 | 50,188,600.76 | 1.0% | 0.01 | `LogWithoutWriteToFile` ```
stickies-v (Migrated from github.com) reviewed 2026-01-28 16:09:21 +00:00
stickies-v (Migrated from github.com) commented 2026-01-28 16:09:21 +00:00

Latest force-push (dca56e0237abb485edd1cfe870069e2d75f08e42) reverts the unconditional argument evaluation for Info and higher levels, and adds a unit test to ensure behaviour before and after this PR remains the same. Thanks for raising this. Would be nice to remove logging side-effects entirely in a follow-up (probably just a documentation change and maybe some fix-ups).

Latest force-push (dca56e0237abb485edd1cfe870069e2d75f08e42) reverts the unconditional argument evaluation for `Info` and higher levels, and adds a unit test to ensure behaviour before and after this PR remains the same. Thanks for raising this. Would be nice to remove logging side-effects entirely in a follow-up (probably just a documentation change and maybe some fix-ups).
stickies-v commented 2026-01-28 16:12:46 +00:00 (Migrated from github.com)

Latest force-push:

  • reverts the unconditional argument evaluation for Info and higher levels, and adds a unit test to ensure behaviour before and after this PR remains the same. Addresses @ajtown's comment.
  • BCLog::Logger's callback now first evaluates WillLogCategoryLevel() before verifying the logger is Enabled(), so we can keep the operation lock-free when the category isn't disabled. No behaviour change, just a small optimization.
Latest force-push: - reverts the unconditional argument evaluation for `Info` and higher levels, and adds a unit test to ensure behaviour before and after this PR remains the same. Addresses @ajtown's [comment](https://github.com/bitcoin/bitcoin/pull/34374#discussion_r2734793117). - `BCLog::Logger`'s callback now first evaluates `WillLogCategoryLevel()` before verifying the logger is `Enabled()`, so we can keep the operation lock-free when the category isn't disabled. No behaviour change, just a small optimization.
DrahtBot commented 2026-01-28 19:03:25 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task iwyu: https://github.com/bitcoin/bitcoin/actions/runs/21445810981/job/61765316670
LLM reason (✨ experimental): IWYU failure: include-what-you-use detected issues and exited non-zero.

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.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `iwyu`: https://github.com/bitcoin/bitcoin/actions/runs/21445810981/job/61765316670</sub> <sub>LLM reason (✨ experimental): IWYU failure: include-what-you-use detected issues and exited non-zero.</sub> <details><summary>Hints</summary> 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. </details>
ryanofsky commented 2026-01-28 20:18:05 +00:00 (Migrated from github.com)

Code review a4e8f3c8d6763a95c1804fc40781b379e6127b66. Major concept and approach ACK. This seems thoughtfully implemented to be minimally disruptive to existing code while rationalizing the kernel logging API:

  • Providing stuctured log entries instead of strings that need to be parsed to get information like timestamps and source locations.
  • Dropping set_options function with obscure options like log_time_micros.
  • Dropping filtering functions like set_level_category enable_category disable_category that can interact in unexpected ways. Just providing basic level filtering, while leaving space to add other filtering options later.
  • Dropping logging_disable function that needs to be called by applications that don't use logging. (Note: #33847 also eliminates this).
  • Eliminating global logging functions not tied to logging connections (also addressed by #33847).

Suggestion to remove extra overhead

I do agree with AJ that requiring a call through a function pointer https://github.com/bitcoin/bitcoin/pull/34374#discussion_r2720709919 just to detect whether to log something does not seem good. Fortunately, there should be no need to do this for bitcoin core logging, though we may want to preserve it as an option for kernel logging. a4e8f3c8d6763a95c1804fc40781b379e6127b66 already has a statically linked g_dispatcher() function with different implementations in kernel/bitcoinkernel.cpp and logging.cpp, so it would only need to add a second g_dispatcher_should_log hook, defined differently for kernel and non-kernel binaries to avoid std::function overhead:

diff

--- a/src/kernel/bitcoinkernel.cpp
+++ b/src/kernel/bitcoinkernel.cpp
@@ -57,9 +57,7 @@
 
 struct KernelLogger {
     std::atomic<util::log::Level> min_level{util::log::Level::Info};
-    util::log::Dispatcher dispatcher{[this](util::log::Level level, uint64_t) {
-        return level >= min_level.load(std::memory_order_relaxed);
-    }};
+    util::log::Dispatcher dispatcher{this};
 };
 
 // Kernel logging state. Intentionally leaked to avoid use-after-destroy if logging occurs during
@@ -75,6 +73,12 @@ util::log::Dispatcher& util::log::g_dispatcher()
     return g_kernel_logger().dispatcher;
 }
 
+bool util::log::g_dispatcher_should_log(void* context, util::log::Level level, uint64_t category)
+{
+    KernelLogger& logger{*static_cast<KernelLogger*>(context)};
+    return level >= logger.min_level.load(std::memory_order_relaxed);
+}
+
 using kernel::ChainstateRole;
 using util::ImmediateTaskRunner;
 
--- a/src/logging.cpp
+++ b/src/logging.cpp
@@ -49,6 +49,12 @@ util::log::Dispatcher& util::log::g_dispatcher()
     return *LogInstance().GetDispatcher();
 }
 
+bool util::log::g_dispatcher_should_log(void* context, util::log::Level level, uint64_t category)
+{
+    BCLog::Logger& logger{*static_cast<BCLog::Logger*>(context)};
+    return logger.Enabled() && logger.WillLogCategoryLevel(static_cast<BCLog::LogFlags>(category), level);
+}
+
 bool fLogIPs = DEFAULT_LOGIPS;
 
 static int FileWriteStr(std::string_view str, FILE *fp)
@@ -617,10 +623,7 @@ bool BCLog::Logger::SetCategoryLogLevel(std::string_view category_str, std::stri
 
 BCLog::Logger::Logger()
 {
-    auto filter_func = [this](util::log::Level level, uint64_t cat) {
-        return this->Enabled() && this->WillLogCategoryLevel(static_cast<BCLog::LogFlags>(cat), level);
-    };
-    m_dispatcher = std::make_unique<util::log::Dispatcher>(filter_func);
+    m_dispatcher = std::make_unique<util::log::Dispatcher>(this);
 
     m_callback_handle = m_dispatcher->RegisterCallback([this](const util::log::Entry& entry) {
         LogPrintStr(entry);
--- a/src/test/util_log_tests.cpp
+++ b/src/test/util_log_tests.cpp
@@ -50,7 +50,7 @@ BOOST_AUTO_TEST_CASE(dispatcher_filter)
     // Use a simple level filter predicate to only log above a specified min_level
     Level min_level;
     auto level_filter = [&min_level](Level level, uint64_t) { return level >= min_level; };
-    Dispatcher dispatcher{level_filter};
+    Dispatcher dispatcher{nullptr, level_filter};
     int callback_count{0};
     auto handle = dispatcher.RegisterCallback([&](const Entry&) { ++callback_count; });
 
--- a/src/util/log.h
+++ b/src/util/log.h
@@ -61,6 +61,9 @@ struct Entry {
     bool should_ratelimit{false}; //!< Hint for consumers if this entry should be ratelimited
 };
 
+//! Early custom callback to determine whether log message should be produced.
+bool g_dispatcher_should_log(void* context, Level level, uint64_t category);
+
 /**
  * Dispatcher is responsible for producing logs. It forwards structured log entries to one or
  * multiple logging sinks (e.g. BCLog::Logger) through its registered callbacks.
@@ -79,12 +82,13 @@ public:
 
     /**
      * Construct a Dispatcher.
+     * @param[in] context Optional context passed to g_dispatcher_should_log()
      * @param[in] filter  Optional predicate to filter log entries before dispatch to minimize
      *                    overhead (e.g. string formatting) for entries that all of the sinks would
      *                    be discarding anyway (e.g. due to a low logging level).
      *                    If null, all entries are dispatched (when callbacks are registered).
      */
-    Dispatcher(FilterFunc filter = nullptr) : m_filter{std::move(filter)} {}
+    Dispatcher(void* context = nullptr, FilterFunc filter = nullptr) : m_context{context}, m_filter{std::move(filter)} {}
 
     /**
      * Register a callback to receive log entries.
@@ -103,7 +107,7 @@ public:
     bool Enabled() const { return m_callback_count.load(std::memory_order_acquire) > 0; }
 
     /** @return true if Enabled() and the filter (if set) passes for the provided level and category. */
-    bool WillLog(Level level, uint64_t category) const { return Enabled() && (!m_filter || m_filter(level, category)); }
+    bool WillLog(Level level, uint64_t category) const { return Enabled() && g_dispatcher_should_log(m_context, level, category) && (!m_filter || m_filter(level, category)); }
 
     /**
      * Format message and dispatch to all registered callbacks. No-op if WillLog() doesn't pass.
@@ -148,6 +152,8 @@ private:
     std::list<Callback> m_callbacks GUARDED_BY(m_mutex);
     //! Lock-free size of m_callbacks for fast checks.
     std::atomic<size_t> m_callback_count{0};
+    //! Optional context argument set in constructor.
+    void* m_context;
     //! Optional filter set in constructor; @see WillLog().
     const FilterFunc m_filter{nullptr};
 };

(Note: this could be rearranged and simplified more, it is just meant to provide a minimal change to consider.)

Suggestion to split up PR

My main feedback on the PR is that it is making a lot of trivial / move-only changes that I think would be better to move to a base PR:

  • Portion of first commit moving SourceLocation from logging.h to util/log.h (base PR could introduce util/log.h)
  • Commit "test: verify log argument evaluation semantics"
  • Portion of third commit moving BCLog::Level from logging.h to util/log.h
  • Commit "kernel: add log level/category name getters"
  • Portion of commit "kernel: implement structured logging C API" adding missing category and level mappings.
  • Commit "move-only: move logging categories to separate header"
  • Commit "logging: move LogAcceptCategory to util" (modulo g_dispatcher call)
  • Commit "logging: move macros to util"

These are all worthwhile changes on their own, and moving them out of this PR should make it substantially smaller and easier to navigate.

I plan to review this PR more and also rebase #30342 on top of this. I think the PR's should complement each other well.

Code review a4e8f3c8d6763a95c1804fc40781b379e6127b66. Major concept and approach ACK. This seems thoughtfully implemented to be minimally disruptive to existing code while rationalizing the kernel logging API: - Providing stuctured log entries instead of strings that need to be parsed to get information like timestamps and source locations. - Dropping `set_options` function with obscure options like `log_time_micros`. - Dropping filtering functions like `set_level_category` `enable_category` `disable_category` that can interact in unexpected ways. Just providing basic level filtering, while leaving space to add other filtering options later. - Dropping `logging_disable` function that needs to be called by applications that don't use logging. (Note: [#33847](https://github.com/bitcoin/bitcoin/pull/33847) also eliminates this). - Eliminating global logging functions not tied to logging connections (also addressed by [#33847](https://github.com/bitcoin/bitcoin/pull/33847)). #### Suggestion to remove extra overhead I do agree with AJ that requiring a call through a function pointer https://github.com/bitcoin/bitcoin/pull/34374#discussion_r2720709919 just to detect _whether_ to log something does not seem good. Fortunately, there should be no need to do this for bitcoin core logging, though we may want to preserve it as an option for kernel logging. a4e8f3c8d6763a95c1804fc40781b379e6127b66 already has a statically linked `g_dispatcher()` function with different implementations in `kernel/bitcoinkernel.cpp` and `logging.cpp`, so it would only need to add a second `g_dispatcher_should_log` hook, defined differently for kernel and non-kernel binaries to avoid `std::function` overhead: <details><summary>diff</summary> <p> ```diff --- a/src/kernel/bitcoinkernel.cpp +++ b/src/kernel/bitcoinkernel.cpp @@ -57,9 +57,7 @@ struct KernelLogger { std::atomic<util::log::Level> min_level{util::log::Level::Info}; - util::log::Dispatcher dispatcher{[this](util::log::Level level, uint64_t) { - return level >= min_level.load(std::memory_order_relaxed); - }}; + util::log::Dispatcher dispatcher{this}; }; // Kernel logging state. Intentionally leaked to avoid use-after-destroy if logging occurs during @@ -75,6 +73,12 @@ util::log::Dispatcher& util::log::g_dispatcher() return g_kernel_logger().dispatcher; } +bool util::log::g_dispatcher_should_log(void* context, util::log::Level level, uint64_t category) +{ + KernelLogger& logger{*static_cast<KernelLogger*>(context)}; + return level >= logger.min_level.load(std::memory_order_relaxed); +} + using kernel::ChainstateRole; using util::ImmediateTaskRunner; --- a/src/logging.cpp +++ b/src/logging.cpp @@ -49,6 +49,12 @@ util::log::Dispatcher& util::log::g_dispatcher() return *LogInstance().GetDispatcher(); } +bool util::log::g_dispatcher_should_log(void* context, util::log::Level level, uint64_t category) +{ + BCLog::Logger& logger{*static_cast<BCLog::Logger*>(context)}; + return logger.Enabled() && logger.WillLogCategoryLevel(static_cast<BCLog::LogFlags>(category), level); +} + bool fLogIPs = DEFAULT_LOGIPS; static int FileWriteStr(std::string_view str, FILE *fp) @@ -617,10 +623,7 @@ bool BCLog::Logger::SetCategoryLogLevel(std::string_view category_str, std::stri BCLog::Logger::Logger() { - auto filter_func = [this](util::log::Level level, uint64_t cat) { - return this->Enabled() && this->WillLogCategoryLevel(static_cast<BCLog::LogFlags>(cat), level); - }; - m_dispatcher = std::make_unique<util::log::Dispatcher>(filter_func); + m_dispatcher = std::make_unique<util::log::Dispatcher>(this); m_callback_handle = m_dispatcher->RegisterCallback([this](const util::log::Entry& entry) { LogPrintStr(entry); --- a/src/test/util_log_tests.cpp +++ b/src/test/util_log_tests.cpp @@ -50,7 +50,7 @@ BOOST_AUTO_TEST_CASE(dispatcher_filter) // Use a simple level filter predicate to only log above a specified min_level Level min_level; auto level_filter = [&min_level](Level level, uint64_t) { return level >= min_level; }; - Dispatcher dispatcher{level_filter}; + Dispatcher dispatcher{nullptr, level_filter}; int callback_count{0}; auto handle = dispatcher.RegisterCallback([&](const Entry&) { ++callback_count; }); --- a/src/util/log.h +++ b/src/util/log.h @@ -61,6 +61,9 @@ struct Entry { bool should_ratelimit{false}; //!< Hint for consumers if this entry should be ratelimited }; +//! Early custom callback to determine whether log message should be produced. +bool g_dispatcher_should_log(void* context, Level level, uint64_t category); + /** * Dispatcher is responsible for producing logs. It forwards structured log entries to one or * multiple logging sinks (e.g. BCLog::Logger) through its registered callbacks. @@ -79,12 +82,13 @@ public: /** * Construct a Dispatcher. + * @param[in] context Optional context passed to g_dispatcher_should_log() * @param[in] filter Optional predicate to filter log entries before dispatch to minimize * overhead (e.g. string formatting) for entries that all of the sinks would * be discarding anyway (e.g. due to a low logging level). * If null, all entries are dispatched (when callbacks are registered). */ - Dispatcher(FilterFunc filter = nullptr) : m_filter{std::move(filter)} {} + Dispatcher(void* context = nullptr, FilterFunc filter = nullptr) : m_context{context}, m_filter{std::move(filter)} {} /** * Register a callback to receive log entries. @@ -103,7 +107,7 @@ public: bool Enabled() const { return m_callback_count.load(std::memory_order_acquire) > 0; } /** @return true if Enabled() and the filter (if set) passes for the provided level and category. */ - bool WillLog(Level level, uint64_t category) const { return Enabled() && (!m_filter || m_filter(level, category)); } + bool WillLog(Level level, uint64_t category) const { return Enabled() && g_dispatcher_should_log(m_context, level, category) && (!m_filter || m_filter(level, category)); } /** * Format message and dispatch to all registered callbacks. No-op if WillLog() doesn't pass. @@ -148,6 +152,8 @@ private: std::list<Callback> m_callbacks GUARDED_BY(m_mutex); //! Lock-free size of m_callbacks for fast checks. std::atomic<size_t> m_callback_count{0}; + //! Optional context argument set in constructor. + void* m_context; //! Optional filter set in constructor; @see WillLog(). const FilterFunc m_filter{nullptr}; }; ``` </p> </details> (Note: this could be rearranged and simplified more, it is just meant to provide a minimal change to consider.) #### Suggestion to split up PR My main feedback on the PR is that it is making a lot of trivial / move-only changes that I think would be better to move to a base PR: - Portion of first commit moving `SourceLocation` from `logging.h` to `util/log.h` (base PR could introduce `util/log.h`) - Commit "test: verify log argument evaluation semantics" - Portion of third commit moving `BCLog::Level` from `logging.h` to `util/log.h` - Commit "kernel: add log level/category name getters" - Portion of commit "kernel: implement structured logging C API" adding missing category and level mappings. - Commit "move-only: move logging categories to separate header" - Commit "logging: move LogAcceptCategory to util" (modulo g_dispatcher call) - Commit "logging: move macros to util" These are all worthwhile changes on their own, and moving them out of this PR should make it substantially smaller and easier to navigate. I plan to review this PR more and also rebase #30342 on top of this. I think the PR's should complement each other well.
stickies-v commented 2026-01-30 12:34:15 +00:00 (Migrated from github.com)

Thanks for the in-depth review and suggestions, @ryanofsky !

so it would only need to add a second g_dispatcher_should_log hook, defined differently for kernel and non-kernel binaries to avoid std::function overhead:

I hadn't considered this approach yet, and I think it's great. If we already have a g_should_log() global hook, I think it makes more sense to just scrap the filtering from Dispatcher entirely and let the user manage it completely. This simplifies the code, improves performance (e.g. if filtering is already done before arg evaluation, it doesn't have to be done again before dispatching) and avoids the pattern of having Dispatcher instances opaquely call a global function, when users would probably expect it to be local.

My main feedback on the PR is that it is making a lot of trivial / move-only changes that I think would be better to move to a base PR:

I've adopted some of your carve-out suggestions, and identified a few other places to make commits more straightforward. I prefer bundling (manageable) amount of clean-up with an actual functional improvement, so I'll open up a new PR that introduces the struct-based logging (i.e. the first commits), and convert this (now draft) follow-up PR to be about levels-based logging and removing logging.cpp from kernel.

Thanks for the in-depth review and suggestions, @ryanofsky ! > so it would only need to add a second `g_dispatcher_should_log` hook, defined differently for kernel and non-kernel binaries to avoid `std::function` overhead: I hadn't considered this approach yet, and I think it's great. If we already have a `g_should_log()` global hook, I think it makes more sense to just scrap the filtering from `Dispatcher` entirely and let the user manage it completely. This simplifies the code, improves performance (e.g. if filtering is already done before arg evaluation, it doesn't have to be done again before dispatching) and avoids the pattern of having `Dispatcher` instances opaquely call a global function, when users would probably expect it to be local. > My main feedback on the PR is that it is making a lot of trivial / move-only changes that I think would be better to move to a base PR: I've adopted some of your carve-out suggestions, and identified a few other places to make commits more straightforward. I prefer bundling (manageable) amount of clean-up with an actual functional improvement, so I'll open up a new PR that introduces the struct-based logging (i.e. the first commits), and convert this (now draft) follow-up PR to be about levels-based logging and removing `logging.cpp` from `kernel`.
ryanofsky commented 2026-01-30 14:22:15 +00:00 (Migrated from github.com)

I prefer bundling (manageable) amount of clean-up with an actual functional improvement

Your approach sounds reasonable. That said, I think it would be nice to first disentangle the move-only changes and make an initial PR that simply moves the log-emitting code from logging.h to util/log.h (logging macros, level and category constants, SourceLocation, LogAcceptCategory). This could likely be merged quickly, making the more substantive changes that follow easier to review.

This also makes sense conceptually: log-emitting and log-handling are separate concerns, and there’s no real reason code that only emits log messages should need to include a header defining a full log management API.

Another selfish reason I have for preferring the move-only changes first is that it should significantly reduce conflicts between your changes and my existing PRs, and make it easier to combine my logging work with yours.

> I prefer bundling (manageable) amount of clean-up with an actual functional improvement Your approach sounds reasonable. That said, I think it would be nice to first disentangle the move-only changes and make an initial PR that simply moves the log-emitting code from `logging.h` to `util/log.h` (logging macros, level and category constants, `SourceLocation`, `LogAcceptCategory`). This could likely be merged quickly, making the more substantive changes that follow easier to review. This also makes sense conceptually: log-emitting and log-handling are separate concerns, and there’s no real reason code that only emits log messages should need to include a header defining a full log management API. Another selfish reason I have for preferring the move-only changes first is that it should significantly reduce conflicts between your changes and my existing PRs, and make it easier to combine my logging work with yours.
ryanofsky commented 2026-01-30 18:04:46 +00:00 (Migrated from github.com)

re: https://github.com/bitcoin/bitcoin/pull/34374#issuecomment-3824030533

I think it would be nice to first disentangle the move-only changes and make an initial PR that simply moves the log-emitting code from logging.h to util/log.h (logging macros, level and category constants, SourceLocation, LogAcceptCategory). This could likely be merged quickly, making the more substantive changes that follow easier to review.

I implemented this in #34465 (after chatting with stickies in IRC). The changes in the first 4 commits were taken directly from this PR. Only the last commit adds a bit of new code.

re: https://github.com/bitcoin/bitcoin/pull/34374#issuecomment-3824030533 > I think it would be nice to first disentangle the move-only changes and make an initial PR that simply moves the log-emitting code from `logging.h` to `util/log.h` (logging macros, level and category constants, `SourceLocation`, `LogAcceptCategory`). This could likely be merged quickly, making the more substantive changes that follow easier to review. I implemented this in #34465 (after chatting with stickies in IRC). The changes in the first 4 commits were taken directly from this PR. Only the last commit adds a bit of new code.
ajtowns (Migrated from github.com) reviewed 2026-01-31 07:12:06 +00:00
ajtowns (Migrated from github.com) commented 2026-01-31 07:12:06 +00:00

I didn't realize developer-notes.md says to only avoid relying on side-effects for LogDebug and LogTrace, I thought this was true for logging in general. I think it's very brittle that program execution potentially relies on side-effects in logging statements,

Would be nice to remove logging side-effects entirely in a follow-up

The easiest way to ensure any potential/accidental side-effects don't make the behaviour brittle is to always evaluate the logging statements, which is what our code currently does -- it's similar logic as always evaluating assertions. Changing that is not a nice follow-up.

> I didn't realize `developer-notes.md` says to only avoid relying on side-effects for `LogDebug` and `LogTrace`, I thought this was true for logging in general. I think it's very brittle that program execution potentially relies on side-effects in logging statements, > Would be nice to remove logging side-effects entirely in a follow-up The easiest way to ensure any potential/accidental side-effects don't make the behaviour brittle is to always evaluate the logging statements, which is what our code currently does -- it's similar logic as always evaluating assertions. Changing that is not a nice follow-up.
DrahtBot commented 2026-01-31 12:26:14 +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).
ryanofsky commented 2026-03-06 15:14:33 +00:00 (Migrated from github.com)

Hi stickies, it'd be really nice to have this rebased & start review. Here is a rebase branch I made: https://github.com/ryanofsky/bitcoin/commits/review.34374.2-edit with minimal changes.

There are more simplifications that can be made, but it's already much smaller and simpler after #34465

Hi stickies, it'd be really nice to have this rebased & start review. Here is a rebase branch I made: https://github.com/ryanofsky/bitcoin/commits/review.34374.2-edit with minimal changes. There are more simplifications that can be made, but it's already much smaller and simpler after #34465
stickies-v commented 2026-03-16 05:47:49 +00:00 (Migrated from github.com)

Hi stickies, it'd be really nice to have this rebased & start review.

Apologies for the slow follow-up here @ryanofsky. My priorities were elsewhere for a while, but I've now picked this up again.

Since we now have the {Should}Log interface in util/log.h, I think also having util::log::Dispatcher doesn't really make sense anymore, so I've been reworking that into a kernel-specific type that implements the {Should}Log interface. I've been looking at a couple of different approaches and commit orderings to make reviewing as smooth as possible and think I'm close to having a good solution.

> Hi stickies, it'd be really nice to have this rebased & start review. Apologies for the slow follow-up here @ryanofsky. My priorities were elsewhere for a while, but I've now picked this up again. Since we now have the `{Should}Log` interface in `util/log.h`, I think also having `util::log::Dispatcher` doesn't really make sense anymore, so I've been reworking that into a kernel-specific type that implements the `{Should}Log` interface. I've been looking at a couple of different approaches and commit orderings to make reviewing as smooth as possible and think I'm close to having a good solution.
ryanofsky commented 2026-03-16 13:26:13 +00:00 (Migrated from github.com)

I think also having util::log::Dispatcher doesn't really make sense anymore, so I've been reworking that into a kernel-specific type that implements the {Should}Log interface.

FWIW I did this in the linked branch:

github.com/bitcoin/bitcoin@8611ff533c/src/kernel/bitcoinkernel.cpp (L85-L93)

The Dispatcher type is kernel-specific there and not used outside the kernel. The only thing I didn't do was move the dispatcher class definition and strip out some unused code because I was avoiding extraneous changes in the rebase. Seems fine to reimplement, though. Should be equivalent to using the branch and the stripping bits of code that are no longer needed.

> I think also having `util::log::Dispatcher` doesn't really make sense anymore, so I've been reworking that into a kernel-specific type that implements the `{Should}Log` interface. FWIW I did this in the linked [branch](https://github.com/ryanofsky/bitcoin/commits/review.34374.2-edit): https://github.com/bitcoin/bitcoin/blob/8611ff533c5c384a2a8fd32000fc9d6619d46b48/src/kernel/bitcoinkernel.cpp#L85-L93 The Dispatcher type is kernel-specific there and not used outside the kernel. The only thing I didn't do was move the dispatcher class definition and strip out some unused code because I was avoiding extraneous changes in the rebase. Seems fine to reimplement, though. Should be equivalent to using the branch and the stripping bits of code that are no longer needed.
ryanofsky commented 2026-04-10 11:59:59 +00:00 (Migrated from github.com)

Here is a rebased version of this PR with the logger cleanups from #34865 dropped: ec9e622d2da96ac482cf1bf599e20292d2f78759 (branch)

Now there's basically no overlap between this PR and #34865 so both could be reviewed in parallel.

Aside from the logger change I also dropped some dead code in the dispatcher class from the previous rebase and rewrote one commit message. Otherwise the branch is unchanged and the commits are straightforward:

  • 6c148e8c405532338a41f8787ddf76ac299b2b73 util: add log::Dispatcher for struct-based log dispatch (1/10)
  • 1f861b7f46ae1227208c35b880a6c8ebdcafee35 kernel: add log level/category name getters (2/10)
  • ddd655b6dd31b26a414f948a25e48c3366c50fba kernel: add missing log levels and categories (3/10)
  • d73cc47d9804a63b6ff9b3c88ab772032e4188a0 kernel: expose btck_LogEntry (4/10)
  • 8efdf8504959404326970610389dcd3130a31ec3 kernel: make logging struct-based (5/10)
  • 18a9a3c0cad3fb6c3e3c3d091879e75c629e519d kernel: remove unused logging functions (6/10)
  • d4e3282a593b646cb0a6a4d167394bf18e9904c4 logging: declare LogAcceptCategory in util (7/10)
  • ef451c810031168acc37ae52403f558abf72715d doc: advise on log rate limiting in developer notes (8/10)
  • c0cda82e443f2730ef423041d43551c4847ac590 logging: use util/log.h where possible (9/10)
  • ec9e622d2da96ac482cf1bf599e20292d2f78759 kernel: implement levels-based logging C API (10/10)
Here is a rebased version of this PR with the logger cleanups from #34865 dropped: ec9e622d2da96ac482cf1bf599e20292d2f78759 ([branch](https://github.com/ryanofsky/bitcoin/commits/pr/kslog)) Now there's basically no overlap between this PR and #34865 so both could be reviewed in parallel. Aside from the logger change I also dropped some dead code in the dispatcher class from the previous [rebase](https://github.com/bitcoin/bitcoin/pull/34374#issuecomment-4012333941) and rewrote one commit message. Otherwise the branch is unchanged and the commits are straightforward: - 6c148e8c405532338a41f8787ddf76ac299b2b73 util: add log::Dispatcher for struct-based log dispatch (1/10) - 1f861b7f46ae1227208c35b880a6c8ebdcafee35 kernel: add log level/category name getters (2/10) - ddd655b6dd31b26a414f948a25e48c3366c50fba kernel: add missing log levels and categories (3/10) - d73cc47d9804a63b6ff9b3c88ab772032e4188a0 kernel: expose btck_LogEntry (4/10) - 8efdf8504959404326970610389dcd3130a31ec3 kernel: make logging struct-based (5/10) - 18a9a3c0cad3fb6c3e3c3d091879e75c629e519d kernel: remove unused logging functions (6/10) - d4e3282a593b646cb0a6a4d167394bf18e9904c4 logging: declare LogAcceptCategory in util (7/10) - ef451c810031168acc37ae52403f558abf72715d doc: advise on log rate limiting in developer notes (8/10) - c0cda82e443f2730ef423041d43551c4847ac590 logging: use util/log.h where possible (9/10) - ec9e622d2da96ac482cf1bf599e20292d2f78759 kernel: implement levels-based logging C API (10/10)
ryanofsky commented 2026-06-08 13:57:33 +00:00 (Migrated from github.com)

Here is a rebased version of this pr: 683056775c58f05f9603f3f7023467ab0e7f723e (branch) again with minimal changes from the original.

I'd really like to review and be able to merge this change because it makes the kernel logging API simpler and more usable.

When I rebased this, I made as few changes as possible so comparison to the current version would be simple. But there are a few additional cleanups that could be made on top of this:

Here is a rebased version of this pr: 683056775c58f05f9603f3f7023467ab0e7f723e ([branch](https://github.com/ryanofsky/bitcoin/commits/pr/kslog)) again with minimal changes from the original. I'd really like to review and be able to merge this change because it makes the kernel logging API simpler and more usable. When I rebased this, I made as few changes as possible so comparison to the current version would be simple. But there are a few additional cleanups that could be made on top of this: - `Dispatcher` class should probably be moved out of `util/log` since dispatching is a log-handling function not a log-generating function. As mentioned https://github.com/bitcoin/bitcoin/pull/34865#issuecomment-4236420269, the `Dispatcher` class could also be dropped and replaced with a btcsignals::signal. But it is a very simple class so it seems fine to keep. - A few simplifications / renames in `bitcoinkernel.cpp` would probably be appropriate like inlining `g_dispatcher`. I just kept current names for things so diffs would be minimal.
DrahtBot commented 2026-09-05 00:29:44 +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.
DrahtBot commented 2026-09-08 23:05:28 +00:00 (Migrated from github.com)

🚧 At least one of the CI tasks failed.
Task lint: https://github.com/bitcoin/bitcoin/actions/runs/34283939029/job/102254999690
LLM reason (✨ experimental): CI failed because the lint check detected a new circular dependency: kernel/bitcoinkernel -> kernel/logger -> kernel/bitcoinkernel (lint-circular-dependencies).

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.

<!--85328a0da195eb286784d51f73fa0af9--> 🚧 At least one of the CI tasks failed. <sub>Task `lint`: https://github.com/bitcoin/bitcoin/actions/runs/34283939029/job/102254999690</sub> <sub>LLM reason (✨ experimental): CI failed because the lint check detected a new circular dependency: `kernel/bitcoinkernel -> kernel/logger -> kernel/bitcoinkernel` (lint-circular-dependencies).</sub> <details><summary>Hints</summary> 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. </details>
stickies-v commented 2026-09-09 17:14:58 +00:00 (Migrated from github.com)

Apologies for the months of radio silence here. I've had many cycles of exploring various architectures and ways to minimize review burden, to then kind of get stuck on something I really didn't like, give up, and repeat a while later. While I've found pretty much every approach to have its own downsides, I believe the current version has the best set of trade-offs. kernel and node logging having to co-exist complicates many things, but is a reality we have to work around.

The main changes in this latest version are:

  • rebased onto latest master
  • util::log::Dispatcher is gone. Instead, this PR implements the Log and Should{Debug,Trace}Log interface, and removes the std::function indirection callbacks this PR used to have. The new KernelLogger that keeps state (minimum level and callbacks) is small and kernel-specific, and an implementation detail.
  • mocktime is added to btck_LogEntry
  • removed the btck_log_level_get_name/btck_log_category_get_name C functions from the earlier version; the C++ wrapper has Name() helpers instead. Consumers know the constants, so I don't think this belongs in the C API. It removes a lot of the churn in the previous approach wrt using arrays for the btck/util/name mapping.
  • when kernel code emits an unmapped category, this will now only crash in debug builds (with Assume). We can't currently enforce a complete mapping at compile time, and it's fairly easy for a new category to accidentally slip into kernel code. If that's in a rare code path, it'd be dangerous for that to crash in non-debug builds.

Thoughts on things I've explored:

  • the top commit is fairly large. In previous approaches, I had separated this into smaller commits, but I've decided to revert it to two big ones to minimize churn. Because the util::log interface requires exactly one implementation, certain gradual changes are not really possible without workarounds that then add review complexity.
  • there's fairly minimal test coverage. Since util::log needs exactly one implementation per binary, test_bitcoin can't link the kernel library, and test_kernel only sees the exported C API, so KernelLogger isn't reachable from either test binary without workarounds. I've tried moving KernelLogger into its own header so test_bitcoin can compile it directly. That runs into the circular dependency linter and, when header-only, into clang's -Wunique-object-duplication for the singleton. Working around that means moving the logging C functions out of bitcoinkernel.cpp and sharing the Handle template through yet another header, which is even more churn. I think we should improve this through https://github.com/bitcoin/bitcoin/pull/28690 and then a new organization of test binaries (e.g. test_node, test_kernel, and test_kernel_interface), but I'm very hesitant to add that to the scope of this PR, which I feel is already fairly large.

Despite my hesitations, I think these changes would massively improve the kernel logging interface, and help with better separation between kernel and node.

Apologies for the months of radio silence here. I've had many cycles of exploring various architectures and ways to minimize review burden, to then kind of get stuck on something I really didn't like, give up, and repeat a while later. While I've found pretty much every approach to have its own downsides, I believe the current version has the best set of trade-offs. `kernel` and `node` logging having to co-exist complicates many things, but is a reality we have to work around. The main changes in this latest version are: - rebased onto latest `master` - `util::log::Dispatcher` is gone. Instead, this PR implements the `Log` and `Should{Debug,Trace}Log` interface, and removes the `std::function` indirection callbacks this PR used to have. The new `KernelLogger` that keeps state (minimum level and callbacks) is small and kernel-specific, and an implementation detail. - `mocktime` is added to `btck_LogEntry` - removed the `btck_log_level_get_name`/`btck_log_category_get_name` C functions from the earlier version; the C++ wrapper has `Name()` helpers instead. Consumers know the constants, so I don't think this belongs in the C API. It removes a lot of the churn in the previous approach wrt using arrays for the btck/util/name mapping. - when kernel code emits an unmapped category, this will now only crash in debug builds (with `Assume`). We can't currently enforce a complete mapping at compile time, and it's fairly easy for a new category to accidentally slip into kernel code. If that's in a rare code path, it'd be dangerous for that to crash in non-debug builds. Thoughts on things I've explored: - the top commit is fairly large. In previous approaches, I had separated this into smaller commits, but I've decided to revert it to two big ones to minimize churn. Because the `util::log` interface requires exactly one implementation, certain gradual changes are not really possible without workarounds that then add review complexity. - there's fairly minimal test coverage. Since `util::log` needs exactly one implementation per binary, `test_bitcoin` can't link the kernel library, and `test_kernel` only sees the exported C API, so `KernelLogger` isn't reachable from either test binary without workarounds. I've tried moving `KernelLogger` into its own header so `test_bitcoin` can compile it directly. That runs into the circular dependency linter and, when header-only, into clang's `-Wunique-object-duplication` for the singleton. Working around that means moving the logging C functions out of `bitcoinkernel.cpp` and sharing the `Handle` template through yet another header, which is even more churn. I think we should improve this through https://github.com/bitcoin/bitcoin/pull/28690 and then a new organization of test binaries (e.g. test_node, test_kernel, and test_kernel_interface), but I'm very hesitant to add that to the scope of this PR, which I feel is already fairly large. Despite my hesitations, I think these changes would massively improve the kernel logging interface, and help with better separation between `kernel` and `node`.
stickies-v (Migrated from github.com) reviewed 2026-09-10 16:11:50 +00:00
stickies-v (Migrated from github.com) commented 2026-09-10 16:11:50 +00:00

Marking this as resolved, as the PR has changed a lot since then and the conversation no longer applies I think. The indirection is gone, and this PR now doesn't touch util/node logging (including logging statement evaluation) at all (except for a documentation change). Happy to re-open if I've missed anything.

Marking this as resolved, as the PR has changed a lot since then and the conversation no longer applies I think. The indirection is gone, and this PR now doesn't touch util/node logging (including logging statement evaluation) at all (except for a documentation change). Happy to re-open if I've missed anything.
w0xlt (Migrated from github.com) reviewed 2026-09-10 23:15:39 +00:00
w0xlt (Migrated from github.com) commented 2026-09-10 23:15:39 +00:00

RegisterCallback() and LogDebug() can also throw std::bad_alloc.

btck_LoggingConnection can be a smart pointer. Holding the connection in a std::unique_ptr prevents its allocation from leaking if logging throws.

Shouldn't user_data be destroyed if the callback registration fails ?

diff --git a/src/kernel/bitcoinkernel.cpp b/src/kernel/bitcoinkernel.cpp
index 72c1587450..635f1bacaf 100644
--- a/src/kernel/bitcoinkernel.cpp
+++ b/src/kernel/bitcoinkernel.cpp
@@ -949,17 +949,20 @@ void btck_logging_set_min_level(btck_LogLevel level)
 btck_LoggingConnection* btck_logging_connection_create(btck_LogCallback callback, void* user_data, btck_DestroyCallback user_data_destroy_callback)
 {
     assert(callback);
-    auto handle{GetKernelLogger().RegisterCallback(
-        callback, user_data, user_data_destroy_callback)};
-    btck_LoggingConnection* connection;
+    std::optional<KernelLogger::CallbackHandle> handle;
     try {
-        connection = btck_LoggingConnection::create(handle);
+        handle = GetKernelLogger().RegisterCallback(callback, user_data, user_data_destroy_callback);
+        std::unique_ptr<btck_LoggingConnection> connection{btck_LoggingConnection::create(*handle)};
+        LogDebug(BCLog::KERNEL, "Logger connected.");
+        return connection.release();
     } catch (...) {
-        GetKernelLogger().UnregisterCallback(handle);
+        if (handle) {
+            GetKernelLogger().UnregisterCallback(*handle);
+        } else if (user_data && user_data_destroy_callback) {
+            user_data_destroy_callback(user_data);
+        }
         return nullptr;
     }
-    LogDebug(BCLog::KERNEL, "Logger connected.");
-    return connection;
 }
 
 void btck_logging_connection_destroy(btck_LoggingConnection* connection)
`RegisterCallback()` and `LogDebug()` can also throw `std::bad_alloc`. `btck_LoggingConnection` can be a smart pointer. Holding the connection in a `std::unique_ptr` prevents its allocation from leaking if logging throws. Shouldn't `user_data` be destroyed if the callback registration fails ? ```diff diff --git a/src/kernel/bitcoinkernel.cpp b/src/kernel/bitcoinkernel.cpp index 72c1587450..635f1bacaf 100644 --- a/src/kernel/bitcoinkernel.cpp +++ b/src/kernel/bitcoinkernel.cpp @@ -949,17 +949,20 @@ void btck_logging_set_min_level(btck_LogLevel level) btck_LoggingConnection* btck_logging_connection_create(btck_LogCallback callback, void* user_data, btck_DestroyCallback user_data_destroy_callback) { assert(callback); - auto handle{GetKernelLogger().RegisterCallback( - callback, user_data, user_data_destroy_callback)}; - btck_LoggingConnection* connection; + std::optional<KernelLogger::CallbackHandle> handle; try { - connection = btck_LoggingConnection::create(handle); + handle = GetKernelLogger().RegisterCallback(callback, user_data, user_data_destroy_callback); + std::unique_ptr<btck_LoggingConnection> connection{btck_LoggingConnection::create(*handle)}; + LogDebug(BCLog::KERNEL, "Logger connected."); + return connection.release(); } catch (...) { - GetKernelLogger().UnregisterCallback(handle); + if (handle) { + GetKernelLogger().UnregisterCallback(*handle); + } else if (user_data && user_data_destroy_callback) { + user_data_destroy_callback(user_data); + } return nullptr; } - LogDebug(BCLog::KERNEL, "Logger connected."); - return connection; } void btck_logging_connection_destroy(btck_LoggingConnection* connection) ```
stickies-v (Migrated from github.com) reviewed 2026-09-11 10:18:21 +00:00
stickies-v (Migrated from github.com) commented 2026-09-11 10:18:21 +00:00

Good point, thanks. I think btck_logging_connection_destroy also needs similar improvements.

Good point, thanks. I think `btck_logging_connection_destroy` also needs similar improvements.
stickies-v (Migrated from github.com) reviewed 2026-09-14 15:16:11 +00:00
stickies-v (Migrated from github.com) commented 2026-09-14 15:16:11 +00:00

Fixed by wrapping user data in a RAII UserData wrapper, and the callback in a RAII CallbackHandle wrapper. It's a larger diff, but I think it makes for cleaner code. The UserData can be reused in other places too, but I'd prefer doing that in a follow-up as it's unrelated to this change. Does this address your concern? You're right that LogDebug also could potentially throw, but this is true in the bitcoinkernel code in general and I feel might be out of scope here?

Fixed by wrapping user data in a RAII `UserData` wrapper, and the callback in a RAII `CallbackHandle` wrapper. It's a larger diff, but I think it makes for cleaner code. The `UserData` can be reused in other places too, but I'd prefer doing that in a follow-up as it's unrelated to this change. Does this address your concern? You're right that `LogDebug` also could potentially throw, but this is true in the bitcoinkernel code in general and I feel might be out of scope here?
stickies-v commented 2026-09-15 11:19:38 +00:00 (Migrated from github.com)

Force-pushed to wrap user data and the callbacks in RAII wrappers, addressing @w0xlt's comment and making it harder to introduce similar issues in the future. Also moved KernelLogger into the anonymous namespace in the final commit.

Force-pushed to wrap user data and the callbacks in RAII wrappers, addressing @w0xlt's [comment](https://github.com/bitcoin/bitcoin/pull/34374#discussion_r3984322784) and making it harder to introduce similar issues in the future. Also moved `KernelLogger` into the anonymous namespace in the final commit.
stickies-v commented 2026-09-15 14:15:09 +00:00 (Migrated from github.com)

Force-pushed to address merge conflict with #36207, no other changes.

Force-pushed to address merge conflict with #36207, no other changes.
w0xlt (Migrated from github.com) reviewed 2026-09-16 20:38:37 +00:00
w0xlt (Migrated from github.com) commented 2026-09-16 20:38:37 +00:00

There is a narrow failure case here, related to the concerns about callbacks running under locks discussed in #36197.

Registration creates a temporary Callback while holding the logger mutex. That temporary owns user_data. If allocating the list node throws std::bad_alloc, the temporary is destroyed before the mutex is released, invoking user_data_destroy_callback.

If that callback destroys another logging connection, it tries to acquire the same mutex and deadlocks, preventing btck_logging_connection_create from returning nullptr.

Suggestion: the callback can be constructed before the lock guard, so exception cleanup releases the mutex before destroying the user data, consistent with UnregisterCallback.

diff --git a/src/kernel/bitcoinkernel.cpp b/src/kernel/bitcoinkernel.cpp
index 0a4b5e30cb..4f512df146 100644
--- a/src/kernel/bitcoinkernel.cpp
+++ b/src/kernel/bitcoinkernel.cpp
@@ -564,8 +564,10 @@ public:
 
 KernelLogger::CallbackHandle KernelLogger::RegisterCallback(btck_LogCallback fn, UserData user_data)
 {
+    // Destroy user_data after releasing m_mutex if insertion throws.
+    Callback callback{fn, std::move(user_data)};
     STDLOCK(m_mutex);
-    m_callbacks.push_back({fn, std::move(user_data)});
+    m_callbacks.push_back(std::move(callback));
     return {*this, std::prev(m_callbacks.end())};
 }
There is a narrow failure case here, related to the concerns about callbacks running under locks discussed in #36197. Registration creates a temporary `Callback` while holding the logger mutex. That temporary owns `user_data`. If allocating the list node throws `std::bad_alloc`, the temporary is destroyed before the mutex is released, invoking `user_data_destroy_callback`. If that callback destroys another logging connection, it tries to acquire the same mutex and deadlocks, preventing `btck_logging_connection_create` from returning `nullptr`. Suggestion: the callback can be constructed before the lock guard, so exception cleanup releases the mutex before destroying the user data, consistent with `UnregisterCallback`. ```diff diff --git a/src/kernel/bitcoinkernel.cpp b/src/kernel/bitcoinkernel.cpp index 0a4b5e30cb..4f512df146 100644 --- a/src/kernel/bitcoinkernel.cpp +++ b/src/kernel/bitcoinkernel.cpp @@ -564,8 +564,10 @@ public: KernelLogger::CallbackHandle KernelLogger::RegisterCallback(btck_LogCallback fn, UserData user_data) { + // Destroy user_data after releasing m_mutex if insertion throws. + Callback callback{fn, std::move(user_data)}; STDLOCK(m_mutex); - m_callbacks.push_back({fn, std::move(user_data)}); + m_callbacks.push_back(std::move(callback)); return {*this, std::prev(m_callbacks.end())}; } ```
stickies-v (Migrated from github.com) reviewed 2026-09-17 07:26:59 +00:00
stickies-v (Migrated from github.com) commented 2026-09-17 07:26:59 +00:00

Good catch, fixed. You're right to point out we need to be careful with executing callbacks when under a lock. I think we should work to making Log execute the logging callbacks without lock too, but I think that's best kept for a follow-up.

If that callback destroys another logging connection

Even worse, it can be triggered if the callback interacts in any way with the logger, including producing a logging statement. (edit: of course, this is still a very narrow failure case, requiring allocation failure and an I think unusual path where cleaning up user data interacts with the kernel).

Good catch, fixed. You're right to point out we need to be careful with executing callbacks when under a lock. I think we should work to making `Log` execute the logging callbacks without lock too, but I think that's best kept for a follow-up. > If that callback destroys another logging connection Even worse, it can be triggered if the callback interacts in any way with the logger, including producing a logging statement. (_edit: of course, this is still a very narrow failure case, requiring allocation failure and an I think unusual path where cleaning up user data interacts with the kernel)_.
stickies-v commented 2026-09-17 09:22:22 +00:00 (Migrated from github.com)

Force-pushed to address a (hard to reach) deadlock issue flagged by @w0xlt

Force-pushed to address a (hard to reach) deadlock issue [flagged](https://github.com/bitcoin/bitcoin/pull/34374#discussion_r4030551866) by @w0xlt
w0xlt (Migrated from github.com) reviewed 2026-09-24 21:18:33 +00:00
w0xlt (Migrated from github.com) left a comment
ACK cfd2312f0f
ryanofsky (Migrated from github.com) approved these changes 2026-09-25 05:22:52 +00:00
ryanofsky (Migrated from github.com) left a comment

Code review ACK cfd2312f0f. Thanks for the nice updates! Dropping util::log::Dispatcher and implementing the log hooks directly in KernelLogger is simpler than routing through a dispatcher that only ever has one implementation per binary, and struct-based entries plus level-only filtering are a good simplification of the API.

I left some comments below, but they are all minor or just notes, and none are blocking.

Code review ACK cfd2312f0fdedcba6ba827ffa9d85d6c1d5c70f1. Thanks for the nice updates! Dropping `util::log::Dispatcher` and implementing the log hooks directly in `KernelLogger` is simpler than routing through a dispatcher that only ever has one implementation per binary, and struct-based entries plus level-only filtering are a good simplification of the API. I left some comments below, but they are all minor or just notes, and none are blocking.
ryanofsky (Migrated from github.com) commented 2026-09-25 04:00:45 +00:00

In commit "kernel: make logging struct-based and simplify configuration" (cfd2312f0f)

Not important: this prints the raw nanosecond count as the timestamp (e.g. 1758718542123456789). Maybe print seconds with a fractional part so it's readable at a glance (needs <iomanip>):

    void LogMessage(const LogEntry& entry)
    {
        const auto us{std::chrono::floor<std::chrono::microseconds>(entry.Timestamp()).time_since_epoch().count()};
        std::cout << "kernel: " << us / 1'000'000 << '.' << std::setw(6) << std::setfill('0') << us % 1'000'000
                  << " [" << Name(entry.Category()) << ":" << Name(entry.Level()) << "] "
                  << entry.Message() << "\n";
    }
In commit "kernel: make logging struct-based and simplify configuration" (cfd2312f0fdedcba6ba827ffa9d85d6c1d5c70f1) Not important: this prints the raw nanosecond count as the timestamp (e.g. `1758718542123456789`). Maybe print seconds with a fractional part so it's readable at a glance (needs `<iomanip>`): ```c++ void LogMessage(const LogEntry& entry) { const auto us{std::chrono::floor<std::chrono::microseconds>(entry.Timestamp()).time_since_epoch().count()}; std::cout << "kernel: " << us / 1'000'000 << '.' << std::setw(6) << std::setfill('0') << us % 1'000'000 << " [" << Name(entry.Category()) << ":" << Name(entry.Level()) << "] " << entry.Message() << "\n"; } ```
ryanofsky (Migrated from github.com) commented 2026-09-25 04:00:33 +00:00

re: https://github.com/bitcoin/bitcoin/pull/34374#discussion_r2731541954

@purpleKarrot I agree the singleton is worth getting rid of, but I think it can be removed later without any API changes, because this PR keeps btck_LoggingConnection handles. I'm rebasing #30342 on top of this PR, and in that version GetKernelLogger() goes away entirely: each logging connection becomes the logger that the kernel objects of its contexts log to, so there is no global logging state left in the kernel library. That is a bigger change than this PR, so I don't think the singleton here should block it.

re: https://github.com/bitcoin/bitcoin/pull/34374#discussion_r2731541954 @purpleKarrot I agree the singleton is worth getting rid of, but I think it can be removed later without any API changes, because this PR keeps `btck_LoggingConnection` handles. I'm rebasing #30342 on top of this PR, and in that version `GetKernelLogger()` goes away entirely: each logging connection becomes the logger that the kernel objects of its contexts log to, so there is no global logging state left in the kernel library. That is a bigger change than this PR, so I don't think the singleton here should block it.
ryanofsky (Migrated from github.com) commented 2026-09-25 05:08:24 +00:00

In commit "kernel: add missing log levels and categories" (3a72f3040c)

Minor, only affects intermediate commits: since btck_LogCategory_LOCK is defined in every build but the get_bclog_flag case for it only exists with DEBUG_LOCKCONTENTION, calling btck_logging_enable_category(btck_LogCategory_LOCK) in a normal build hits the assert(false) until get_bclog_flag is removed in the last commit. Could be avoided in this commit with:

#else
    case btck_LogCategory_LOCK: {
        // No lock contention logging in this build, so there is nothing to enable or disable.
        return BCLog::LogFlags::NONE;
    }
#endif
In commit "kernel: add missing log levels and categories" (3a72f3040c295db54df534bd193f118b3d028749) Minor, only affects intermediate commits: since `btck_LogCategory_LOCK` is defined in every build but the `get_bclog_flag` case for it only exists with `DEBUG_LOCKCONTENTION`, calling `btck_logging_enable_category(btck_LogCategory_LOCK)` in a normal build hits the `assert(false)` until `get_bclog_flag` is removed in the last commit. Could be avoided in this commit with: ```c++ #else case btck_LogCategory_LOCK: { // No lock contention logging in this build, so there is nothing to enable or disable. return BCLog::LogFlags::NONE; } #endif ```
ryanofsky (Migrated from github.com) commented 2026-09-25 04:00:46 +00:00

In commit "kernel: add KernelLogger" (95e63e954a)

Note for a followup: btcsignals could be used to simplify this code, see a85f463f69a048a72b154347ae4c362ed22579c3 (tag) . This gets rid of ~60 lines, and no kernel lock would be held while a callback or a user_data destroy callback runs, so the kind of deadlock w0xlt found can't happen. This also relates to your follow-up idea in https://github.com/bitcoin/bitcoin/pull/34374#discussion_r4034240501 of running callbacks without the lock.

There are complications with this approach though so probably best to leave it alone here:

  • It depends on btcsignals fix #36011
  • It changes semantics: destroying a connection no longer waits for a callback running on another thread (the user_data would be destroyed when that callback returns instead, like boost signals)
  • Callbacks of different connections would run concurrently, and to keep each connection's callback from running concurrently with itself it still needs a per-connection mutex, so logging from inside a callback would still deadlock.
  • It would need btcsignals to use StdMutex, since with DEBUG_LOCKORDER a detected lock order problem is logged while lock tracking state is locked.
In commit "kernel: add KernelLogger" (95e63e954af32b2c4c7fa11a291e642ae961839d) Note for a followup: btcsignals could be used to simplify this code, see a85f463f69a048a72b154347ae4c362ed22579c3 ([tag](https://github.com/ryanofsky/bitcoin/commits/tmp/kslog-sigdest.1)) . This gets rid of ~60 lines, and no kernel lock would be held while a callback or a `user_data` destroy callback runs, so the kind of deadlock w0xlt found can't happen. This also relates to your follow-up idea in https://github.com/bitcoin/bitcoin/pull/34374#discussion_r4034240501 of running callbacks without the lock. There are complications with this approach though so probably best to leave it alone here: - It depends on btcsignals fix #36011 - It changes semantics: destroying a connection no longer waits for a callback running on another thread (the `user_data` would be destroyed when that callback returns instead, like boost signals) - Callbacks of different connections would run concurrently, and to keep each connection's callback from running concurrently with itself it still needs a per-connection mutex, so logging from inside a callback would still deadlock. - It would need btcsignals to use `StdMutex`, since with DEBUG_LOCKORDER a detected lock order problem is logged while lock tracking state is locked.
ryanofsky (Migrated from github.com) commented 2026-09-25 04:00:35 +00:00

In commit "kernel: add KernelLogger" (95e63e954a)

Minor: Log() takes m_mutex before checking m_callbacks.empty(), so when no logging connection is registered, every Info-and-above entry in every kernel thread still serializes on the mutex, and at debug level statements are formatted only to be dropped. The earlier util::log::Dispatcher avoided this with an atomic m_callback_count. An atomic count updated in RegisterCallback/UnregisterCallback and checked in ShouldLog() would cover both cases:

    bool ShouldLog(util::log::Level level) const
    {
        return m_callback_count.load(std::memory_order_relaxed) > 0 &&
               level >= m_min_level.load(std::memory_order_relaxed);
    }
In commit "kernel: add KernelLogger" (95e63e954af32b2c4c7fa11a291e642ae961839d) Minor: `Log()` takes `m_mutex` before checking `m_callbacks.empty()`, so when no logging connection is registered, every Info-and-above entry in every kernel thread still serializes on the mutex, and at debug level statements are formatted only to be dropped. The earlier `util::log::Dispatcher` avoided this with an atomic `m_callback_count`. An atomic count updated in `RegisterCallback`/`UnregisterCallback` and checked in `ShouldLog()` would cover both cases: ```c++ bool ShouldLog(util::log::Level level) const { return m_callback_count.load(std::memory_order_relaxed) > 0 && level >= m_min_level.load(std::memory_order_relaxed); } ```
ryanofsky (Migrated from github.com) commented 2026-09-25 04:00:34 +00:00

In commit "kernel: make logging struct-based and simplify configuration" (cfd2312f0f)

Note: no change needed here, just a heads-up since it touches this function. I rebased #33847 on top of this PR, and it changes btck_logging_set_min_level to take the logging connection, with each connection filtering at its own level, plus a btck_context_options_set_logger function to say which contexts a connection receives entries from. Happy to discuss it over there so it doesn't hold up this PR.

In commit "kernel: make logging struct-based and simplify configuration" (cfd2312f0fdedcba6ba827ffa9d85d6c1d5c70f1) Note: no change needed here, just a heads-up since it touches this function. I rebased #33847 on top of this PR, and it changes `btck_logging_set_min_level` to take the logging connection, with each connection filtering at its own level, plus a `btck_context_options_set_logger` function to say which contexts a connection receives entries from. Happy to discuss it over there so it doesn't hold up this PR.
ryanofsky (Migrated from github.com) commented 2026-09-25 04:00:43 +00:00

In commit "kernel: make logging struct-based and simplify configuration" (cfd2312f0f)

Minor: after this PR the only function left that changes a logging setting is btck_logging_set_min_level, so this could be more specific e.g. "The minimum level set with btck_logging_set_min_level is global and applies to all connections."

In commit "kernel: make logging struct-based and simplify configuration" (cfd2312f0fdedcba6ba827ffa9d85d6c1d5c70f1) Minor: after this PR the only function left that changes a logging setting is `btck_logging_set_min_level`, so this could be more specific e.g. "The minimum level set with btck_logging_set_min_level is global and applies to all connections."
ryanofsky (Migrated from github.com) commented 2026-09-25 04:00:39 +00:00

In commit "kernel: expose btck_LogEntry" (94cf10c4bf)

Minor: since timestamp_ns is the only time value in the kernel API in nanoseconds (the mocktime field below it, btck_set_mock_time, and the btck_NotifyHeaderTip timestamp are all seconds), and its actual precision depends on the platform's system clock, it might help to say both in the comment:

    //! Wall-clock time the entry was logged, in nanoseconds since the Unix
    //! epoch. Unlike other time values in the kernel API, including the mocktime
    //! field below, which are in seconds, this is in nanoseconds. The actual
    //! precision depends on the platform's system clock and may be coarser
    //! (e.g. microseconds), in which case the low digits are zero.
    int64_t timestamp_ns;
In commit "kernel: expose btck_LogEntry" (94cf10c4bf8417e4543097c1e8673ad35485d1fd) Minor: since `timestamp_ns` is the only time value in the kernel API in nanoseconds (the `mocktime` field below it, `btck_set_mock_time`, and the `btck_NotifyHeaderTip` timestamp are all seconds), and its actual precision depends on the platform's system clock, it might help to say both in the comment: ```c //! Wall-clock time the entry was logged, in nanoseconds since the Unix //! epoch. Unlike other time values in the kernel API, including the mocktime //! field below, which are in seconds, this is in nanoseconds. The actual //! precision depends on the platform's system clock and may be coarser //! (e.g. microseconds), in which case the low digits are zero. int64_t timestamp_ns; ```
ryanofsky (Migrated from github.com) commented 2026-09-25 04:00:42 +00:00

In commit "kernel: make logging struct-based and simplify configuration" (cfd2312f0f)

Minor: this allows WARNING and ERROR as minimum levels, so an application can hide warnings and errors. That seems fine (an application can also just not create a logging connection), but it might be good for the doc to steer away from doing this, e.g.: "Warning and Error entries report problems that usually need attention, so setting a minimum level above Info is not recommended for most applications."

In commit "kernel: make logging struct-based and simplify configuration" (cfd2312f0fdedcba6ba827ffa9d85d6c1d5c70f1) Minor: this allows `WARNING` and `ERROR` as minimum levels, so an application can hide warnings and errors. That seems fine (an application can also just not create a logging connection), but it might be good for the doc to steer away from doing this, e.g.: "Warning and Error entries report problems that usually need attention, so setting a minimum level above Info is not recommended for most applications."
ryanofsky (Migrated from github.com) commented 2026-09-25 04:00:38 +00:00

In commit "kernel: expose btck_LogEntry" (94cf10c4bf)

Timestamp() returns a sys_time but MockTime() returns std::chrono::seconds, which a duration, even though mock time is a point in time and 0 means no mock time is set. Since LogEntry is new, it would seem more correct to return std::optional<std::chrono::sys_seconds> which is a time point not a duration, with std::nullopt when mocktime is unset.

    //! Mock time in effect when the entry was logged, or std::nullopt if none was set.
    std::optional<std::chrono::sys_seconds> MockTime() const
    {
        if (m_entry->mocktime == 0) return std::nullopt;
        return std::chrono::sys_seconds{std::chrono::seconds{m_entry->mocktime}};
    }
In commit "kernel: expose btck_LogEntry" (94cf10c4bf8417e4543097c1e8673ad35485d1fd) `Timestamp()` returns a `sys_time` but `MockTime()` returns `std::chrono::seconds`, which a duration, even though mock time is a point in time and 0 means no mock time is set. Since `LogEntry` is new, it would seem more correct to return `std::optional<std::chrono::sys_seconds>` which is a time point not a duration, with `std::nullopt` when `mocktime` is unset. ```c++ //! Mock time in effect when the entry was logged, or std::nullopt if none was set. std::optional<std::chrono::sys_seconds> MockTime() const { if (m_entry->mocktime == 0) return std::nullopt; return std::chrono::sys_seconds{std::chrono::seconds{m_entry->mocktime}}; } ```
ryanofsky (Migrated from github.com) commented 2026-01-28 21:02:26 +00:00

re: https://github.com/bitcoin/bitcoin/pull/34374#discussion_r2720709919

Jumping through a std::function when we could just be checking an atomic bitfield doesn't seem like a good approach.

Tend to agree, and I posted a minimal diff in https://github.com/bitcoin/bitcoin/pull/34374#issuecomment-3813719678 that addresses this

re: https://github.com/bitcoin/bitcoin/pull/34374#discussion_r2720709919 > Jumping through a `std::function` when we could just be checking an atomic bitfield doesn't seem like a good approach. Tend to agree, and I posted a minimal diff in https://github.com/bitcoin/bitcoin/pull/34374#issuecomment-3813719678 that addresses this
stickies-v (Migrated from github.com) reviewed 2026-09-25 16:38:01 +00:00
stickies-v (Migrated from github.com) left a comment

Thanks for the reviews, @w0xlt and @ryanofsky! Force-pushed to address review comments from @ryanofsky:

  • added an atomic callback count to KernelLogger to reduce lock acquisition and unnecessary string formatting when no callbacks are registered
  • changed LogEntry::MockTime() type to a std::optional<std::chrono::sys_seconds>, and some related docstring/name updates
  • setting the btck_LogCategory_LOCK category in intermediate commits can no longer fail an assertion when compiled without DEBUG_LOCKCONTENTION.
Thanks for the reviews, @w0xlt and @ryanofsky! Force-pushed to address review comments from @ryanofsky: - added an atomic callback count to `KernelLogger` to reduce lock acquisition and unnecessary string formatting when no callbacks are registered - changed `LogEntry::MockTime()` type to a `std::optional<std::chrono::sys_seconds>`, and some related docstring/name updates - setting the `btck_LogCategory_LOCK` category in intermediate commits can no longer fail an assertion when compiled without `DEBUG_LOCKCONTENTION`.
stickies-v (Migrated from github.com) commented 2026-09-25 16:13:21 +00:00

Going to leave this as-is. It seems (https://github.com/bitcoin/bitcoin/pull/36042) like we're reasonably close to being able to use std::format, which would be the natural way to improve things in the C++ wrapper. The bitcoin-chainstate and test_kernel timestamp output doesn't really matter much, I think.

Going to leave this as-is. It seems (https://github.com/bitcoin/bitcoin/pull/36042) like we're reasonably close to being able to use `std::format`, which would be the natural way to improve things in the C++ wrapper. The `bitcoin-chainstate` and `test_kernel` timestamp output doesn't really matter much, I think.
stickies-v (Migrated from github.com) commented 2026-09-25 16:22:31 +00:00

Fixed.

Fixed.
stickies-v (Migrated from github.com) commented 2026-09-25 16:23:33 +00:00

I explored using btcsignals earlier as per your earlier suggestion, but found it didn't completely fit the requirements yet. Could be interesting for a follow-up indeed, so I'll leave this unresolved for visibility, but agreed it shouldn't be done here.

I explored using btcsignals earlier as per your earlier suggestion, but found it didn't completely fit the requirements yet. Could be interesting for a follow-up indeed, so I'll leave this unresolved for visibility, but agreed it shouldn't be done here.
stickies-v (Migrated from github.com) commented 2026-09-25 15:38:28 +00:00

I dropped that in this branch in my quest to simplify things as much as possible, but thinking about it again I agree that the cost is small enough for the benefit, so I've added it back in again. Thanks.

I dropped that in this branch in my quest to simplify things as much as possible, but thinking about it again I agree that the cost is small enough for the benefit, so I've added it back in again. Thanks.
stickies-v (Migrated from github.com) commented 2026-09-25 16:05:54 +00:00

Going to leave this as-is. The globalness of btck_logging_set_min_level is already described in its docstring, so keeping the btck_LoggingConnection docstring to generally describe its globalness seems simpler to me.

Going to leave this as-is. The globalness of `btck_logging_set_min_level` is already described in its docstring, so keeping the `btck_LoggingConnection` docstring to generally describe its globalness seems simpler to me.
stickies-v (Migrated from github.com) commented 2026-09-25 15:41:15 +00:00

I've only slightly reworded the docstring, there is nothing the user can/should do when their platform has a coarser time, so I'd rather keep the documentation concise there. I've used this opportunity to rename mocktime -> mocktime_s for consistency and clarity, though.

I've only slightly reworded the docstring, there is nothing the user can/should do when their platform has a coarser time, so I'd rather keep the documentation concise there. I've used this opportunity to rename `mocktime` -> `mocktime_s` for consistency and clarity, though.
stickies-v (Migrated from github.com) commented 2026-09-25 16:09:23 +00:00

Going to leave this as-is. I think it's self-evident enough that warning and error logs are interesting to look at when looking at logs. Directionally, we also don't want to have log statements be part of the kernel interface, i.e. being things users must look at in order for their application to be safe or functioning correctly.

Going to leave this as-is. I think it's self-evident enough that warning and error logs are interesting to look at when looking at logs. Directionally, we also don't want to have log statements be part of the kernel interface, i.e. being things users must look at in order for their application to be safe or functioning correctly.
stickies-v (Migrated from github.com) commented 2026-09-25 16:11:01 +00:00

I wanted to not change the mocktime interface vs what we currently have, but you've changed my mind. This is the first time we're exposing this externally, and there is no point being consistent with an internal interface. Using sys_seconds now.

I wanted to not change the mocktime interface vs what we currently have, but you've changed my mind. This is the first time we're exposing this externally, and there is no point being consistent with an internal interface. Using `sys_seconds` now.
musaHaruna (Migrated from github.com) reviewed 2026-09-25 19:04:14 +00:00
musaHaruna (Migrated from github.com) left a comment

Tested 5c213d0 on macOS arm64 with a Debug build. My review was limited to the connection/disconnection message fields and cleanup when connection creation fails.

I started from the existing logging test and checked the fields attached to the connection/disconnection messages. The existing test counts these messages; I also checked their level and category i.e:

BOOST_CHECK_EQUAL(entry.message, "Logger connected.");
BOOST_CHECK(entry.level == LogLevel::DEBUG_LEVEL);
BOOST_CHECK(entry.category == LogCategory::KERNEL);

I checked the same fields for "Logger disconnecting.". All checks passed.

I also checked what happens when creating a connection fails by making a memory request fail on purpose. I found one case where the supplied data was not cleaned up, and another where the docs promise a cleanup callback that does not run when the data is null. Details inline below.

Tested [5c213d0](https://github.com/bitcoin/bitcoin/pull/34374/changes/5c213d042cb64ac9a7cb5369b2cf823e39943960) on macOS arm64 with a Debug build. My review was limited to the connection/disconnection message fields and cleanup when connection creation fails. I started from the existing logging test and checked the fields attached to the connection/disconnection messages. The existing test counts these messages; I also checked their level and category i.e: ```cpp BOOST_CHECK_EQUAL(entry.message, "Logger connected."); BOOST_CHECK(entry.level == LogLevel::DEBUG_LEVEL); BOOST_CHECK(entry.category == LogCategory::KERNEL); ``` I checked the same fields for `"Logger disconnecting."`. All checks passed. I also checked what happens when creating a connection fails by making a memory request fail on purpose. I found one case where the supplied data was not cleaned up, and another where the docs promise a cleanup callback that does not run when the data is null. Details inline below.
musaHaruna (Migrated from github.com) reviewed 2026-09-25 19:05:10 +00:00
musaHaruna (Migrated from github.com) commented 2026-09-25 19:05:10 +00:00

Could we construct UserData before calling GetKernelLogger()?

In my test, I request a connection from a global object’s constructor before main(). This triggers the logger’s first creation. I deliberately made the memory allocation for that logger fail. Connection creation returned null, but the supplied cleanup callback was not called for the non-null user data.

The issue is that GetKernelLogger() is evaluated before the UserData argument is constructed. If it throws, the data has not yet been placed in the wrapper that would clean it up.
Could we move that construction onto the preceding line, inside the existing try block?

        UserData data{user_data, {user_data_destroy_callback}};
auto handle{GetKernelLogger().RegisterCallback(
    callback, std::move(data))};
Could we construct `UserData` before calling `GetKernelLogger()`? In my test, I request a connection from a global object’s constructor before `main()`. This triggers the logger’s first creation. I deliberately made the memory allocation for that logger fail. Connection creation returned null, but the supplied cleanup callback was not called for the non-null user data. The issue is that `GetKernelLogger()` is evaluated before the `UserData` argument is constructed. If it throws, the data has not yet been placed in the wrapper that would clean it up. Could we move that construction onto the preceding line, inside the existing `try` block? ```suggestion UserData data{user_data, {user_data_destroy_callback}}; auto handle{GetKernelLogger().RegisterCallback( callback, std::move(data))}; ```
musaHaruna (Migrated from github.com) reviewed 2026-09-25 19:05:22 +00:00
musaHaruna (Migrated from github.com) commented 2026-09-25 19:05:23 +00:00

Is skipping the cleanup callback when user_data is null intended?

I passed null user data with a valid cleanup callback, then made the memory allocation for registering the callback fail. Creation returned null, but the cleanup callback was never called.

This happens because unique_ptr does not invoke its deleter when its stored pointer is null. There is no user-data object to leak here, but the docs say the cleanup callback runs on error if provided.

If skipping it is intended, could we make the extra condition explicit?

 * @return                A new kernel logging connection, or null on error. On error,
 *                             user_data_destroy_callback is invoked if both user_data and user_data_destroy_callback are non-null.
Is skipping the cleanup callback when `user_data` is null intended? I passed null user data with a valid cleanup callback, then made the memory allocation for registering the callback fail. Creation returned null, but the cleanup callback was never called. This happens because` unique_ptr` does not invoke its deleter when its stored pointer is null. There is no user-data object to leak here, but the docs say the cleanup callback runs on error if provided. If skipping it is intended, could we make the extra condition explicit? ```suggestion * @return A new kernel logging connection, or null on error. On error, * user_data_destroy_callback is invoked if both user_data and user_data_destroy_callback are non-null. ```
stickies-v (Migrated from github.com) reviewed 2026-09-25 21:50:48 +00:00
stickies-v (Migrated from github.com) commented 2026-09-25 21:50:48 +00:00

GetKernelLogger() just returns a reference to a static variable, so I think this is fine either way, but I'm happy to change it if I have to force-push. This feels like a nit to me so I'll consider it non-blocking unless I'm missing something?

`GetKernelLogger()` just returns a reference to a static variable, so I think this is fine either way, but I'm happy to change it if I have to force-push. This feels like a nit to me so I'll consider it non-blocking unless I'm missing something?
stickies-v (Migrated from github.com) reviewed 2026-09-25 22:01:16 +00:00
stickies-v (Migrated from github.com) commented 2026-09-25 22:01:16 +00:00

user_data_destroy_callback is defined as "Nullable, function for freeing the user data.". If user_data is null, there is nothing to free, so indeed user_data_destroy_callback should not be called.

A null user_data and non-null user_data_destroy_callback is nonsensical and clearly a programmer error, so I think the correct approach is to assert this in btck_logging_connection_create and update the user_data_destroy_callback docstring there. I thought we were already doing this, but at least it seems it's no longer in the current branch so I'll fix that!

`user_data_destroy_callback` is defined as "Nullable, function for freeing the user data.". If `user_data` is null, there is nothing to free, so indeed `user_data_destroy_callback` should not be called. A null `user_data` and non-null `user_data_destroy_callback` is nonsensical and clearly a programmer error, so I think the correct approach is to assert this in `btck_logging_connection_create` and update the `user_data_destroy_callback` docstring there. I thought we were already doing this, but at least it seems it's no longer in the current branch so I'll fix that!
stickies-v commented 2026-09-28 10:29:42 +00:00 (Migrated from github.com)

Force-pushed to address review comments from @musaHaruna:

  • construct UserData before calling GetKernelLogger() to ensure user_data is always freed on error
  • update user_data_destroy_callback docstring to highlight that the destroy cb is not called when user_data is null
Force-pushed to address review comments from @musaHaruna: - construct `UserData` before calling `GetKernelLogger()` to ensure `user_data` is always freed on error - update `user_data_destroy_callback` docstring to highlight that the destroy cb is not called when `user_data` is null
stickies-v (Migrated from github.com) reviewed 2026-09-28 10:29:55 +00:00
stickies-v (Migrated from github.com) commented 2026-09-28 10:29:55 +00:00

Fixed, thanks!

Fixed, thanks!
stickies-v (Migrated from github.com) reviewed 2026-09-28 10:33:07 +00:00
stickies-v (Migrated from github.com) commented 2026-09-28 10:33:07 +00:00

I've changed my mind on this, and adopted your docstring updated, thanks! For btck_ValidationInterfaceCallbacks and btck_NotificationInterfaceCallbacks, we don't assert that user_data_destroy is null for a null user_data, and I don't have a strong enough view on having a different approach here to introduce the inconsistency. It still feels strange to have this happen, but perhaps there are valid reasons for it that I haven't thought of, so just documenting it now seems reasonable.

I've changed my mind on this, and adopted your docstring updated, thanks! For `btck_ValidationInterfaceCallbacks` and `btck_NotificationInterfaceCallbacks`, we don't assert that `user_data_destroy` is null for a null `user_data`, and I don't have a strong enough view on having a different approach here to introduce the inconsistency. It still feels strange to have this happen, but perhaps there are valid reasons for it that I haven't thought of, so just documenting it now seems reasonable.
ryanofsky (Migrated from github.com) approved these changes 2026-09-29 22:36:49 +00:00
ryanofsky (Migrated from github.com) left a comment

Code review ACK ad66992952. Since my last review: added an atomic callback count so nothing is locked or formatted when no connection is registered, MockTime() returns an optional time point, the LOCK category no longer asserts in intermediate commits, and user_data is now freed if getting the kernel logger throws.

Thanks for considering the other suggestions, the reasons for leaving them out make sense to me.

Code review ACK ad66992952e5e465a2aa0f27e956e381f17ad759. Since my last review: added an atomic callback count so nothing is locked or formatted when no connection is registered, `MockTime()` returns an optional time point, the `LOCK` category no longer asserts in intermediate commits, and `user_data` is now freed if getting the kernel logger throws. Thanks for considering the other suggestions, the reasons for leaving them out make sense to me.
ryanofsky (Migrated from github.com) commented 2026-09-29 22:18:52 +00:00

In commit "kernel: add KernelLogger" (bf1f4cb09d)

Since this PR drops category filtering, I think it would be good to replace both btck_LogCategory_ALL and btck_LogCategory_UNKNOWN with a single btck_LogCategory_NONE:

  • ALL made sense as a filter argument ("enable all categories"), but as the category of a delivered entry it is confusing. Log messages with no category are not from every category, they just don't have a category.
  • UNKNOWN only exists as the release-build fallback for an unmapped category, which is a kernel bug, not something kernel applications using our API should have to handle. We should handle it ourselves and not expose this externally by mapping these cases to NONE.

Specific suggestion here would be to drop ALL and UNKNOWN and add NONE instead like:

#define btck_LogCategory_NONE ((btck_LogCategory)(0)) //!< Uncategorized, e.g. most INFO, WARNING and ERROR entries.
    case BCLog::LogFlags::ALL: {
        return btck_LogCategory_NONE;
    }
    ...
    default: {
        // Every category the kernel library can emit must be mapped above, either to its own
        // btck_LogCategory or to btck_LogCategory_NONE. This cannot be enforced at compile time,
        // so abort in debug builds to catch it early, and deliver the entry as NONE otherwise.
        Assume(false);
        return btck_LogCategory_NONE;
    }

The wrapper's LogCategory::ALL/UNKNOWN and their Name() strings would change the same way.

In commit "kernel: add KernelLogger" (bf1f4cb09d1273bdee28dc13aab598b94d3973c8) Since this PR drops category filtering, I think it would be good to replace both `btck_LogCategory_ALL` and `btck_LogCategory_UNKNOWN` with a single `btck_LogCategory_NONE`: - `ALL` made sense as a filter argument ("enable all categories"), but as the category of a delivered entry it is confusing. Log messages with no category are not from every category, they just don't have a category. - `UNKNOWN` only exists as the release-build fallback for an unmapped category, which is a kernel bug, not something kernel applications using our API should have to handle. We should handle it ourselves and not expose this externally by mapping these cases to NONE. Specific suggestion here would be to drop ALL and UNKNOWN and add NONE instead like: ```c #define btck_LogCategory_NONE ((btck_LogCategory)(0)) //!< Uncategorized, e.g. most INFO, WARNING and ERROR entries. ``` ```c++ case BCLog::LogFlags::ALL: { return btck_LogCategory_NONE; } ... default: { // Every category the kernel library can emit must be mapped above, either to its own // btck_LogCategory or to btck_LogCategory_NONE. This cannot be enforced at compile time, // so abort in debug builds to catch it early, and deliver the entry as NONE otherwise. Assume(false); return btck_LogCategory_NONE; } ``` The wrapper's `LogCategory::ALL`/`UNKNOWN` and their `Name()` strings would change the same way.
ryanofsky (Migrated from github.com) commented 2026-09-29 22:18:53 +00:00

re: https://github.com/bitcoin/bitcoin/pull/34374#discussion_r4121137413

In commit "kernel: make logging struct-based and simplify configuration" (ad66992952)

Nit, only if you push again: with the "Not called if user_data is null" note on the parameter, the @return sentence could be dropped or made to match, e.g. "On error, user_data is freed as described above." Right now if read literally it would imply the callback can be called with a null user_data on error.

re: https://github.com/bitcoin/bitcoin/pull/34374#discussion_r4121137413 In commit "kernel: make logging struct-based and simplify configuration" (ad66992952e5e465a2aa0f27e956e381f17ad759) Nit, only if you push again: with the "Not called if user_data is null" note on the parameter, the `@return` sentence could be dropped or made to match, e.g. "On error, user_data is freed as described above." Right now if read literally it would imply the callback can be called with a null `user_data` on error.
First-time contributor

Base: 69142eacd1374925cc9e4ea736c21fcec16307d0
Head: eb086540b680cfed42ab7f9498c1ade459828808

Design and approach

Confirm the cost of formatting all Debug and Trace categories (src/kernel/bitcoinkernel.cpp:972, head)

When Debug or Trace is enabled and a callback is registered, these hooks return true for every category. Logging macros then evaluate arguments and format messages before the callback can filter them. The base logger could suppress disabled categories earlier, and kernel-linked code has category-specific Debug logging, including LEVELDB and MEMPOOL. Is formatting every category acceptable for intended kernel consumers, or should a category prefilter remain? Consumer requirements or representative workload measurements would settle the tradeoff. A prefilter would add configuration state but preserve early suppression.

🟡 Minor

Messages ending in multiple newlines retain a trailing newline (src/kernel/bitcoinkernel.cpp:592, head)

For a message ending in two or more \n characters, RemoveSuffixView removes only one, so the callback still receives a trailing newline despite the stated newline-free message behavior. Remove all trailing newlines or document that only one suffix newline is removed. No current kernel log producer ending in multiple newlines was identified, so production impact is unconfirmed.

💡 Suggestion

Test teardown during an in-flight callback and pre-registration logging (src/test/kernel/test_kernel.cpp:717, head)

This test destroys connections only after callbacks return, so it does not verify that destruction waits for an in-flight callback. Add a synchronized test that blocks a callback, destroys its connection on another thread, and verifies the user-data destroy callback runs only after the blocked callback is released. Also test that a log entry emitted before registration is not replayed to a later connection.

Review coverage was incomplete. The archaeology review had incomplete evidence. The adversarial review had incomplete evidence. The adversarial_glm review had incomplete evidence. Verification was partial. Some candidate findings remain unresolved.

Full review report

<!-- forgejo-review-bot:bitcoin/bitcoin --> Base: `69142eacd1374925cc9e4ea736c21fcec16307d0` Head: `eb086540b680cfed42ab7f9498c1ade459828808` ##### Design and approach **Confirm the cost of formatting all Debug and Trace categories** (src/kernel/bitcoinkernel.cpp:972, head) When Debug or Trace is enabled and a callback is registered, these hooks return true for every category. Logging macros then evaluate arguments and format messages before the callback can filter them. The base logger could suppress disabled categories earlier, and kernel-linked code has category-specific Debug logging, including LEVELDB and MEMPOOL. Is formatting every category acceptable for intended kernel consumers, or should a category prefilter remain? Consumer requirements or representative workload measurements would settle the tradeoff. A prefilter would add configuration state but preserve early suppression. ##### 🟡 Minor **Messages ending in multiple newlines retain a trailing newline** (src/kernel/bitcoinkernel.cpp:592, head) For a message ending in two or more `\n` characters, RemoveSuffixView removes only one, so the callback still receives a trailing newline despite the stated newline-free message behavior. Remove all trailing newlines or document that only one suffix newline is removed. No current kernel log producer ending in multiple newlines was identified, so production impact is unconfirmed. ##### 💡 Suggestion **Test teardown during an in-flight callback and pre-registration logging** (src/test/kernel/test_kernel.cpp:717, head) This test destroys connections only after callbacks return, so it does not verify that destruction waits for an in-flight callback. Add a synchronized test that blocks a callback, destroys its connection on another thread, and verifies the user-data destroy callback runs only after the blocked callback is released. Also test that a log entry emitted before registration is not replayed to a later connection. Review coverage was incomplete. The archaeology review had incomplete evidence. The adversarial review had incomplete evidence. The adversarial_glm review had incomplete evidence. Verification was partial. Some candidate findings remain unresolved. [Full review report](<https://review.fish.foo/traces/34374-eb086540b680cfed42ab7f9498c1ade459828808-300-2.html>)
w0xlt (Migrated from github.com) reviewed 2026-09-29 22:58:16 +00:00
w0xlt (Migrated from github.com) commented 2026-09-29 22:37:26 +00:00

The build passes -fmacro-prefix-map=${PROJECT_SOURCE_DIR}/src=. (CMakeLists.txt), so source file names are expanded with a leading ./. The node strips it when formatting source locations (RemovePrefixView(entry.source_loc.file_name(), "./") in BCLog::Logger::Format()), but KernelLogger::Log passes the file name through unchanged, so kernel consumers get ./validation.cpp where node logs show validation.cpp.

    // Strip the "./" prefix added by -fmacro-prefix-map, matching the node's log output.
    std::string_view file_name{util::RemovePrefixView(entry.source_loc.file_name(), "./")};

btck_LogEntry::file_name as seen by a TRACE-level logging connection, before and after:

BEFORE: file=./kernel/bitcoinkernel.cpp msg=Logger connected.
AFTER:  file=kernel/bitcoinkernel.cpp msg=Logger connected.
The build passes `-fmacro-prefix-map=${PROJECT_SOURCE_DIR}/src=.` (`CMakeLists.txt`), so source file names are expanded with a leading `./`. The node strips it when formatting source locations (`RemovePrefixView(entry.source_loc.file_name(), "./")` in `BCLog::Logger::Format()`), but `KernelLogger::Log` passes the file name through unchanged, so kernel consumers get `./validation.cpp` where node logs show `validation.cpp`. ```suggestion // Strip the "./" prefix added by -fmacro-prefix-map, matching the node's log output. std::string_view file_name{util::RemovePrefixView(entry.source_loc.file_name(), "./")}; ``` `btck_LogEntry::file_name` as seen by a TRACE-level logging connection, before and after: ```txt BEFORE: file=./kernel/bitcoinkernel.cpp msg=Logger connected. AFTER: file=kernel/bitcoinkernel.cpp msg=Logger connected.
w0xlt (Migrated from github.com) commented 2026-09-29 22:26:44 +00:00

The PR contains a subtle, undocumented behaviour change.

On master, btck_logging_connection_create registered the consumer's callback on the node's logger via LogInstance().PushBackCallback(...). Each log line then went through BCLog::Logger::Format(), which calls LogEscapeMessage(entry.message), before reaching the callback. Consumers got the escaping for free, because they received the same formatted line the node writes to debug.log.

With this PR, the kernel has its own logger. BCLog::Logger no longer sits between the log statement and the kernel callback, and logging.cpp is no longer compiled into the kernel, so LogEscapeMessage() never runs.

As I understand it, the point of the PR is to give consumers structured fields instead of a pre-formatted line, so escaping, like timestamp and prefix formatting, becomes the consumer's job. Could the docs say this explicitly?

diff --git a/src/kernel/bitcoinkernel.h b/src/kernel/bitcoinkernel.h
index 192b276a95..df890488de 100644
--- a/src/kernel/bitcoinkernel.h
+++ b/src/kernel/bitcoinkernel.h
@@ -389,7 +389,8 @@ typedef uint8_t btck_LogLevel;
  * accompanying *_len field to determine their length.
  */
 typedef struct {
-    //! Log message text.
+    //! Log message text. It is not escaped and may contain control characters,
+    //! so sanitize it before writing it to a terminal or log file.
     const char* message;
     //! Length of message.
     size_t message_len;
The PR contains a subtle, undocumented behaviour change. On master, `btck_logging_connection_create` registered the consumer's callback on the node's logger via `LogInstance().PushBackCallback(...)`. Each log line then went through `BCLog::Logger::Format()`, which calls `LogEscapeMessage(entry.message)`, before reaching the callback. Consumers got the escaping for free, because they received the same formatted line the node writes to `debug.log`. With this PR, the kernel has its own logger. `BCLog::Logger` no longer sits between the log statement and the kernel callback, and `logging.cpp` is no longer compiled into the kernel, so `LogEscapeMessage()` never runs. As I understand it, the point of the PR is to give consumers structured fields instead of a pre-formatted line, so escaping, like timestamp and prefix formatting, becomes the consumer's job. Could the docs say this explicitly? ```diff diff --git a/src/kernel/bitcoinkernel.h b/src/kernel/bitcoinkernel.h index 192b276a95..df890488de 100644 --- a/src/kernel/bitcoinkernel.h +++ b/src/kernel/bitcoinkernel.h @@ -389,7 +389,8 @@ typedef uint8_t btck_LogLevel; * accompanying *_len field to determine their length. */ typedef struct { - //! Log message text. + //! Log message text. It is not escaped and may contain control characters, + //! so sanitize it before writing it to a terminal or log file. const char* message; //! Length of message. size_t message_len; ```
w0xlt (Migrated from github.com) commented 2026-09-29 22:04:27 +00:00

nit:

diff --git a/src/kernel/bitcoinkernel.h b/src/kernel/bitcoinkernel.h
index 192b276a95..7fbbc8d7b4 100644
--- a/src/kernel/bitcoinkernel.h
+++ b/src/kernel/bitcoinkernel.h
@@ -422,8 +422,9 @@ typedef struct {
 /** Callback function types */
 
 /**
- * Function signature for the global logging callback. All bitcoin kernel
- * internal logs will pass through this callback.
+ * Function signature for the global logging callback. Receives kernel log
+ * entries at or above the minimum level set with @ref btck_logging_set_min_level
+ * that are logged while its btck_LoggingConnection exists.
  *
  * The callback may be invoked from any thread. Invocations are serialized: no
  * two invocations run at the same time, and destroying a btck_LoggingConnection
nit: ```diff diff --git a/src/kernel/bitcoinkernel.h b/src/kernel/bitcoinkernel.h index 192b276a95..7fbbc8d7b4 100644 --- a/src/kernel/bitcoinkernel.h +++ b/src/kernel/bitcoinkernel.h @@ -422,8 +422,9 @@ typedef struct { /** Callback function types */ /** - * Function signature for the global logging callback. All bitcoin kernel - * internal logs will pass through this callback. + * Function signature for the global logging callback. Receives kernel log + * entries at or above the minimum level set with @ref btck_logging_set_min_level + * that are logged while its btck_LoggingConnection exists. * * The callback may be invoked from any thread. Invocations are serialized: no * two invocations run at the same time, and destroying a btck_LoggingConnection ```
w0xlt (Migrated from github.com) commented 2026-09-29 21:53:22 +00:00

nit:

diff --git a/src/kernel/bitcoinkernel.h b/src/kernel/bitcoinkernel.h
index 192b276a95..d53cb02c6b 100644
--- a/src/kernel/bitcoinkernel.h
+++ b/src/kernel/bitcoinkernel.h
@@ -954,7 +954,8 @@ BITCOINKERNEL_API void btck_logging_set_min_level(btck_LogLevel level);
  * @param[in] user_data_destroy_callback Nullable, function for freeing the user data. Not called
  *                                       if user_data is null.
  * @return                               A new kernel logging connection, or null on error. On error,
- *                                       user_data_destroy_callback is invoked if it is provided.
+ *                                       user_data_destroy_callback is invoked if both it and user_data
+ *                                       are non-null.
  */
 BITCOINKERNEL_API btck_LoggingConnection* BITCOINKERNEL_WARN_UNUSED_RESULT btck_logging_connection_create(
     btck_LogCallback log_callback,
nit: ```diff diff --git a/src/kernel/bitcoinkernel.h b/src/kernel/bitcoinkernel.h index 192b276a95..d53cb02c6b 100644 --- a/src/kernel/bitcoinkernel.h +++ b/src/kernel/bitcoinkernel.h @@ -954,7 +954,8 @@ BITCOINKERNEL_API void btck_logging_set_min_level(btck_LogLevel level); * @param[in] user_data_destroy_callback Nullable, function for freeing the user data. Not called * if user_data is null. * @return A new kernel logging connection, or null on error. On error, - * user_data_destroy_callback is invoked if it is provided. + * user_data_destroy_callback is invoked if both it and user_data + * are non-null. */ BITCOINKERNEL_API btck_LoggingConnection* BITCOINKERNEL_WARN_UNUSED_RESULT btck_logging_connection_create( btck_LogCallback log_callback, ```
w0xlt (Migrated from github.com) commented 2026-09-29 22:09:29 +00:00

Dead code (can be amended into ad66992952).

diff --git a/src/logging.h b/src/logging.h
index fed246fb9a..87c5883c25 100644
--- a/src/logging.h
+++ b/src/logging.h
@@ -202,12 +202,6 @@ namespace BCLog {
             m_print_callbacks.erase(it);
         }
 
-        size_t NumConnections() EXCLUSIVE_LOCKS_REQUIRED(!m_cs)
-        {
-            STDLOCK(m_cs);
-            return m_print_callbacks.size();
-        }
-
         /** Start logging (and flush all buffered messages) */
         bool StartLogging() EXCLUSIVE_LOCKS_REQUIRED(!m_cs);
         /** Only for testing */
Dead code (can be amended into ad66992952). ```diff diff --git a/src/logging.h b/src/logging.h index fed246fb9a..87c5883c25 100644 --- a/src/logging.h +++ b/src/logging.h @@ -202,12 +202,6 @@ namespace BCLog { m_print_callbacks.erase(it); } - size_t NumConnections() EXCLUSIVE_LOCKS_REQUIRED(!m_cs) - { - STDLOCK(m_cs); - return m_print_callbacks.size(); - } - /** Start logging (and flush all buffered messages) */ bool StartLogging() EXCLUSIVE_LOCKS_REQUIRED(!m_cs); /** Only for testing */ ```
stickies-v (Migrated from github.com) reviewed 2026-10-02 15:52:33 +00:00
stickies-v (Migrated from github.com) left a comment

Force-pushed to address review comments from @ryanofsky and @w0xlt:

  • replace btck_LogCategory_ALL and btck_LogCategory_UNKNOWN with a single btck_LogCategory_NONE
  • document that btck_LogEntry::message is not escaped
  • docstring fixes for btck_LogCallback and btck_logging_connection_create
  • remove the now-unused BCLog::Logger::NumConnections()
Force-pushed to address review comments from @ryanofsky and @w0xlt: - replace `btck_LogCategory_ALL` and `btck_LogCategory_UNKNOWN` with a single `btck_LogCategory_NONE` - document that `btck_LogEntry::message` is not escaped - docstring fixes for `btck_LogCallback` and `btck_logging_connection_create` - remove the now-unused `BCLog::Logger::NumConnections()`
stickies-v (Migrated from github.com) commented 2026-10-02 15:13:52 +00:00

I went back and forth on this issue, and opted for UNKNOWN as to make it easier for users to catch this bug since we cannot enforce it at compile-time.

You've changed my mind, and I've now adopted your suggestion. It is still our responsibility to make sure unmapped categories don't happen, we have some ability to do so with Assume, and polluting the public interface with something they should never see and should only minimally affect how they use the library is a bit excessive. Thanks!

I went back and forth on this issue, and opted for `UNKNOWN` as to make it easier for users to catch this bug since we cannot enforce it at compile-time. You've changed my mind, and I've now adopted your suggestion. It is still our responsibility to make sure unmapped categories don't happen, we have some ability to do so with `Assume`, and polluting the public interface with something they should never see and should only minimally affect how they use the library is a bit excessive. Thanks!
stickies-v (Migrated from github.com) commented 2026-10-02 15:10:42 +00:00

I don't think we need to match node behaviour here. It seems natural to use the path provided by the compiler, and whoever builds the library can control how source locations are returned through compiler flags (e.g. -fmacro-prefix-map). The ./ prefix is also compiler-specific: with clang, the default build already returns validation.cpp. So I think it makes sense to leave this as-is?

I don't think we need to match node behaviour here. It seems natural to use the path provided by the compiler, and whoever builds the library can control how source locations are returned through compiler flags (e.g. `-fmacro-prefix-map`). The `./` prefix is also compiler-specific: with clang, the default build already returns `validation.cpp`. So I think it makes sense to leave this as-is?
stickies-v (Migrated from github.com) commented 2026-10-02 15:17:12 +00:00

Thanks, slightly updated the docstring to clarify it's unsanitized but I don't want to be overly verbose/prescriptive either.

Thanks, slightly updated the docstring to clarify it's unsanitized but I don't want to be overly verbose/prescriptive either.
stickies-v (Migrated from github.com) commented 2026-10-02 15:21:08 +00:00

Thanks, updated!

Thanks, updated!
stickies-v (Migrated from github.com) commented 2026-10-02 15:21:36 +00:00

Thanks, updated!

Thanks, updated!
stickies-v (Migrated from github.com) commented 2026-10-02 15:21:18 +00:00

Thanks, updated!

Thanks, updated!
stickies-v (Migrated from github.com) commented 2026-10-02 15:21:49 +00:00

Good catch - removed it, thanks!

Good catch - removed it, thanks!
w0xlt (Migrated from github.com) reviewed 2026-10-02 22:56:24 +00:00
w0xlt (Migrated from github.com) left a comment
ACK eb086540b680cfed42ab7f9498c1ade459828808
musaHaruna (Migrated from github.com) reviewed 2026-10-03 10:49:23 +00:00
musaHaruna (Migrated from github.com) left a comment

Code Review ACK eb08654

Thanks for incorporating my suggestions @stickies-v.

Code Review ACK [eb08654](https://github.com/bitcoin/bitcoin/commit/eb086540b680cfed42ab7f9498c1ade459828808) Thanks for incorporating my suggestions @stickies-v.
yuvicc commented 2026-10-07 10:12:14 +00:00 (Migrated from github.com)

Concept ACK

I've had issues parsing logs when using the kernel API, the changes here looks structured and useful improvement for kernel API users.

Concept ACK I've had issues parsing logs when using the kernel API, the changes here looks structured and useful improvement for kernel API users.
sedited (Migrated from github.com) approved these changes 2026-10-08 11:37:01 +00:00
sedited (Migrated from github.com) left a comment

Nice, ACK eb086540b6

There are some things that could still be worked out, like enabling debug logging only when the category is set, and settling the global vs. instantiated logging question, but we can have those in the coming weeks.

Nice, ACK eb086540b680cfed42ab7f9498c1ade459828808 There are some things that could still be worked out, like enabling debug logging only when the category is set, and settling the global vs. instantiated logging question, but we can have those in the coming weeks.
sedited (Migrated from github.com) commented 2026-10-07 13:25:10 +00:00

Might this be better as an opaque struct? I'm not convinced actually, since these are mostly cheap views into the data, and it shouldn't matter much if they need to be copied from this struct or through getter functions.

Might this be better as an opaque struct? I'm not convinced actually, since these are mostly cheap views into the data, and it shouldn't matter much if they need to be copied from this struct or through getter functions.
stickies-v (Migrated from github.com) reviewed 2026-10-08 12:55:18 +00:00
stickies-v (Migrated from github.com) commented 2026-10-08 12:55:18 +00:00

I suppose an opaque struct would be better for ABI stability and consistency of the interface, and I think it might be a bit more straightforward for FFI. Performance seems to be a bit worse (few nanoseconds per callback), but compared to the cost of string formatting etc is not super meaningful. Specifically, there's more indirection for every field that's accessed, but fields that aren't accessed aren't constructed for nothing.

I don't have a strong view either way. I initially felt like an opaque struct would require an unreasonably large API surface for the functionality, but it does seem like the mildly better approach. The increased API surface is trivial in nature so I don't think it's that bad either.

Unless people feel strongly about this, would prefer leaving as-is and address in a quick follow-up if there's appetite for it? This PR has had a lot of activity and revisions already.

I suppose an opaque struct would be better for ABI stability and consistency of the interface, and I think it might be a bit more straightforward for FFI. Performance seems to be a bit worse (few nanoseconds per callback), but compared to the cost of string formatting etc is not super meaningful. Specifically, there's more indirection for every field that's accessed, but fields that aren't accessed aren't constructed for nothing. I don't have a strong view either way. I initially felt like an opaque struct would require an unreasonably large API surface for the functionality, but it does seem like the mildly better approach. The increased API surface is trivial in nature so I don't think it's that bad either. Unless people feel strongly about this, would prefer leaving as-is and address in a quick follow-up if there's appetite for it? This PR has had a lot of activity and revisions already.
yuvicc commented 2026-10-09 04:12:23 +00:00 (Migrated from github.com)

post-merge ACK eb086540b6

post-merge ACK eb086540b680cfed42ab7f9498c1ade459828808
stickies-v (Migrated from github.com) reviewed 2026-10-20 18:36:53 +00:00
stickies-v (Migrated from github.com) commented 2026-10-20 18:36:53 +00:00

Done in #36584

Done in #36584
Sign in to join this conversation.
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!34374
No description provided.