Skip to content

Commit

Permalink
pw_log_null: Prevent asserts being routed to pw_log_null
Browse files Browse the repository at this point in the history
pw_assert_log routes asserts to pw_log. If pw_log_null is in use, those
asserts do nothing, which is dangerous. Similarly, a
PW_LOG(PW_LOG_LEVEL_FATAL, ...) message does nothing with pw_log_null.

Prevent fatal logs or asserts from doing nothing in pw_log_null by
failing to compile. This is done by checking the log level in a
static_assert. Note that this also prevents pw_log_null from working
with non-constant log levels.

Change-Id: I87d645115f5053ebad5127bda67b088a632ed562
Reviewed-on: https://pigweed-review.googlesource.com/c/pigweed/pigweed/+/262853
Docs-Not-Needed: Wyatt Hepler <[email protected]>
Presubmit-Verified: CQ Bot Account <[email protected]>
Lint: Lint 🤖 <[email protected]>
Pigweed-Auto-Submit: Wyatt Hepler <[email protected]>
Commit-Queue: Auto-Submit <[email protected]>
Reviewed-by: Dave Roth <[email protected]>
  • Loading branch information
255 authored and CQ Bot Account committed Feb 13, 2025
1 parent e1d73a0 commit 04fb4db
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
5 changes: 4 additions & 1 deletion pw_log_null/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,10 @@ pw_source_set("pw_log_null") {
public = [ "public_overrides/pw_log_backend/log_backend.h" ]
sources = [ "public/pw_log_null/log_null.h" ]
friend = [ ":*" ]
public_deps = [ dir_pw_preprocessor ]
public_deps = [
"$dir_pw_log:pw_log.facade",
dir_pw_preprocessor,
]
}

pw_source_set("pw_log_null.impl") {
Expand Down
1 change: 1 addition & 0 deletions pw_log_null/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ pw_add_library(pw_log_null INTERFACE
public_overrides
PUBLIC_DEPS
pw_preprocessor
pw_log.facade
)

pw_add_test(pw_log_null.test
Expand Down
12 changes: 9 additions & 3 deletions pw_log_null/public/pw_log_null/log_null.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
// the License.
#pragma once

#include "pw_preprocessor/arguments.h"
#include "pw_log/levels.h"
#include "pw_preprocessor/compiler.h"
#include "pw_preprocessor/util.h"

Expand Down Expand Up @@ -49,5 +49,11 @@ static inline void pw_log_Ignored(int level,

PW_EXTERN_C_END

#define PW_HANDLE_LOG(level, module, flags, message, ...) \
pw_log_Ignored((level), module, (flags), message PW_COMMA_ARGS(__VA_ARGS__))
#define PW_HANDLE_LOG(level, module, flags, ...) \
do { \
static_assert( \
(level) != PW_LOG_LEVEL_FATAL, \
"pw_log_null does not support FATAL logs, including asserts from " \
"pw_assert_log; disabling FATAL logs or asserts is unsafe"); \
pw_log_Ignored((level), (module), (flags), __VA_ARGS__); \
} while (0)

0 comments on commit 04fb4db

Please sign in to comment.