Debian Codegraph @

src/basic/assert-util.c

raw
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 const char *format) {
21
22 static char buffer[LINE_MAX];
23
24 if (_likely_(LOG_PRI(level) > log_get_max_level()))
25 return;
26
27 DISABLE_WARNING_FORMAT_NONLITERAL;
28 (void) snprintf(buffer, sizeof buffer, format, text, file, line, func);
29 REENABLE_WARNING;
30
31 log_abort_msg = buffer;
32
33 log_dispatch_internal(level, 0, file, line, func, NULL, NULL, NULL, NULL, buffer);
34 }
35
36 _noreturn_ void log_assert_failed(const char *text, const char *file, int line, const char *func) {
37 log_assert(LOG_CRIT, text, file, line, func,
38 "Assertion '%s' failed at %s:%u, function %s(). Aborting.");
39 abort();
40 }
41
42 _noreturn_ void log_assert_failed_unreachable(const char *file, int line, const char *func) {
43 log_assert(LOG_CRIT, "Code should not be reached", file, line, func,
44 "%s at %s:%u, function %s(). Aborting. 💥");
45 abort();
46 }
47
48 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 if (log_get_assert_return_is_critical && log_get_assert_return_is_critical())
51 log_assert_failed(text, file, line, func);
52
53 PROTECT_ERRNO;
54 log_assert(LOG_DEBUG, text, file, line, func,
55 "Assertion '%s' failed at %s:%u, function %s(), ignoring.");
56 }