summaryrefslogtreecommitdiff
path: root/libmisc/assert.c
blob: 3f4df476ee62a293ae0e0d1d96ed8d24b6fbd8e0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
/* libmisc/assert.c - More assertions
 *
 * Copyright (C) 2024  Luke T. Shumaker <lukeshu@lukeshu.com>
 * SPDX-License-Identifier: AGPL-3.0-or-later
 */

#include <stdbool.h> /* for bool, true, false */
#include <stdio.h>   /* for printf() */
#include <stdlib.h>  /* for abort() */

#include <libmisc/assert.h>

#ifndef NDEBUG

static bool in_fail = false;

__attribute__((noreturn, weak))
void __assert_msg_fail(const char *expr,
                  const char *file, unsigned int line, const char *func,
                  const char *msg) {
	if (!in_fail) {
		in_fail = true;
		printf("error: %s:%u:%s(): assertion \"%s\" failed%s%s\n",
		       file, line, func,
		       expr,
		       msg ? ": " : "", msg);
	}
	abort();
}

#endif