Skip to content

[logging] introduce Log{Level}OnError macros and update core - #12533

Merged
jwhui merged 1 commit into
openthread:mainfrom
abtink:log/log-on-error-enhc
Feb 24, 2026
Merged

[logging] introduce Log{Level}OnError macros and update core#12533
jwhui merged 1 commit into
openthread:mainfrom
abtink:log/log-on-error-enhc

Conversation

@abtink

@abtink abtink commented Feb 23, 2026

Copy link
Copy Markdown
Member

This commit introduces a new set of logging macros, LogCritOnError, LogWarnOnError, LogNoteOnError, LogInfoOnError, and LogDebgOnError, to provide a consistent and streamlined way to log errors across the codebase.

The new macros automatically prepend "Failed to " and append the error string (using ErrorToString()) to the log message, reducing boilerplate code and ensuring a uniform log format. These macros only emit a log if the provided Error is not kErrorNone.

This change improves code readability and maintainability by consolidating error logging logic into the logging framework.

@gemini-code-assist

Copy link
Copy Markdown
Contributor

Summary of Changes

Hello @abtink, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

This pull request significantly enhances the error logging mechanism by introducing a new family of Log{Level}OnError macros. The primary goal is to centralize and standardize how errors are reported throughout the codebase, making logs more consistent and easier to parse. By abstracting the common pattern of checking for an error and then formatting a 'Failed to ...' message, the changes reduce repetitive code and improve the clarity of error messages, ultimately leading to better debugging and system understanding.

Highlights

  • New Error Logging Macros: Introduced a new set of logging macros, LogCritOnError, LogWarnOnError, LogNoteOnError, LogInfoOnError, and LogDebgOnError, to standardize error reporting.
  • Automated Error Formatting: These new macros automatically prepend 'Failed to ' and append the error string (using ErrorToString()) to the log message, reducing boilerplate and ensuring consistent log format.
  • Conditional Logging: The Log{Level}OnError macros only emit a log message if the provided Error is not kErrorNone, preventing unnecessary log entries for successful operations.
  • Codebase Refactoring: Existing error logging calls across various core modules have been updated to utilize these new, streamlined macros, improving readability and maintainability.

🧠 New Feature in Public Preview: You can now enable Memory to help Gemini Code Assist learn from your team's feedback. This makes future code reviews more consistent and personalized to your project's style. Click here to enable Memory in your admin console.

