Repository navigation
Conversation
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. External sites
ReviewsSee the guideline and AI policy for information on the review process.
If your review is incorrectly listed, please copy-paste ConflictsReviewers, this pull request conflicts with the following ones:
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. |
e1a58bc to
446f523
Compare
|
🚧 At least one of the CI tasks failed. HintsTry to run the tests locally, according to the documentation. However, a CI failure may still
Leave a comment here, if you need help tracking down a confusing failure. |
446f523 to
c20ff4c
Compare
c20ff4c to
0d075e3
Compare
0d075e3 to
68681ee
Compare
musaHaruna
left a comment
There was a problem hiding this 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.
|
Force-pushed to address review comments from @musaHaruna:
|
ryanofsky
left a comment
There was a problem hiding this comment.
Code review ACK ad66992. 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.
Introduce the btck_LogEntry struct the logging callback will use in a future commit (instead of the current string argument). The struct is not yet used. The level and category definitions move up so the callback typedef can reference btck_LogEntry; review with --color-moved. No behaviour change. Part of a series of commits to make kernel logging struct-based instead of string-based.
Convenience functions for consumers that want to format levels and categories. No behaviour change. Part of a series of commits to make kernel logging struct-based instead of string-based.
Both functions lose their purpose once the kernel logger is no longer coupled to the node logger: formatting becomes the consumer's job, and nothing is buffered before a connection exists, so there is nothing to disable. Remove them early to simplify the next commits. Behaviour change until the backend switch later in this series: - strings are delivered in BCLog::Logger's default format - the 1MB pre-connection buffer cannot be turned off. Part of a series of commits to make kernel logging struct-based instead of string-based.
Add the kernel's own logging backend, unused until the next commit switches the C API over to it. Callbacks run while the logger's mutex is held: invocations are serialized, and unregistering waits for an in-flight callback. Callbacks are typed with a local LogCallback alias carrying the struct-based signature, because btck_LogCallback still delivers a string at this point. The next commit changes the typedef and replaces the alias. No behaviour change. Part of a series of commits to make kernel logging struct-based instead of string-based.
Update btck_LogCallback to use struct-based logging and drop logging.cpp from the kernel build. Since consumers can now easily filter by category, simplify the logging configuration to just btck_logging_set_min_level. Behaviour changes for consumers: - Filtering is now purely levels-based, and consumers can choose any level they like. Debug and Trace logging applies to all categories, further filtering should be done client-side. Note: evaluation remains unchanged: Debug and Trace statements are only evaluated when they are requested, Info and above is always evaluated. - Entries logged before the first connection are dropped, not buffered. - Trailing newlines are stripped, callback exceptions are swallowed, and connection create and destroy no longer take cs_main. - Log messages are no longer escaped, this can be done client-side. - btck_LogCategory_ALL is replaced by btck_LogCategory_NONE, since uncategorized entries do not belong to all categories. KernelLogger moves into the anonymous namespace now that every member is used. It stayed outside in the previous commit to avoid unused member warnings. Node logging is unchanged. Final commit of the series to make kernel logging struct-based instead of string-based.
stickies-v
left a comment
There was a problem hiding this comment.
Force-pushed to address review comments from @ryanofsky and @w0xlt:
- replace
btck_LogCategory_ALLandbtck_LogCategory_UNKNOWNwith a singlebtck_LogCategory_NONE - document that
btck_LogEntry::messageis not escaped - docstring fixes for
btck_LogCallbackandbtck_logging_connection_create - remove the now-unused
BCLog::Logger::NumConnections()
musaHaruna
left a comment
There was a problem hiding this comment.
Code Review ACK eb08654
Thanks for incorporating my suggestions @stickies-v.
|
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
left a comment
There was a problem hiding this comment.
Nice, ACK eb08654
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.
| typedef struct { | ||
| //! Log message text. Not escaped, so it may contain newlines and other | ||
| //! control characters. | ||
| const char* message; | ||
| //! Length of message. | ||
| size_t message_len; | ||
| //! Name of the thread that produced the log message. | ||
| const char* thread_name; | ||
| //! Length of thread_name. | ||
| size_t thread_name_len; | ||
| //! Wall-clock time when the entry was logged, in nanoseconds since the | ||
| //! Unix epoch. | ||
| int64_t timestamp_ns; | ||
| //! 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). | ||
| int64_t mocktime_s; | ||
| //! Name of the source file that produced the log statement. | ||
| const char* file_name; | ||
| //! Length of file_name. | ||
| size_t file_name_len; | ||
| //! Name of the function that produced the log message. | ||
| const char* function_name; | ||
| //! Length of function_name. | ||
| size_t function_name_len; | ||
| //! Line number of the source code that produced the log statement. | ||
| uint32_t line; | ||
| //! Log severity level. | ||
| btck_LogLevel level; | ||
| //! Log category. | ||
| btck_LogCategory category; | ||
| } btck_LogEntry; |
There was a problem hiding this comment.
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.
There was a problem hiding this comment.
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.
|
post-merge ACK eb08654 |

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
bitcoinkernellibrary (#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:
btck_logging_set_level_categoryandbtck_logging_enable_categorywhen they want to producedebugortracelogs. 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.Approach
Log generation (
util/log.h: macros,util::log::Entry, and theShouldDebugLog/ShouldTraceLog/Loghooks an application must provide) was already separated from log handling (logging.h) in #34465 and subsequent PRs. This PR givesbitcoinkernelits own implementation of those hooks, so it no longer depends onlogging.cpp, and upgrades the C API to deliver struct-based entries. Node logging is not changed.WARNING,ERROR,TXPACKAGES,LOCK) and thebtck_LogEntrystruct, addName()helpers to the C++ wrapper, and removebtck_logging_set_optionsandbtck_logging_disable(with struct-based entries there is no kernel-side format to configure, and without buffering there is nothing to disable).KernelLogger: a kernel-owned backend that holds the registered callbacks and the minimum level, and converts eachutil::log::Entryinto abtck_LogEntry. Introduced in a separate commit to keep the scope of the behaviour-changing commit smaller.btck_LogCallbackreceives abtck_LogEntryinstead of a string, theutil::loghooks are implemented onKernelLogger,logging.cppis dropped from the kernel build, and the logging configuration interface is reduced tobtck_logging_set_min_level().Behaviour changes for consumers (also described in each commit message):
Infoand above.Debug/Traceapply to all categories, consumers can filter onbtck_LogEntry::category(but string formatting is done for the entire level, instead of per enabled category).Appendix
bitcoinkernelC logging interface