1 // Copyright 2020 The Pigweed Authors
3 // Licensed under the Apache License, Version 2.0 (the "License"); you may not
4 // use this file except in compliance with the License. You may obtain a copy of
7 // https://www.apache.org/licenses/LICENSE-2.0
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11 // WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12 // License for the specific language governing permissions and limitations under
16 #include "pw_log/levels.h"
17 #include "pw_log/log.h"
18 #include "pw_log/options.h"
19 #include "pw_preprocessor/compiler.h"
20 #include "pw_preprocessor/util.h"
22 // Use the highest available log flag to indicate an assert failure.
23 #define PW_LOG_ASSERT_FAILED_FLAG (1u << (PW_LOG_FLAG_BITS - 1u))
25 // Die with a message with several attributes included. This crash frontend
26 // funnels everything into the logger, which must then handle the true crash
28 #define PW_HANDLE_CRASH(message, ...) \
30 PW_LOG(PW_LOG_LEVEL_CRITICAL, \
31 PW_LOG_ASSERT_FAILED_FLAG, \
32 __FILE__ ":%d: Crash: " message, \
38 // Die with a message with several attributes included. This assert frontend
39 // funnels everything into the logger, which is responsible for displaying the
40 // log, then crashing/rebooting the device.
41 #define PW_HANDLE_ASSERT_FAILURE(condition_string, message, ...) \
43 PW_LOG(PW_LOG_LEVEL_CRITICAL, \
44 PW_LOG_ASSERT_FAILED_FLAG, \
45 __FILE__ ":%d: Check failed: " condition_string ". " message, \
51 // Sample assert failure message produced by the below implementation:
53 // foo.cc:25: Check failed: old_x (=610) < new_x (=50). Details: foo=10, bar.
55 // Putting the value next to the operand makes the string easier to read.
58 // This is too hairy for clang format to handle and retain readability.
59 #define PW_HANDLE_ASSERT_BINARY_COMPARE_FAILURE(arg_a_str, \
67 PW_LOG(PW_LOG_LEVEL_CRITICAL, \
68 PW_LOG_ASSERT_FAILED_FLAG, \
69 __FILE__ ":%d: Check failed: " \
70 arg_a_str " (=" type_fmt ") " \
71 comparison_op_str " " \
72 arg_b_str " (=" type_fmt ")" \
74 __LINE__, arg_a_val, arg_b_val, __VA_ARGS__); \