Debian Codegraph @

libmowgli-2/src/libmowgli/ext/error_backtrace.c

raw
1 /*
2 * libmowgli: A collection of useful routines for programming.
3 * error_backtrace.c: Print errors and explain how they were reached.
4 *
5 * Copyright (c) 2007 William Pitcock <nenolod -at- sacredspiral.co.uk>
6 *
7 * Permission to use, copy, modify, and/or distribute this software for any
8 * purpose with or without fee is hereby granted, provided that the above
9 * copyright notice and this permission notice is present in all copies.
10 *
11 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
12 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
13 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
14 * DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT,
15 * INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
16 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
17 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
18 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
19 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING
20 * IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
21 * POSSIBILITY OF SUCH DAMAGE.
22 */
23
24 #include "mowgli.h"
25
26 void
27 mowgli_error_context_display(mowgli_error_context_t *e, const char *delim)
28 {
29 return_if_fail(e != NULL);
30 return_if_fail(delim != NULL);
31 return_if_fail(MOWGLI_LIST_LENGTH(&e->bt) != 0);
32
33 mowgli_node_t *n;
34 char *bt_msg;
35
36 MOWGLI_LIST_FOREACH(n, e->bt.head)
37 {
38 bt_msg = (char *) n->data;
39
40 fprintf(stderr, "%s%s", bt_msg, n->next != NULL ? delim : "\n");
41 }
42 }
43
44 void
45 mowgli_error_context_destroy(mowgli_error_context_t *e)
46 {
47 mowgli_node_t *n, *tn;
48
49 return_if_fail(e != NULL);
50
51 if (MOWGLI_LIST_LENGTH(&e->bt) == 0)
52 {
53 mowgli_free(e);
54 return;
55 }
56
57 MOWGLI_LIST_FOREACH_SAFE(n, tn, e->bt.head)
58 {
59 mowgli_free(n->data);
60
61 mowgli_node_delete(n, &e->bt);
62 mowgli_node_free(n);
63 }
64
65 mowgli_free(e);
66 }
67
68 void
69 mowgli_error_context_display_with_error(mowgli_error_context_t *e, const char *delim, const char *error)
70 {
71 mowgli_error_context_display(e, delim);
72 fprintf(stderr, "Error: %s\n", error);
73
74 _exit(EXIT_FAILURE);
75 }
76
77 void
78 mowgli_error_context_push(mowgli_error_context_t *e, const char *msg, ...)
79 {
80 char buf[65535];
81 va_list va;
82
83 return_if_fail(e != NULL);
84 return_if_fail(msg != NULL);
85
86 va_start(va, msg);
87 vsnprintf(buf, 65535, msg, va);
88 va_end(va);
89
90 mowgli_node_add(mowgli_strdup(buf), mowgli_node_create(), &e->bt);
91 }
92
93 void
94 mowgli_error_context_pop(mowgli_error_context_t *e)
95 {
96 return_if_fail(e != NULL);
97
98 mowgli_node_delete(e->bt.tail, &e->bt);
99 }
100
101 mowgli_error_context_t *
102 mowgli_error_context_create(void)
103 {
104 mowgli_error_context_t *out = mowgli_alloc(sizeof *out);
105
106 return out;
107 }