comparison src/libmlk-core/core/panic.c @ 320:8f9937403749

misc: improve loading of data
author David Demelier <markand@malikania.fr>
date Fri, 01 Oct 2021 20:30:00 +0200
parents libmlk-core/core/panic.c@d01e83210ca2
children 76ecec33dc9b
comparison
equal deleted inserted replaced
319:b843eef4cc35 320:8f9937403749
1 /*
2 * panic.c -- unrecoverable error handling
3 *
4 * Copyright (c) 2020-2021 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 "core_p.h"
24 #include "error.h"
25 #include "panic.h"
26
27 static void
28 terminate(void)
29 {
30 fprintf(stderr, _("abort: %s\n"), error());
31 abort();
32 exit(1);
33 }
34
35 void (*panic_handler)(void) = terminate;
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 }