Changelog
  • src/core/border_router/infra_if.cpp
    • Updated error logging to use LogDebgOnError for ICMPv6 message processing, NAT64 synthetic address handling, and DHCPv6 message processing.
  • src/core/border_router/routing_manager.cpp
    • Refactored Router Advertisement (RA) transmission failure logging to use LogWarnOnError.
    • Modified OmrPrefixManager::AddOrUpdateLocalInNetData to use SuccessOrExit and LogWarnOnError for adding/updating Thread Network Data.
    • Updated OmrPrefixManager::RemoveLocalFromNetData to use LogWarnOnError for removing data.
    • Applied LogWarnOnError for logging failures during NAT64 prefix discovery.
  • src/core/border_router/rx_ra_tracker.cpp
    • Replaced direct LogCrit call with LogCritOnError for reporting Router Solicitation (RS) send failures.
  • src/core/coap/coap.cpp
    • Converted error logging for processing received CoAP requests to use LogInfoOnError.
  • src/core/common/log.cpp
    • Implemented the Logger::LogOnError template function to handle conditional error logging.
    • Added explicit template instantiations for Logger::LogOnError across all log levels.
    • Introduced a new private Logger::Log method to centralize log message formatting, including prepending 'Failed to ' and appending error strings.
    • Removed the old Logger::LogOnError function.
  • src/core/common/log.hpp
    • Defined new LogCritOnError, LogWarnOnError, LogNoteOnError, LogInfoOnError, and LogDebgOnError macros.
    • Added Doxygen documentation for the newly introduced LogOnError macros.
    • Updated the Logger class declaration to include the new LogOnError template method and the private Log method.
    • Added extern template declarations for the LogOnError instantiations.
  • src/core/mac/data_poll_sender.cpp
    • Updated data poll request error logging to use LogWarnOnError.
    • Refactored data poll send failure logging to use LogInfoOnError and streamlined retry attempt calculation.
  • src/core/mac/mac.cpp
    • Changed frame transmission failure logging on required radio links to use LogDebgOnError.
  • src/core/mac/sub_mac.cpp
    • Refactored RadioSleep() error handling to use LogWarnOnError and SuccessOrExit.
    • Updated Receive() error handling to use SuccessOrExit and LogWarnOnError.
  • src/core/meshcop/border_agent_tracker.cpp
    • Replaced custom Tracker::LogOnError calls with the new LogWarnOnError macro for various operations like adding agents, setting host names, setting host addresses, and setting TXT data.
    • Removed the custom Tracker::LogOnError function implementation.
  • src/core/meshcop/border_agent_tracker.hpp
    • Removed the declaration of the custom Tracker::LogOnError static method.
  • src/core/meshcop/dataset_manager.cpp
    • Updated error logging for setting PAN channel and wake-up channel during dataset application to use LogCritOnError.
  • src/core/net/dnssd_server.cpp
    • Modified upstream query transaction failure logging to use LogWarnOnError.
  • src/core/net/mdns.cpp
    • Changed message parsing failure logging to use LogInfoOnError.
  • src/core/net/srp_advertising_proxy.cpp
    • Updated host registration error logging to use LogWarnOnError.
    • Modified service registration error logging to use LogWarnOnError.
  • src/core/net/srp_client.cpp
    • Converted server connection failure logging to use LogInfoOnError.
    • Updated SRP update send failure logging to use LogInfoOnError.
    • Changed response processing error logging to use LogInfoOnError.
  • src/core/net/srp_server.cpp
    • Modified SRP update processing error logging to use LogInfoOnError.
  • src/core/radio/ble_secure.cpp
    • Updated HandleTlsReceive error logging to use LogCritOnError.
  • src/core/radio/trel_peer_discoverer.cpp
    • Changed DNS-SD service registration failure logging to use LogInfoOnError.
  • src/core/thread/address_resolver.cpp
    • Updated error logging for sending address error messages to use LogInfoOnError.
    • Modified error logging for processing address error messages to use LogWarnOnError.
  • src/core/thread/mesh_forwarder_ftd.cpp
    • Converted error logging for getting forwarded frame priority to use LogInfoOnError.
  • src/core/thread/mle.cpp
    • Refactored Mle::LogError to conditionally use LogInfoOnError or LogWarnOnError based on message action and error type.
  • src/core/thread/mle_ftd.cpp
    • Updated error logging for adding IPv6 addresses to child devices to use LogWarnOnError.
  • src/core/thread/mlr_manager.cpp
    • Changed SendMlrMessage() error logging to use LogInfoOnError.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in pull request comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request introduces new LogOnError macros to streamline error logging, reducing boilerplate and ensuring consistent log formats. The changes involve replacing existing if (error != kErrorNone) blocks with the new macros across various files. This improves code readability and maintainability by centralizing error logging logic. The comments provided identify redundant checks and suggest format string adjustments for the new logging macros, aligning with best practices for clear and concise code.

Comment thread src/core/mac/data_poll_sender.cpp
Comment thread src/core/net/srp_advertising_proxy.cpp Outdated
@github-actions

github-actions Bot commented Feb 23, 2026

Copy link
Copy Markdown

Merging #12533 into main

name branch text data bss total
ot-cli-ftd b3d169d 483168 860 66532 550560
b3fa6a3 482968 860 66532 550360
+/- -200 +0 +0 -200
ot-ncp-ftd b3d169d 448996 764 61768 511528
b3fa6a3 448788 764 61768 511320
+/- -208 +0 +0 -208
ot-cli-mtd b3d169d 374912 764 50964 426640
b3fa6a3 374744 764 50964 426472
+/- -168 +0 +0 -168
ot-ncp-mtd b3d169d 355988 764 46232 402984
b3fa6a3 355828 764 46232 402824
+/- -160 +0 +0 -160
ot-cli-ftd-br b3d169d 599472 868 136588 736928
b3fa6a3 599080 868 136588 736536
+/- -392 +0 +0 -392
ot-rcp b3d169d 63264 568 20804 84636
b3fa6a3 63328 568 20804 84700
+/- +64 +0 +0 +64
Library files
name branch text data bss total
libopenthread-ftd.a b3d169d 250551 95 40311 290957
b3fa6a3 250454 95 40311 290860
+/- -97 +0 +0 -97
libopenthread-cli-ftd.a b3d169d 61710 0 8091 69801
b3fa6a3 61710 0 8091 69801
+/- +0 +0 +0 +0
libopenthread-ncp-ftd.a b3d169d 33581 0 5948 39529
b3fa6a3 33581 0 5948 39529
+/- +0 +0 +0 +0
libopenthread-mtd.a b3d169d 168429 0 24775 193204
b3fa6a3 168375 0 24775 193150
+/- -54 +0 +0 -54
libopenthread-cli-mtd.a b3d169d 41419 0 8059 49478
b3fa6a3 41419 0 8059 49478
+/- +0 +0 +0 +0
libopenthread-ncp-mtd.a b3d169d 25805 0 5948 31753
b3fa6a3 25805 0 5948 31753
+/- +0 +0 +0 +0
libopenthread-ftd-br.a b3d169d 367768 100 110327 478195
b3fa6a3 367473 100 110327 477900
+/- -295 +0 +0 -295
libopenthread-cli-ftd-br.a b3d169d 80757 0 8131 88888
b3fa6a3 80757 0 8131 88888
+/- +0 +0 +0 +0
libopenthread-rcp.a b3d169d 9944 0 5060 15004
b3fa6a3 9944 0 5060 15004
+/- +0 +0 +0 +0
libopenthread-radio.a b3d169d 19537 0 238 19775
b3fa6a3 19707 0 238 19945
+/- +170 +0 +0 +170

