logging: rewrite macros to enforce restrictions at compile-time, improve efficiency and usability #34778

Open
ryanofsky wants to merge 6 commits from pr/relog into master AGit
ryanofsky commented 2026-03-09 15:28:25 +00:00 (Migrated from github.com)

This PR is an internal cleanup for util/log.h that fixes several problems with the logging macros defined there.

The main change is replacing the internal detail_LogWithSrcLoc macro with a LOG_EMIT macro. It makes all the external logging macros (LogDebug, LogInfo, etc.) call LOG_EMIT, eliminating inconsistencies, improving efficiency, and producing clearer compiler errors when called incorrectly.

Specific problems this PR fixes:

  • Current logging macros duplicate logic and have undocumented, untested differences. For example, some macros evaluate their arguments when logging is disabled and others do not. By constrast, the LOG_EMIT macro implements logging logic in one place, preventing hidden inconsistencies.

  • Current logging macros produce hard to decipher syntax errors when category arguments are specified but not allowed, or required but not provided. Now all logging macros produce clear static_assert errors indicating whether to add or remove category arguments.

  • Current LogInfo, LogWarning, and LogError macros generate log entries and call util::log::Log(Entry) even when logging is disabled. LOG_EMIT avoids this overhead, which is probably minor but could improve performance in some kernel applications and fuzzing.

  • Current detail_LogWithSrcLoc macro is dangerous to call because because it does not check for inconsistent or invalid arguments. For example, calling it always bypasses ratelimiting without a check to prevent this. (Previously there was a runtime check, but it was removed without justification in 34332dba2f). New LOG_EMIT macro is compile-time safe and all the same restrictions as other macros.

  • Having multiple implementations of logging macros makes it difficult to add new logging features (see below), because it requires changing many macros and adding separate macro arguments for each feature. Having a single core LOG_EMIT macro taking a single compile-time options argument makes it possible to add new features cleanly.

Individual commits also implement other improvements: adding missing test coverage and removing misleading Entry::should_ratelimit field which is stored in buffered log messages after rate-limiting has been applied, and has no effect.


Possible followups

This PR does not add any new logging features. It is purely an internal cleanup that fixes the problems listed above. However, it is worth noting that this PR could make it easier to add logging features in the future. For example:

  1. New logging options could be added. Examples: .stacktrace = true to log the current stack trace, .exception = true to log the current exception, .impossible = true to indicate an impossible condition and halt fuzzing or request a bug report, .log_once = true to log at most once from this source location, .log_every = 60m to log at most once per hour, .dedup = true to drop duplicste messages, etc.

  2. External macros like LogInfo and LogDebug could accept these options. Example: f5ca95b1e610ebad7063143ddef98fc318251bc4

  3. Context information could be added to log messages, e.g., wallet names, chainstate names, or request IDs to distinguish logs from paralell requests. Example: #30343

  4. Non-global logging streams could also be supported. For example, kernel functions could accept optional logger arguments instead of always writing to the global logger. Example: #30342

This PR is an internal cleanup for `util/log.h` that fixes several problems with the logging macros defined there. The main change is replacing the internal `detail_LogWithSrcLoc` macro with a `LOG_EMIT` macro. It makes all the external logging macros (`LogDebug`, `LogInfo`, etc.) call `LOG_EMIT`, eliminating inconsistencies, improving efficiency, and producing clearer compiler errors when called incorrectly. Specific problems this PR fixes: - Current logging macros duplicate logic and have undocumented, untested differences. For example, some macros evaluate their arguments when logging is disabled and others do not. By constrast, the `LOG_EMIT` macro implements logging logic in one place, preventing hidden inconsistencies. - Current logging macros produce hard to decipher syntax errors when category arguments are specified but not allowed, or required but not provided. Now all logging macros produce clear `static_assert` errors indicating whether to add or remove category arguments. - Current `LogInfo`, `LogWarning`, and `LogError` macros generate log entries and call `util::log::Log(Entry)` even when logging is disabled. `LOG_EMIT` avoids this overhead, which is probably minor but could improve performance in some kernel applications and fuzzing. - Current `detail_LogWithSrcLoc` macro is dangerous to call because because it does not check for inconsistent or invalid arguments. For example, calling it always bypasses ratelimiting without a check to prevent this. (Previously there was a runtime check, but it was removed without justification in 34332dba2f6f6892859ecb62daa9a2add76ae05e). New `LOG_EMIT` macro is compile-time safe and all the same restrictions as other macros. - Having multiple implementations of logging macros makes it difficult to add new logging features (see below), because it requires changing many macros and adding separate macro arguments for each feature. Having a single core `LOG_EMIT` macro taking a single compile-time `options` argument makes it possible to add new features cleanly. Individual commits also implement other improvements: adding missing test coverage and removing misleading `Entry::should_ratelimit` field which is stored in buffered log messages after rate-limiting has been applied, and has no effect. --- #### Possible followups This PR does not add any new logging features. It is purely an internal cleanup that fixes the problems listed above. However, it is worth noting that this PR could make it easier to add logging features in the future. For example: 1. New logging options could be added. Examples: `.stacktrace = true` to log the current stack trace, `.exception = true` to log the current exception, `.impossible = true` to indicate an impossible condition and halt fuzzing or request a bug report, `.log_once = true` to log at most once from this source location, `.log_every = 60m` to log at most once per hour, `.dedup = true` to drop duplicste messages, etc. 2. External macros like `LogInfo` and `LogDebug` could accept these options. Example: f5ca95b1e610ebad7063143ddef98fc318251bc4 3. Context information could be added to log messages, e.g., wallet names, chainstate names, or request IDs to distinguish logs from paralell requests. Example: [#30343](https://github.com/bitcoin/bitcoin/pull/30343) 4. Non-global logging streams could also be supported. For example, kernel functions could accept optional logger arguments instead of always writing to the global logger. Example: [#30342](https://github.com/bitcoin/bitcoin/pull/30342)
DrahtBot commented 2026-03-09 15:29:06 +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
Concept NACK ajtowns

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:

  • #37038 (coins: store spent dbcache entries compactly by LarryRuane)
  • #36904 (log: Remove trailing and embedded newlines from log messages by maflcko)
  • #35355 (Use atomics for determining whether trace logging is enabled by ajtowns)
  • #35322 (logging: streamline Logger state and drop redundant methods by ryanofsky)
  • #34775 (kernel: allow only one global logging connection by stickies-v)

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/34778 * Ralph review <img src=https://review.fish.foo/assets/bitcoin/bitcoin/34778.png alt="Ralph review status"> https://git.fish.foo/bitcoin/bitcoin/pulls/34778#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 | | ---- | --------- | | Concept NACK | [ajtowns](https://github.com/bitcoin/bitcoin/pull/34778#issuecomment-4030462014) | 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: * [#37038](https://github.com/bitcoin/bitcoin/pull/37038) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/37038.svg"></sub> (coins: store spent dbcache entries compactly by LarryRuane) * [#36904](https://github.com/bitcoin/bitcoin/pull/36904) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/36904.svg"></sub> (log: Remove trailing and embedded newlines from log messages by maflcko) * [#35355](https://github.com/bitcoin/bitcoin/pull/35355) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/35355.svg"></sub> (Use atomics for determining whether trace logging is enabled by ajtowns) * [#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) * [#34775](https://github.com/bitcoin/bitcoin/pull/34775) <sub><img src="https://drahtbot.space/ack_count/bitcoin/bitcoin/34775.svg"></sub> (kernel: allow only one global logging connection by stickies-v) If you consider this pull request important, please also help to review the conflicting pull requests. Ideally, start with the one that should be merged first. <!--5faf32d7da4f0f540f40219e4f7537a3-->
ajtowns commented 2026-03-10 10:46:49 +00:00 (Migrated from github.com)

Concept NACK to reintroducing a generic LogPrint. The NO_RATE_LIMIT commit from #34038 has been available for review for three months now, and has had a concept ack for that long.

Concept NACK to reintroducing a generic `LogPrint`. The `NO_RATE_LIMIT` commit from #34038 has been available for review for three months now, and has had a concept ack for that long.
ryanofsky commented 2026-03-10 13:43:13 +00:00 (Migrated from github.com)

The NO_RATE_LIMIT commit from #34038 has been available for review for three months now, and has had a concept ack for that long.

That's interesting. #34038 is mostly a logging backend change, changing the RPC interface and command line options. It seems like the no-ratelimit change part of that PR might not be related to the other changes there? (I don't see the connection at least.)

To compare the two approaches, #34038 syntax looks like:

LogInfo(util::log::NO_RATE_LIMIT, "message");

while syntax here is:

LogPrint((.level = util::log::Level::Info, .ratelimit = false), "message"),

There is not much difference between the approaches externally, and they are compatible (this PR could easily support a NO_RATE_LIMIT flag if reviewers would prefer that.)

This bigger goal of this PR is to replace current implementation of the logging macros which is messy and inconsistent with a clean implementation. All preprocessor functionality is implemented in a single macro called LogPrint, getting rid of the branching code paths at different priority levels [1] [2] that have unexplained differences. The differences are all kept, they are just explicit rather than implicit, with better documentation and tests and stricter enforcement of restrictions.

The .ratelimit = true option is exposed as a side-effect of this refactoring, and the refactoring also allows other options to be implemented cleanly in the future. Examples could be .log_once=true .log_every=24h .log_every=60m .stack_trace=true .dedup = true etc. I was also thinking of an .impossible = true option that could be used to document preconditions & provide better bug reports and output from fuzz & unit test failures. The point is that I think we should make it easy to add logging options and try to implement most options with constexpr code instead of macros. And get rid of complication of different macros at different log levels having different implementations.

Anyway I'd be happy to review #34038 and I think the runtime changes there look nice. The NO_RATE_LIMIT flag idea there could be nice. If reviewers like it maybe I could support it here and #34038 could be simpler and only implement the logging backend improvements without making a unrelated change (it would seem) to the logging macros?

> The `NO_RATE_LIMIT` commit from #34038 has been available for review for three months now, and has had a concept ack for that long. That's interesting. #34038 is mostly a logging backend change, changing the RPC interface and command line options. It seems like the no-ratelimit change part of that PR might not be related to the other changes there? (I don't see the connection at least.) To compare the two approaches, #34038 syntax looks like: ```c++ LogInfo(util::log::NO_RATE_LIMIT, "message"); ``` while syntax here is: ```c++ LogPrint((.level = util::log::Level::Info, .ratelimit = false), "message"), ``` There is not much difference between the approaches externally, and they are compatible (this PR could easily support a `NO_RATE_LIMIT` flag if reviewers would prefer that.) This bigger goal of this PR is to replace current implementation of the logging macros which is messy and inconsistent with a clean implementation. All preprocessor functionality is implemented in a single macro called [`LogPrint`](https://github.com/ryanofsky/bitcoin/blob/e983d97a9141180f9d06f510f17b87a3333b2954/src/util/log.h#L162-L171), getting rid of the branching code paths at different priority levels [[1]](https://github.com/bitcoin/bitcoin/blob/195306c359ff99f9172d1f1690b124269d3a4a0d/src/util/log.h#L95-L97) [[2]](https://github.com/bitcoin/bitcoin/blob/195306c359ff99f9172d1f1690b124269d3a4a0d/src/util/log.h#L115-L116) that have unexplained differences. The differences are all kept, they are just explicit rather than implicit, with better documentation and tests and stricter enforcement of restrictions. The `.ratelimit = true` option is exposed as a side-effect of this refactoring, and the refactoring also allows other options to be implemented cleanly in the future. Examples could be `.log_once=true` `.log_every=24h` `.log_every=60m` `.stack_trace=true` `.dedup = true` etc. I was also thinking of an `.impossible = true` option that could be used to document preconditions & provide better bug reports and output from fuzz & unit test failures. The point is that I think we should make it easy to add logging options and try to implement most options with `constexpr` code instead of macros. And get rid of complication of different macros at different log levels having different implementations. Anyway I'd be happy to review #34038 and I think the runtime changes there look nice. The NO_RATE_LIMIT flag idea there could be nice. If reviewers like it maybe I could support it here and #34038 could be simpler and only implement the logging backend improvements without making a unrelated change (it would seem) to the logging macros?
maflcko (Migrated from github.com) reviewed 2026-03-10 14:30:13 +00:00
maflcko (Migrated from github.com) commented 2026-03-10 14:30:13 +00:00

not sure about using the internal/hidden macro here, after the changes. If someone wants to log at info level, it seems clearer to just call LogInfo, instead of using some internal macro with a bunch of options.

Also, supporting .ratelimit = false for other levels doesn't really make sense, and certainly isn't needed by current code at all, so supporting it seems odd.

edit: I see it is needed for one warning log as well. So I guess my main concern is that this will open the gates for the "wild west" that we were trying to fix: For example, this new macro may encourage to re-introduce helpers of the form: [](auto level, auto msg) { LogPrint((.level=level),"%s", msg); }, which will then work incorrectly with regard to rate-limiting (silently dropping warnings/errors, when info logs overflow)

not sure about using the internal/hidden macro here, after the changes. If someone wants to log at info level, it seems clearer to just call `LogInfo`, instead of using some internal macro with a bunch of options. Also, supporting `.ratelimit = false` for other levels doesn't really make sense, and certainly isn't needed by current code at all, so supporting it seems odd. edit: I see it is needed for one warning log as well. So I guess my main concern is that this will open the gates for the "wild west" that we were trying to fix: For example, this new macro may encourage to re-introduce helpers of the form: `[](auto level, auto msg) { LogPrint((.level=level),"%s", msg); }`, which will then work incorrectly with regard to rate-limiting (silently dropping warnings/errors, when info logs overflow)
ryanofsky (Migrated from github.com) reviewed 2026-03-10 14:46:16 +00:00
ryanofsky (Migrated from github.com) commented 2026-03-10 14:46:16 +00:00

re: https://github.com/bitcoin/bitcoin/pull/34778#discussion_r2912164572

so supporting it seems odd.

When you say odd do you mean the code is doing something it should not do? Or not doing something it should do? For example, It would be easy to add a static assert to prevent ratelimiting being requested at certain levels if you feel that is important. Though I'm not sure why ratelimiting should not be available at different levels (I haven't looked into the rate limiting implementation).

