New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
logging: Add severity level to logs #24464
Conversation
4e9880d
to
b6875b4
Compare
|
The following sections might be updated with supplementary metadata relevant to reviewers and maintainers. 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. |
Concept ACK, but
- Category shouldn't be optional.
- Severity should be a parameter, not part of the function name. Often, severity can vary based on the specifics.
|
Concept ACK. I agree with @luke-jr that it'd be better for severity to be a parameter rather than part of the macro name |
6820e9b
to
4444a6b
Compare
|
Concept ACK. I've always liked this idea because it allows moving all "global" log messages to a category, eventually, just with a higher priority than debug. |
3b0e508
to
6b07619
Compare
7bff5b3
to
3a35513
Compare
src/logging.cpp
Outdated
|
|
||
| } | ||
|
|
||
| std::string LogCategoryToStr(const BCLog::LogFlags& category) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It would be good to consistently pass both LogFlags and Level in this pull either by (non-const) value or by reference to const, but not both. See https://isocpp.github.io/CppCoreGuidelines/CppCoreGuidelines#Rf-in.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder at what point it makes sense to have an array of category strings and use a size_t cast of LogFlags as an index (i.e. like static constexpr std::array NETWORKS{"ipv4", "ipv6", "onion", "i2p", "cjdns"}; in bitcoin-cli.cpp)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for the reference! Updated this pull to pass both LogFlags and Level by value.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I wonder at what point it makes sense to have an array of category strings and use a size_t cast of LogFlags as an index
@jonatack We chose switch-case as it provides us with a compile-time warning. Additionally, IMO for large enums, switch-cases are easier to review as compared to checking the index of the elements against the enum value.
However, I do agree that this function could easily become bloated but I am not sure what is the best solution for it.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yes, I was musing more about performance as this code can be called frequently and an array lookup is constant time; maybe you can use the existing CLogCategoryDesc.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Do you have any suggestions on how we should map the enums to the index on the array? Currently, the majority enums values of LogFlags are bit flags (ie 1<<i) and the enum values of LogFlags::NONE and LogFlags::ALL are 0 and all bits sets respectively.
a62d157
to
2f83557
Compare
|
@jonatack @MarcoFalke thanks for the detailed review! a62d157 to 2f83557: Updated the PR to address the review comments |
| std::string LogCategoryToStr(BCLog::LogFlags category) | ||
| { | ||
| switch (category) { | ||
| case BCLog::LogFlags::NONE: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
It seems there's an overlap here with CLogCategoryDesc LogCategories[]. I think it would be best to use that? If not, at the least it would be good to add comments in both places (or even a test) to keep the lists in sync.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done, added a comment to state that the LogCategoryToStr should sync with LogCategories and added tests to ensure that both are in sync.
I think it would be best to use that
I am not really sure how to do that as we cannot simply map the enum values of LogFlags to indexes on LogCategories. This is due to most of the enum values of LogCategories being bit flags (ie 1 << i) but LogCategories::NONE and LogCategories::ALL are not bit flags.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is due to most of the enum values of LogCategories being bit flags (ie 1 << i) but LogCategories::NONE and LogCategories::ALL are not bit flags.
One way to do it would be to introduce a third "source data" structure, and build both LogCategories and the one used for LogCategoryToStr from that.
That said, I think this is ok for now.
|
Code review and lightly tested ACK e11cdc9 |

Formed in 2009, the Archive Team (not to be confused with the archive.org Archive-It Team) is a rogue archivist collective dedicated to saving copies of rapidly dying or deleted websites for the sake of history and digital heritage. The group is 100% composed of volunteers and interested parties, and has expanded into a large amount of related projects for saving online and digital history.

Overview: This PR introduces a new macro,
LogPrintLevel, that allows developers to add logs with the severity level. Additionally, it will also print the log category if it is specified.Sample log:
Motivation: This feature was suggested in #20576 and I believe that it will bring the following benefits:
debug.logDetails:
... [category:level].... ie:category == NONElevel == NONEcategory == NONEandlevel == NONE, do not print any fields (current behaviour)LogPrintf: no changes in log as it callsLogPrintf_withcategory = NONEandlevel = NONELogPrint: prints additional[category]field as it callsLogPrintf_withcategory = categoryandlevel = NONEnet.cpp: As a proof of concept, updated logs with obvious severity (ie prefixed withWarning/Error:..) to use the new logging with severity.Testing:
Compiling and running
bitcoindwith this PR should instantly display logs with the category name (ienet/tor/...)Grepping for
net:debugindebug.logshould display the updated logs with severity level:Code