1 // Copyright 2022 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 <stdio.h>
17 
18 #include "pw_assert/internal/print_and_abort.h"
19 
20 // Implement the pw_assert backend by forwarding to the print_and_abort.h
21 // macros, which use printf and abort.
22 #define PW_HANDLE_CRASH(...)                         \
23   PW_ASSERT_PRINT_EXPRESSION("CRASH", "PW_CRASH()"); \
24   _PW_ASSERT_PRINT_MESSAGE_AND_ABORT(__VA_ARGS__)
25 
26 #define PW_HANDLE_ASSERT_FAILURE(condition_string, ...)  \
27   PW_ASSERT_PRINT_EXPRESSION("CHECK", condition_string); \
28   _PW_ASSERT_PRINT_MESSAGE_AND_ABORT(__VA_ARGS__)
29 
30 #define PW_HANDLE_ASSERT_BINARY_COMPARE_FAILURE(arg_a_str,                   \
31                                                 arg_a_val,                   \
32                                                 comparison_op_str,           \
33                                                 arg_b_str,                   \
34                                                 arg_b_val,                   \
35                                                 type_fmt,                    \
36                                                 ...)                         \
37   PW_ASSERT_PRINT_EXPRESSION("CHECK",                                        \
38                              arg_a_str " " comparison_op_str " " arg_b_str); \
39   fprintf(stderr,                                                            \
40           "  \033[33mEVALUATED CONDITION\033[0m\n\n    " arg_a_str           \
41           " (=" type_fmt ") " comparison_op_str " " arg_b_str " (=" type_fmt \
42           ")"                                                                \
43           ".\n\n",                                                           \
44           arg_a_val,                                                         \
45           arg_b_val);                                                        \
46   _PW_ASSERT_PRINT_MESSAGE_AND_ABORT(__VA_ARGS__)
47 
48 #define _PW_ASSERT_PRINT_MESSAGE_AND_ABORT(...)                    \
49   fprintf(stderr, "  \033[33mMESSAGE\033[0m\n\n    " __VA_ARGS__); \
50   fprintf(stderr, "\n\n");                                         \
51   fflush(stderr);                                                  \
52   abort()
53