• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2020 The Pigweed Authors
2 //
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
5 // the License at
6 //
7 //     https://www.apache.org/licenses/LICENSE-2.0
8 //
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
13 // the License.
14 #pragma once
15 
16 #include "pw_log/levels.h"
17 #include "pw_log/log.h"
18 #include "pw_log/options.h"
19 #include "pw_preprocessor/arguments.h"
20 #include "pw_preprocessor/compiler.h"
21 #include "pw_preprocessor/util.h"
22 
23 // Die with a message with several attributes included. This crash frontend
24 // funnels everything into the logger, which must then handle the true crash
25 // behaviour.
26 #define PW_HANDLE_CRASH(...)              \
27   do {                                    \
28     PW_HANDLE_LOG(PW_LOG_LEVEL_FATAL,     \
29                   PW_LOG_MODULE_NAME,     \
30                   PW_LOG_FLAGS,           \
31                   "Crash: " __VA_ARGS__); \
32     PW_UNREACHABLE;                       \
33   } while (0)
34 
35 // Die with a message with several attributes included. This assert frontend
36 // funnels everything into the logger, which is responsible for displaying the
37 // log, then crashing/rebooting the device.
38 #define PW_HANDLE_ASSERT_FAILURE(condition_string, ...)         \
39   do {                                                          \
40     PW_LOG(PW_LOG_LEVEL_FATAL,                                  \
41            PW_LOG_MODULE_NAME,                                  \
42            PW_LOG_FLAGS,                                        \
43            "Check failed: " condition_string ". " __VA_ARGS__); \
44     PW_UNREACHABLE;                                             \
45   } while (0)
46 
47 // Sample assert failure message produced by the below implementation:
48 //
49 //   Check failed: old_x (=610) < new_x (=50). Details: foo=10, bar.
50 //
51 // Putting the value next to the operand makes the string easier to read.
52 
53 // clang-format off
54 // This is too hairy for clang format to handle and retain readability.
55 #define PW_HANDLE_ASSERT_BINARY_COMPARE_FAILURE(arg_a_str,                    \
56                                                 arg_a_val,                    \
57                                                 comparison_op_str,            \
58                                                 arg_b_str,                    \
59                                                 arg_b_val,                    \
60                                                 type_fmt,                     \
61                                                 message, ...)                 \
62   do {                                                                        \
63     PW_LOG(PW_LOG_LEVEL_FATAL,                                                \
64            PW_LOG_MODULE_NAME,                                                \
65            PW_LOG_FLAGS,                                                      \
66            "Check failed: "                                                   \
67                  arg_a_str " (=" type_fmt ") "                                \
68                  comparison_op_str " "                                        \
69                  arg_b_str " (=" type_fmt ")"                                 \
70                  ". " message,                                                \
71               PW_DROP_LAST_ARG_IF_EMPTY(arg_a_val, arg_b_val, __VA_ARGS__));  \
72     PW_UNREACHABLE;                                                           \
73   } while(0)
74 // clang-format on
75