Overall, the idea here is to accept options and pass them along the backend to implement. I imagine the bitcoinkernel backend probably won't support rate limiting at all for example (or at least it doesn't in #34374)

re: https://github.com/bitcoin/bitcoin/pull/34778#discussion_r2912164572 > so supporting it seems odd. When you say odd do you mean the code is doing something it should not do? Or not doing something it should do? For example, It would be easy to add a static assert to prevent ratelimiting being requested at certain levels if you feel that is important. Though I'm not sure why ratelimiting should not be available at different levels (I haven't looked into the rate limiting implementation). Overall, the idea here is to accept options and pass them along the backend to implement. I imagine the bitcoinkernel backend probably won't support rate limiting at all for example (or at least it doesn't in #34374)
maflcko (Migrated from github.com) reviewed 2026-03-10 14:55:03 +00:00
maflcko (Migrated from github.com) commented 2026-03-10 14:55:03 +00:00

(I haven't looked into the rate limiting implementation).

I've edited my comment to explain one case that will make code more brittle.

I mostly think this should remain internal/hidden, maybe LogPrintInternalDetail(...). Otherwise, devs may think this is fine to use without thinking much about the implicit edges.

> (I haven't looked into the rate limiting implementation). I've edited my comment to explain one case that will make code more brittle. I mostly think this should remain internal/hidden, maybe `LogPrintInternalDetail(...)`. Otherwise, devs may think this is fine to use without thinking much about the implicit edges.
ryanofsky (Migrated from github.com) reviewed 2026-03-10 15:12:19 +00:00
ryanofsky (Migrated from github.com) commented 2026-03-10 15:12:19 +00:00

re: https://github.com/bitcoin/bitcoin/pull/34778#discussion_r2912164572

For example, this new macro may encourage to re-introduce helpers of the form: [](auto level, auto msg) { LogPrint((.level=level),"%s", msg); }, which will then work incorrectly with regard to rate-limiting (silently dropping warnings/errors, when info logs overflow)

Hmm, I'm confused why someone would do what you are suggesting, but if they did it should be safe and work correctly. The ratelimit default value should be the same as before.

The point of adding LogPrint is to be a safe, single implementation of logging code that other macros can just forward to without their own bespoke logic and special cases.

Please let me know if I am missing something here. Like I said I haven't actually looked at the ratelimiting implementation. This PR is just forwarding the option. If any unsafe option values are currently accepted, we should definitely prohibit them with a static assert, but I'm not seeing where that could be happening.

re: https://github.com/bitcoin/bitcoin/pull/34778#discussion_r2912164572 > For example, this new macro may encourage to re-introduce helpers of the form: `[](auto level, auto msg) { LogPrint((.level=level),"%s", msg); }`, which will then work incorrectly with regard to rate-limiting (silently dropping warnings/errors, when info logs overflow) Hmm, I'm confused why someone would do what you are suggesting, but if they did it should be safe and work correctly. The ratelimit [default value](https://github.com/ryanofsky/bitcoin/blob/e983d97a9141180f9d06f510f17b87a3333b2954/src/util/log.h#L55-L59) should be the same as before. The point of adding `LogPrint` is to be a safe, single implementation of logging code that other macros can just forward to without their own bespoke logic and special cases. Please let me know if I am missing something here. Like I said I haven't actually looked at the ratelimiting implementation. This PR is just forwarding the option. If any unsafe option values are currently accepted, we should definitely prohibit them with a static assert, but I'm not seeing where that could be happening.
maflcko (Migrated from github.com) reviewed 2026-03-10 15:29:46 +00:00
maflcko (Migrated from github.com) commented 2026-03-10 15:29:46 +00:00

Hmm, I'm confused why someone would do what you are suggesting,

Well, you can go back in the git history to find this exact pattern. See 2210feb446.

but if they did it should be safe and work correctly.

No, see 2210feb446, which fixed this. (You can refer to the commit messages for more details)

> Hmm, I'm confused why someone would do what you are suggesting, Well, you can go back in the git history to find this exact pattern. See 2210feb4466eff1455468f0a25045fce4b89c55d. > but if they did it should be safe and work correctly. No, see 2210feb4466eff1455468f0a25045fce4b89c55d, which fixed this. (You can refer to the commit messages for more details)
ryanofsky (Migrated from github.com) reviewed 2026-03-10 15:48:52 +00:00
ryanofsky (Migrated from github.com) commented 2026-03-10 15:48:52 +00:00

re: https://github.com/bitcoin/bitcoin/pull/34778#discussion_r2912600904

Hmm, I'm confused why someone would do what you are suggesting,

Well, you can go back in the git history to find this exact pattern. See 2210feb.

Thanks for clarifying. For others who may be following, the pattern is shown specifically in f273167661 and 94c51ae540 and it is not allowed by this PR. This PR enforces the same restrictions as current code.

E.g. if you try to revert f273167661 and call LogPrint((.level = ConvertIPCLogLevel(message.level)) , "%s\n", message.message); you will see an error because the level is not a compile time constant.

(EDIT: I'd add that I don't see anything inherently wrong with the pattern used in those commits, and no reason it couldn't work well. The previous LogPrintLevel implemention just had a bug and was good candidate for removal for other reasons.)

re: https://github.com/bitcoin/bitcoin/pull/34778#discussion_r2912600904 > > Hmm, I'm confused why someone would do what you are suggesting, > > Well, you can go back in the git history to find this exact pattern. See [2210feb](https://github.com/bitcoin/bitcoin/commit/2210feb4466eff1455468f0a25045fce4b89c55d). Thanks for clarifying. For others who may be following, the pattern is shown specifically in f2731676619d14fa4527aa9086fb73078d20f26f and 94c51ae540723f5f648477789c11f6395730ae05 and it is not allowed by this PR. This PR enforces the same restrictions as current code. E.g. if you try to revert f2731676619d14fa4527aa9086fb73078d20f26f and call `LogPrint((.level = ConvertIPCLogLevel(message.level)) , "%s\n", message.message);` you will see an error because the level is not a compile time constant. (EDIT: I'd add that I don't see anything inherently wrong with the pattern used in those commits, and no reason it couldn't work well. The previous `LogPrintLevel` implemention just had a bug and was good candidate for removal for other reasons.)
maflcko (Migrated from github.com) reviewed 2026-03-10 16:49:39 +00:00
maflcko (Migrated from github.com) commented 2026-03-10 16:49:39 +00:00

and it is not allowed by this PR. This PR enforces the same restrictions as current code.

I don't think this is true. I am sure one can write a similar pattern deriving the level constexpr/consteval.

(EDIT: I'd add that I don't see anything inherently wrong with the pattern used in those commits, and no reason it couldn't work well.

logging takes the source location, which is then used for other stuff (such as rate limiting or -logsourcelocations). Using the same source location for different logs is brittle in itself. Though, using it for different log levels is doubly brittle. I don't see the need to regress here

> and it is not allowed by this PR. This PR enforces the same restrictions as current code. I don't think this is true. I am sure one can write a similar pattern deriving the level constexpr/consteval. > (EDIT: I'd add that I don't see anything inherently wrong with the pattern used in those commits, and no reason it couldn't work well. logging takes the source location, which is then used for other stuff (such as rate limiting or -logsourcelocations). Using the same source location for different logs is brittle in itself. Though, using it for different log levels is doubly brittle. I don't see the need to regress here
ryanofsky (Migrated from github.com) reviewed 2026-03-10 17:41:28 +00:00
ryanofsky (Migrated from github.com) commented 2026-03-10 17:41:28 +00:00

re: https://github.com/bitcoin/bitcoin/pull/34778#discussion_r2913106415

and it is not allowed by this PR. This PR enforces the same restrictions as current code.

I don't think this is true. I am sure one can write a similar pattern deriving the level constexpr/consteval.

Can we slow down and try to work through an example here? I would like to understand the problem you are concerned about and either confirm that it is not an issue here, or make some fix to solve it.

As far as I can tell, you did a little pattern matching like "this new function looks similar to that previously removed function" and assuming "therefore it has the same bugs and limitations of the previous function." This is a reasonable assumption, but it happens not to be true in this case!

(EDIT: I'd add that I don't see anything inherently wrong with the pattern used in those commits, and no reason it couldn't work well.

logging takes the source location, which is then used for other stuff (such as rate limiting or -logsourcelocations). Using the same source location for different logs is brittle in itself.

Sorry yes I should have clarified I only think the pattern is good for external code that does not include source locations. Internal logs should definitely have source locations! I don't think you will find any regressions on that front either implemented in this PR or enabled by it.

To the contrary, followup PR #30343 adds source locations to wallet code which is currently missing them.

re: https://github.com/bitcoin/bitcoin/pull/34778#discussion_r2913106415 > > and it is not allowed by this PR. This PR enforces the same restrictions as current code. > > I don't think this is true. I am sure one can write a similar pattern deriving the level constexpr/consteval. Can we slow down and try to work through an example here? I would like to understand the problem you are concerned about and either confirm that it is not an issue here, or make some fix to solve it. As far as I can tell, you did a little pattern matching like "this new function looks similar to that previously removed function" and assuming "therefore it has the same bugs and limitations of the previous function." This is a reasonable assumption, but it happens not to be true in this case! > > (EDIT: I'd add that I don't see anything inherently wrong with the pattern used in those commits, and no reason it couldn't work well. > > logging takes the source location, which is then used for other stuff (such as rate limiting or -logsourcelocations). Using the same source location for different logs is brittle in itself. Sorry yes I should have clarified I only think the pattern is good for external code that does not include source locations. Internal logs should definitely have source locations! I don't think you will find any regressions on that front either implemented in this PR or enabled by it. To the contrary, followup PR #30343 **adds** source locations to wallet code which is currently missing them.
maflcko (Migrated from github.com) reviewed 2026-03-10 18:18:02 +00:00
maflcko (Migrated from github.com) commented 2026-03-10 18:18:02 +00:00

example

Of course, this synthetic example is not found in the codebase as-is, but I don't see why it wouldn't compile with the changes in this pull

struct Module {
std::string m_name;
template<Level level>
void ModuleLog(std::string m) { LogPrint((.level=level), "[%s] %s", m_name, m); }
...
};


> example Of course, this synthetic example is not found in the codebase as-is, but I don't see why it wouldn't compile with the changes in this pull ```cpp struct Module { std::string m_name; template<Level level> void ModuleLog(std::string m) { LogPrint((.level=level), "[%s] %s", m_name, m); } ... };
ryanofsky (Migrated from github.com) reviewed 2026-03-10 19:03:02 +00:00
ryanofsky (Migrated from github.com) commented 2026-03-10 19:03:02 +00:00

re: https://github.com/bitcoin/bitcoin/pull/34778#discussion_r2913611122

Thanks for the concrete example. I am assuming based on your earlier comments that problem with this example is that it hides source locations, and I agree hiding source locations would be bad. I guess I just disagree with the realism of the example. You seem to be saying is that this PR could make developers more tempted to write bad per-module logging interfaces and add calls like:

my_module.ModuleLog<Level::Info>("message")

to the code, hiding source locations that would otherwise be available. Even though developers could implement literally the same bad interface today by calling the LogInfo/LogWarning/LogError macros with an if or switch statement? And even though we already have an existing WalletLogPrintf function today which has no problem hiding source locations with the current logging macros?

I just don't see this PR by itself having any real impact on support for source locations. But if that is the concern and I did not misunderstand something, thanks for clarifying.

re: https://github.com/bitcoin/bitcoin/pull/34778#discussion_r2913611122 Thanks for the concrete example. I am assuming based on your earlier comments that problem with this example is that it hides source locations, and I agree hiding source locations would be bad. I guess I just disagree with the realism of the example. You seem to be saying is that this PR could make developers more tempted to write bad per-module logging interfaces and add calls like: ```c++ my_module.ModuleLog<Level::Info>("message") ``` to the code, hiding source locations that would otherwise be available. Even though developers could implement literally the same bad interface today by calling the LogInfo/LogWarning/LogError macros with an `if` or `switch` statement? And even though we already have an existing `WalletLogPrintf` function today which has no problem hiding source locations with the current logging macros? I just don't see this PR by itself having any real impact on support for source locations. But if that is the concern and I did not misunderstand something, thanks for clarifying.
maflcko (Migrated from github.com) reviewed 2026-03-16 14:53:23 +00:00
maflcko (Migrated from github.com) commented 2026-03-16 14:53:23 +00:00

I guess I just disagree with the realism of the example.

You give a real place yourself (WalletLogPrintf), where a dev (or an LLM) could apply the example today (or use it as an excuse to introduce the pattern in another module) in any of the 300 open pulls. So I don't see why the example should not be real.

an if or switch statement?

Fair enough, but at least this won't apply rate-limiting across levels, when it shouldn't.

And to re-iterate an earlier point: Just as the logging was fixed to be consistent, so that one can call git grep LogInfo to find all info logs, this will introduce a new alias to grep for. I am sure over time devs will then use (inside the same file), stuff like:

LogPrint((.level = util::log::Level::Info), "a");
LogInfo("b):

I don't see the point in allowing all of this, if there is no reason for it. Why not just indicate in the name of the macro or function that this is only meant for internal use, like it was before this pull request?

> I guess I just disagree with the realism of the example. You give a real place yourself (`WalletLogPrintf`), where a dev (or an LLM) could apply the example today (or use it as an excuse to introduce the pattern in another module) in any of the 300 open pulls. So I don't see why the example should not be real. > an if or switch statement? Fair enough, but at least this won't apply rate-limiting across levels, when it shouldn't. And to re-iterate an earlier point: Just as the logging was fixed to be consistent, so that one can call `git grep LogInfo` to find all info logs, this will introduce a new alias to grep for. I am sure over time devs will then use (inside the same file), stuff like: ``` LogPrint((.level = util::log::Level::Info), "a"); LogInfo("b): ``` I don't see the point in allowing all of this, if there is no reason for it. Why not just indicate in the name of the macro or function that this is only meant for internal use, like it was before this pull request?
ryanofsky commented 2026-05-06 09:17:43 +00:00 (Migrated from github.com)

Rebased 31612a7f5f0d197bdc4a9747ad9e7509bf693d7d -> 0c090593e8bdbdd3f65ea12aa1177ebc3d22d86a (pr/relog.3 -> pr/relog.4, compare) due to conflict with #34865

Rebased 0c090593e8bdbdd3f65ea12aa1177ebc3d22d86a -> f0929e1b78 (pr/relog.4 -> pr/relog.5, compare) due to conflict with #34806

Rebased f0929e1b78 -> 4323986a35 (pr/relog.5 -> pr/relog.6, compare) due to conflicts with #34729 and #36397, also updating the kernel log hooks for the macro rewrite now that #34374 is merged (previously done in #30342)

<!-- begin push-4 --> Rebased 31612a7f5f0d197bdc4a9747ad9e7509bf693d7d -> 0c090593e8bdbdd3f65ea12aa1177ebc3d22d86a ([`pr/relog.3`](https://github.com/ryanofsky/bitcoin/commits/pr/relog.3) -> [`pr/relog.4`](https://github.com/ryanofsky/bitcoin/commits/pr/relog.4), [compare](https://github.com/ryanofsky/bitcoin/compare/pr/relog.3-rebase..pr/relog.4))<!-- end --> due to conflict with #34865 <!-- begin push-5 --> Rebased 0c090593e8bdbdd3f65ea12aa1177ebc3d22d86a -> f0929e1b781af5012d835f78a8b54054fb5cb21e ([`pr/relog.4`](https://github.com/ryanofsky/bitcoin/commits/pr/relog.4) -> [`pr/relog.5`](https://github.com/ryanofsky/bitcoin/commits/pr/relog.5), [compare](https://github.com/ryanofsky/bitcoin/compare/pr/relog.4-rebase..pr/relog.5))<!-- end --> due to conflict with #34806 <!-- begin push-6 --> Rebased f0929e1b781af5012d835f78a8b54054fb5cb21e -> 4323986a35beb0d6e148141832802f02e08a7b7f ([`pr/relog.5`](https://github.com/ryanofsky/bitcoin/commits/pr/relog.5) -> [`pr/relog.6`](https://github.com/ryanofsky/bitcoin/commits/pr/relog.6), [compare](https://github.com/ryanofsky/bitcoin/compare/pr/relog.5-rebase..pr/relog.6))<!-- end --> due to conflicts with #34729 and #36397, also updating the kernel log hooks for the macro rewrite now that #34374 is merged (previously done in #30342)
sedited commented 2026-09-17 07:41:44 +00:00 (Migrated from github.com)

This has not received review for half a year, has a N-A-C-K and maflcko's last comment wasn't replied to. Maybe this should be closed at this point?

This has not received review for half a year, has a N-A-C-K and maflcko's last comment wasn't replied to. Maybe this should be closed at this point?
First-time contributor

🟠 Minor · -loglevelalways omits the category on Info, Warning, and Error lines

With -loglevelalways=1, Info lines have no prefix and Warning/Error lines omit all:, contrary to the option’s help text.

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

🔵 Suggestion · Disabled Debug and Trace calls acquire the logger mutex

Every Debug or Trace call for a disabled category now locks m_cs, adding hot-path contention that the base avoided; no availability impact is established.

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

🔵 Suggestion · Category masks and integers pass the logging category parameter

A masked category or integer now compiles, but can reach an assertion when logging is enabled; no current caller does this.

Location: src/util/log.h:173 (head).

🔵 Suggestion · Argument-evaluation test never enables the category

The Debug and Trace assertions pass at both base and head, so they do not test whether the new logger-disabled gate skips argument evaluation.

Location: src/test/logging_tests.cpp:592 (head).

🔵 Suggestion · Developer notes do not explain how to pass ratelimit = false

The documentation names a rate-limit option but gives no usable example, unlike the text it replaced.

Location: doc/developer-notes.md:818 (head).


Reviewed: 4323986a35be · Full review report · Previous reports

permalink

<!-- forgejo-review-bot:bitcoin/bitcoin --> <!-- ralph-engine:current:7ba49ca38b129bc35f71e566efbd136b17ae87b582376d9eebd04f18959e8274:9b5e0237a3fe9e1d05225291551198f07780ddb92e41e55ade9e98c2687fc127 --> <!-- ralph-base:3c51322d9f95225d683a8d438a1dcde970ed5c11 --> <!-- ralph-head:4323986a35beb0d6e148141832802f02e08a7b7f --> <!-- ralph-case:d35a72b0029c7ff0e98fff88dc048dfa3312c7ab0c8b97fd2da1d6a57d26fee7 --> **🟠 Minor · `-loglevelalways` omits the category on Info, Warning, and Error lines** With `-loglevelalways=1`, Info lines have no prefix and Warning/Error lines omit `all:`, contrary to the option’s help text. Location: [`src/logging.cpp:359`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/4323986a35beb0d6e148141832802f02e08a7b7f/src/logging.cpp#L359>) (head). **🔵 Suggestion · Disabled Debug and Trace calls acquire the logger mutex** Every Debug or Trace call for a disabled category now locks `m_cs`, adding hot-path contention that the base avoided; no availability impact is established. Location: [`src/logging.cpp:625`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/4323986a35beb0d6e148141832802f02e08a7b7f/src/logging.cpp#L625>) (head). **🔵 Suggestion · Category masks and integers pass the logging category parameter** A masked category or integer now compiles, but can reach an assertion when logging is enabled; no current caller does this. Location: [`src/util/log.h:173`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/4323986a35beb0d6e148141832802f02e08a7b7f/src/util/log.h#L173>) (head). **🔵 Suggestion · Argument-evaluation test never enables the category** The Debug and Trace assertions pass at both base and head, so they do not test whether the new logger-disabled gate skips argument evaluation. Location: [`src/test/logging_tests.cpp:592`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/4323986a35beb0d6e148141832802f02e08a7b7f/src/test/logging_tests.cpp#L592>) (head). **🔵 Suggestion · Developer notes do not explain how to pass `ratelimit = false`** The documentation names a rate-limit option but gives no usable example, unlike the text it replaced. Location: [`doc/developer-notes.md:818`](<https://git.fish.foo/bitcoin/bitcoin/src/commit/4323986a35beb0d6e148141832802f02e08a7b7f/doc/developer-notes.md#L818>) (head). <!-- Review tier: sensitive --> --- Reviewed: [`4323986a35be`](<https://git.fish.foo/bitcoin/bitcoin/commit/4323986a35beb0d6e148141832802f02e08a7b7f>) · [Full review report](<https://review.fish.foo/traces/34778-4323986a35beb0d6e148141832802f02e08a7b7f-1792-2.html>) · [Previous reports](<https://review.fish.foo/traces/34778/>) [permalink](<https://review.fish.foo/traces/34778-4323986a35beb0d6e148141832802f02e08a7b7f-1792-2.html>)
cyb3ralbert commented 2026-10-14 05:03:28 +00:00 (Migrated from github.com)

Tested f0929e1 (also merged with master 8406af1c).

I ran into two issues:

  1. -loglevelalways output changes. With bitcoind -regtest -loglevelalways=1 -debug=net, the merge-base prints [all:info] on info lines, while this PR omits the prefix. In a unit test, warning and error lines are prefixed with [warning] and [error] instead of [all:warning] and [all:error].

    Is this change intentional? The help text still says "Always prepend a category and level", and the 98f3bd6 commit message says there is no visible effect except in tests. If the change is intentional, could the help text be updated and the behavior mentioned in the release notes?

  2. LogDebug accepts invalid flag types. LogDebug now compiles when passed a plain integer or an OR-ed mask, whereas both failed to compile on the merge-base. For example, LogDebug(BCLog::NET | BCLog::HTTP, "x") aborts in a unit test when net is enabled, at logging.cpp:261, with Assertion 'it != LOG_CATEGORIES_BY_FLAG.end()' failed. LogDebug(1, "x") instead logs as [net].

    There are no such call sites in the current tree, so this mainly affects future code. Could GetContext take BCLog::LogFlags again, so these invalid arguments are rejected at compile time?

Tested `f0929e1` (also merged with master `8406af1c`). I ran into two issues: 1. **`-loglevelalways` output changes.** With `bitcoind -regtest -loglevelalways=1 -debug=net`, the merge-base prints `[all:info]` on info lines, while this PR omits the prefix. In a unit test, warning and error lines are prefixed with `[warning]` and `[error]` instead of `[all:warning]` and `[all:error]`. Is this change intentional? The help text still says "Always prepend a category and level", and the `98f3bd6` commit message says there is no visible effect except in tests. If the change is intentional, could the help text be updated and the behavior mentioned in the release notes? 2. **`LogDebug` accepts invalid flag types.** `LogDebug` now compiles when passed a plain integer or an OR-ed mask, whereas both failed to compile on the merge-base. For example, `LogDebug(BCLog::NET | BCLog::HTTP, "x")` aborts in a unit test when `net` is enabled, at `logging.cpp:261`, with `Assertion 'it != LOG_CATEGORIES_BY_FLAG.end()' failed`. `LogDebug(1, "x")` instead logs as `[net]`. There are no such call sites in the current tree, so this mainly affects future code. Could `GetContext` take `BCLog::LogFlags` again, so these invalid arguments are rejected at compile time?
DrahtBot commented 2026-10-20 08:48:08 +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).
Sign in to join this conversation.
No reviewers
No milestone
No project
No assignees
2 participants
Notifications
Due date
The due date is invalid or out of range. Please use the format "yyyy-mm-dd".

No due date set.

Dependencies

No dependencies set

Reference
bitcoin/bitcoin!34778
No description provided.