• 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/compiler.h"
20 #include "pw_preprocessor/util.h"
21 
22 // Die with a message with several attributes included. This crash frontend
23 // funnels everything into the logger, which must then handle the true crash
24 // behaviour.
25 #define PW_HANDLE_CRASH(message, ...)                                         \
26   do {                                                                        \
27     PW_LOG(PW_LOG_LEVEL_FATAL, PW_LOG_FLAGS, "Crash: " message, __VA_ARGS__); \
28     PW_UNREACHABLE;                                                           \
29   } while (0)
30 
31 // Die with a message with several attributes included. This assert frontend
32 // funnels everything into the logger, which is responsible for displaying the
33 // log, then crashing/rebooting the device.
34 #define PW_HANDLE_ASSERT_FAILURE(condition_string, message, ...) \
35   do {                                                           \
36     PW_LOG(PW_LOG_LEVEL_FATAL,                                   \
37            PW_LOG_FLAGS,                                         \
38            "Check failed: " condition_string ". " message,       \
39            __VA_ARGS__);                                         \
40     PW_UNREACHABLE;                                              \
41   } while (0)
42 
43 // Sample assert failure message produced by the below implementation:
44 //
45 //   Check failed: old_x (=610) < new_x (=50). Details: foo=10, bar.
46 //
47 // Putting the value next to the operand makes the string easier to read.
48 
49 // clang-format off
50 // This is too hairy for clang format to handle and retain readability.
51 #define PW_HANDLE_ASSERT_BINARY_COMPARE_FAILURE(arg_a_str,                \
52                                                 arg_a_val,                \
53                                                 comparison_op_str,        \
54                                                 arg_b_str,                \
55                                                 arg_b_val,                \
56                                                 type_fmt,                 \
57                                                 message, ...)             \
58   do {                                                                    \
59     PW_LOG(PW_LOG_LEVEL_FATAL,                                            \
60            PW_LOG_FLAGS,                                                  \
61            "Check failed: "                                               \
62                  arg_a_str " (=" type_fmt ") "                            \
63                  comparison_op_str " "                                    \
64                  arg_b_str " (=" type_fmt ")"                             \
65                  ". " message,                                            \
66               arg_a_val, arg_b_val, __VA_ARGS__);                         \
67     PW_UNREACHABLE;                                                       \
68   } while(0)
69 // clang-format on
70