comparison libmlk-core/mlk/core/panic.c @ 431:8f59201dc76b

core: cleanup hierarchy
author David Demelier <markand@malikania.fr>
date Sat, 15 Oct 2022 20:23:14 +0200
parents src/libmlk-core/core/panic.c@d74f53299252
children 773a082f0b91
comparison
equal deleted inserted replaced
430:1645433e008d 431:8f59201dc76b
1 /*
2 * panic.c -- unrecoverable error handling
3 *
4 * Copyright (c) 2020-2022 David Demelier <markand@malikania.fr>
5 *
6 * Permission to use, copy, modify, and/or distribute this software for any
7 * purpose with or without fee is hereby granted, provided that the above
8 * copyright notice and this permission notice appear in all copies.
9 *
10 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18
19 #include <assert.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22
23 #include "error.h"
24 #include "panic.h"
25
26 static void
27 terminate(void)
28 {
29 fprintf(stderr, "abort: %s\n", error());
30 abort();
31 exit(1);
32 }
33
34 void (*panic_handler)(void) = terminate;
35 void *panic_data = NULL;
36
37 void
38 panicf(const char *fmt, ...)
39 {
40 assert(fmt);
41
42 va_list ap;
43
44 /*
45 * Store the error before calling panic because va_end would not be
46 * called.
47 */
48 va_start(ap, fmt);
49 errorva(fmt, ap);
50 va_end(ap);
51
52 panic();
53 }
54
55 void
56 panicva(const char *fmt, va_list ap)
57 {
58 assert(fmt);
59 assert(panic_handler);
60
61 errorva(fmt, ap);
62 panic();
63 }
64
65 void
66 panic(void)
67 {
68 assert(panic_handler);
69
70 panic_handler();
71
72 /*
73 * This should not happen, if it does it means the user did not fully
74 * satisfied the constraint of panic_handler.
75 */
76 fprintf(stderr, "abort: panic handler returned\n");
77 exit(1);
78 }