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 // Use the highest available log flag to indicate an assert failure. 23 #define PW_LOG_ASSERT_FAILED_FLAG (1u << (PW_LOG_FLAG_BITS - 1u)) 24 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 27 // behaviour. 28 #define PW_HANDLE_CRASH(message, ...) \ 29 do { \ 30 PW_LOG(PW_LOG_LEVEL_CRITICAL, \ 31 PW_LOG_ASSERT_FAILED_FLAG, \ 32 __FILE__ ":%d: Crash: " message, \ 33 __LINE__, \ 34 __VA_ARGS__); \ 35 PW_UNREACHABLE; \ 36 } while (0) 37 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, ...) \ 42 do { \ 43 PW_LOG(PW_LOG_LEVEL_CRITICAL, \ 44 PW_LOG_ASSERT_FAILED_FLAG, \ 45 __FILE__ ":%d: Check failed: " condition_string ". " message, \ 46 __LINE__, \ 47 __VA_ARGS__); \ 48 PW_UNREACHABLE; \ 49 } while (0) 50 51 // Sample assert failure message produced by the below implementation: 52 // 53 // foo.cc:25: Check failed: old_x (=610) < new_x (=50). Details: foo=10, bar. 54 // 55 // Putting the value next to the operand makes the string easier to read. 56 57 // clang-format off 58 // This is too hairy for clang format to handle and retain readability. 59 #define PW_HANDLE_ASSERT_BINARY_COMPARE_FAILURE(arg_a_str, \ 60 arg_a_val, \ 61 comparison_op_str, \ 62 arg_b_str, \ 63 arg_b_val, \ 64 type_fmt, \ 65 message, ...) \ 66 do { \ 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 ")" \ 73 ". " message, \ 74 __LINE__, arg_a_val, arg_b_val, __VA_ARGS__); \ 75 PW_UNREACHABLE; \ 76 } while(0) 77 // clang-format on 78