xref: /aosp_15_r20/external/pigweed/pw_assert/public/pw_assert/internal/print_and_abort.h (revision 61c4878ac05f98d0ceed94b57d316916de578985)
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 #include <stdlib.h>
18 
19 #include "pw_assert/config.h"
20 
21 #if PW_ASSERT_ENABLE_DEBUG
22 #define _PW_ASSERT_MACRO(type) "PW_" type "() or PW_D" type "()"
23 #else
24 #define _PW_ASSERT_MACRO(type) "PW_" type "()"
25 #endif  // PW_ASSERT_ENABLE_DEBUG
26 
27 #ifdef __GNUC__
28 #define _PW_ASSERT_ABORT_FUNCTION __PRETTY_FUNCTION__
29 #else
30 #define _PW_ASSERT_ABORT_FUNCTION __func__
31 #endif  // __GNUC__
32 
33 // clang-format off
34 #define _PW_ASSERT_CRASH_BANNER                                   \
35   "\n"                                                            \
36   "   ▄████▄      ██▀███      ▄▄▄           ██████     ██░ ██ \n" \
37   "  ▒██▀ ▀█     ▓██ ▒ ██▒   ▒████▄       ▒██    ▒    ▓██░ ██▒\n" \
38   "  ▒▓█ �� ▄    ▓██ ░▄█ ▒   ▒██  ▀█▄     ░ ▓██▄      ▒██▀▀██░\n" \
39   "  ▒▓▓▄ ▄██▒   ▒██▀▀█▄     ░██▄▄▄▄██      ▒   ██▒   ░▓█ ░██ \n" \
40   "  ▒ ▓███▀ ░   ░██▓ ▒██▒    ▓█   ▓██▒   ▒██████▒▒   ░▓█▒░██▓\n" \
41   "  ░ ░▒ ▒  ░   ░ ▒▓ ░▒▓░    ▒▒   ▓▒█░   ▒ ▒▓▒ ▒ ░    ▒ ░░▒░▒\n" \
42   "    ░  ▒        ░▒ ░ ▒░     ▒   ▒▒ ░   ░ ░▒  ░ ░    ▒ ░▒░ ░\n" \
43   "  ░             ░░   ░      ░   ▒      ░  ░  ░      ░  ░░ ░\n" \
44   "  ░ ░            ░              ░  ░         ░      ░  ░  ░\n" \
45   "  ░\n"                                                         \
46   "\n"
47 // clang-format on
48 
49 // This assert implementation prints the file path, line number, and assert
50 // expression using printf. Uses ANSI escape codes for colors.
51 //
52 // This is done with single printf to work better in multithreaded enironments.
53 #define PW_ASSERT_HANDLE_FAILURE(expression)        \
54   PW_ASSERT_PRINT_EXPRESSION("ASSERT", expression); \
55   fflush(stderr);                                   \
56   abort()
57 
58 #define PW_ASSERT_PRINT_EXPRESSION(macro, expression)            \
59   fflush(stdout);                                                \
60   fprintf(stderr, "\033[31m" _PW_ASSERT_CRASH_BANNER "\033[0m"); \
61   fprintf(stderr,                                     \
62           "\033[41m\033[37m\033[1m%s:%d:\033[0m "     \
63           "\033[1m"                                   \
64           _PW_ASSERT_MACRO(macro)                     \
65           " "                                         \
66           "\033[31mFAILED!\033[0m\n\n"                \
67           "  \033[33mFAILED ASSERTION\033[0m\n\n"                    \
68           "    %s\n\n"                                \
69           "  \033[33mFILE & LINE\033[0m\n\n"                         \
70           "    %s:%d\n\n"                             \
71           "  \033[33mFUNCTION\033[0m\n\n"                            \
72           "    %s\n\n",                               \
73           __FILE__,                                   \
74           __LINE__,                                   \
75           expression,                                 \
76           __FILE__,                                   \
77           __LINE__,                                   \
78           _PW_ASSERT_ABORT_FUNCTION)
79