/src/systemd/src/basic/assert-util.c
Line | Count | Source |
1 | | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
2 | | |
3 | | #include <stdio.h> |
4 | | #include <stdlib.h> |
5 | | |
6 | | #include "assert-util.h" |
7 | | #include "errno-util.h" |
8 | | #include "log.h" |
9 | | |
10 | | /* Akin to glibc's __abort_msg; which is private and we hence cannot |
11 | | * use here. */ |
12 | | static char *log_abort_msg = NULL; |
13 | | |
14 | | static void log_assert( |
15 | | int level, |
16 | | const char *text, |
17 | | const char *file, |
18 | | int line, |
19 | | const char *func, |
20 | 0 | const char *format) { |
21 | |
|
22 | 0 | static char buffer[LINE_MAX]; |
23 | |
|
24 | 0 | if (_likely_(LOG_PRI(level) > log_get_max_level())) |
25 | 0 | return; |
26 | | |
27 | 0 | DISABLE_WARNING_FORMAT_NONLITERAL; |
28 | 0 | (void) snprintf(buffer, sizeof buffer, format, text, file, line, func); |
29 | 0 | REENABLE_WARNING; |
30 | |
|
31 | 0 | log_abort_msg = buffer; |
32 | |
|
33 | 0 | log_dispatch_internal(level, 0, file, line, func, NULL, NULL, NULL, NULL, buffer); |
34 | 0 | } |
35 | | |
36 | 0 | _noreturn_ void log_assert_failed(const char *text, const char *file, int line, const char *func) { |
37 | 0 | log_assert(LOG_CRIT, text, file, line, func, |
38 | 0 | "Assertion '%s' failed at %s:%u, function %s(). Aborting."); |
39 | 0 | abort(); |
40 | 0 | } |
41 | | |
42 | 0 | _noreturn_ void log_assert_failed_unreachable(const char *file, int line, const char *func) { |
43 | 0 | log_assert(LOG_CRIT, "Code should not be reached", file, line, func, |
44 | 0 | "%s at %s:%u, function %s(). Aborting. 💥"); |
45 | 0 | abort(); |
46 | 0 | } |
47 | | |
48 | 0 | void log_assert_failed_return(const char *text, const char *file, int line, const char *func) { |
49 | | /* log_get_assert_return_is_critical is a weak symbol. It may be NULL. */ |
50 | 0 | if (log_get_assert_return_is_critical && log_get_assert_return_is_critical()) |
51 | 0 | log_assert_failed(text, file, line, func); |
52 | |
|
53 | 0 | PROTECT_ERRNO; |
54 | | log_assert(LOG_DEBUG, text, file, line, func, |
55 | 0 | "Assertion '%s' failed at %s:%u, function %s(), ignoring."); |
56 | 0 | } |