This commit introduces a new set of logging macros, `LogCritOnError`,
`LogWarnOnError`, `LogNoteOnError`, `LogInfoOnError`, and
`LogDebgOnError`, to provide a consistent and streamlined way to log
errors across the codebase.

The new macros automatically prepend "Failed to " and append the error
string (using `ErrorToString()`) to the log message, reducing
boilerplate code and ensuring a uniform log format. These macros only
emit a log if the provided `Error` is not `kErrorNone`.

This change improves code readability and maintainability by
consolidating error logging logic into the logging framework.
@abtink
abtink force-pushed the log/log-on-error-enhc branch from b220f3a to be34e2a Compare February 23, 2026 22:18
@codecov

codecov Bot commented Feb 23, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 70.68966% with 17 lines in your changes missing coverage. Please review.
✅ Project coverage is 75.05%. Comparing base (c280027) to head (be34e2a).
⚠️ Report is 14 commits behind head on main.

Files with missing lines Patch % Lines
src/core/meshcop/border_agent_tracker.cpp 0.00% 4 Missing ⚠️
src/core/mac/data_poll_sender.cpp 60.00% 2 Missing ⚠️
src/core/net/srp_advertising_proxy.cpp 0.00% 2 Missing ⚠️
src/core/net/srp_client.cpp 33.33% 2 Missing ⚠️
src/core/border_router/routing_manager.cpp 80.00% 1 Missing ⚠️
src/core/coap/coap.cpp 66.66% 1 Missing ⚠️
src/core/net/dnssd_server.cpp 0.00% 1 Missing ⚠️
src/core/net/mdns.cpp 0.00% 1 Missing ⚠️
src/core/net/srp_server.cpp 0.00% 1 Missing ⚠️
src/core/radio/ble_secure.cpp 0.00% 1 Missing ⚠️
... and 1 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #12533      +/-   ##
==========================================
+ Coverage   73.41%   75.05%   +1.63%     
==========================================
  Files         695      698       +3     
  Lines       95811    95735      -76     
==========================================
+ Hits        70340    71851    +1511     
+ Misses      25471    23884    -1587     
Files with missing lines Coverage Δ
src/core/border_router/infra_if.cpp 67.67% <ø> (ø)
src/core/border_router/rx_ra_tracker.cpp 90.75% <100.00%> (ø)
src/core/common/log.cpp 100.00% <100.00%> (ø)
src/core/common/log.hpp 100.00% <ø> (ø)
src/core/mac/mac.cpp 90.70% <ø> (+8.34%) ⬆️
src/core/mac/sub_mac.cpp 90.82% <100.00%> (+2.01%) ⬆️
src/core/meshcop/border_agent_tracker.hpp 0.00% <ø> (ø)
src/core/meshcop/dataset_manager.cpp 94.28% <100.00%> (+3.28%) ⬆️
src/core/radio/trel_peer_discoverer.cpp 97.24% <ø> (+1.87%) ⬆️
src/core/thread/address_resolver.cpp 94.57% <100.00%> (+0.88%) ⬆️
... and 14 more

... and 272 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

@abtink
abtink marked this pull request as ready for review February 24, 2026 18:18
@jwhui
jwhui merged commit 65c1b97 into openthread:main Feb 24, 2026
105 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants