996
|
1 /* -*- mode: C; coding: utf-8; tab-width: 4; indent-tabs-mode: t; c-basic-offset: 4 -*-
|
|
2 *
|
|
3 * cfunge - A standard-conforming Befunge93/98/109 interpreter in C.
|
|
4 * Copyright (C) 2008-2009 Arvid Norlander <anmaster AT tele2 DOT se>
|
|
5 *
|
|
6 * This program is free software: you can redistribute it and/or modify
|
|
7 * it under the terms of the GNU General Public License as published by
|
|
8 * the Free Software Foundation, either version 3 of the License, or
|
|
9 * (at the proxy's option) any later version. Arvid Norlander is a
|
|
10 * proxy who can decide which future versions of the GNU General Public
|
|
11 * License can be used.
|
|
12 *
|
|
13 * This program is distributed in the hope that it will be useful,
|
|
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
16 * GNU General Public License for more details.
|
|
17 *
|
|
18 * You should have received a copy of the GNU General Public License
|
|
19 * along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
20 */
|
|
21
|
|
22 /**
|
|
23 * @file
|
|
24 * Diagnostics handling.
|
|
25 */
|
|
26
|
|
27 #ifndef FUNGE_HAD_SRC_DIAGNOSTIC_H
|
|
28 #define FUNGE_HAD_SRC_DIAGNOSTIC_H
|
|
29
|
|
30 #include "global.h"
|
|
31 #include "settings.h"
|
|
32
|
|
33 #include <stdarg.h>
|
|
34 #include <stdlib.h> /* abort, exit */
|
|
35
|
|
36 /**
|
|
37 * @defgroup diagnostics Diagnostics
|
|
38 * Various functions and macros for error reporting.
|
|
39 */
|
|
40 /*@{*/
|
|
41 /// For internal use in this header only.
|
|
42 #define DIAG_SOURCELOC "[" __FILE__ ":" FUNGE_CPP_STRINGIFY(__LINE__) "]"
|
|
43
|
|
44 /// Print location in code, out of memory, message and abort().
|
|
45 #define DIAG_OOM(m_reason) \
|
|
46 do { \
|
|
47 fputs("FATAL: Out of memory at " DIAG_SOURCELOC ":\n " m_reason "\n", stderr); \
|
|
48 abort(); \
|
|
49 } while(0)
|
|
50
|
|
51 /**
|
|
52 * Like DIAG_FATAL_LOC() but abort()s instead, thus hopefully producing core
|
|
53 * dump. Meant for internal errors that should never happen.
|
|
54 */
|
|
55 #define DIAG_CRIT_LOC(m_message) \
|
|
56 do { \
|
|
57 fputs("CRITICAL ERROR " DIAG_SOURCELOC ": " m_message "\n", stderr); \
|
|
58 abort(); \
|
|
59 } while(0)
|
|
60
|
|
61
|
|
62 /**
|
|
63 * Like diag_fatal() but includes file and line number, meant for errors that
|
|
64 * should never happen (not for user typoing something).
|
|
65 */
|
|
66 #define DIAG_FATAL_LOC(m_message) \
|
|
67 do { \
|
|
68 fputs("FATAL " DIAG_SOURCELOC ": " m_message "\n", stderr); \
|
|
69 exit(EXIT_FAILURE); \
|
|
70 } while(0)
|
|
71
|
|
72 /**
|
|
73 * Like diag_error() but includes file and line number, meant for errors that
|
|
74 * should never happen (not for user typoing something).
|
|
75 */
|
|
76 #define DIAG_ERROR_LOC(m_message) \
|
|
77 do { \
|
|
78 if (FUNGE_UNLIKELY(setting_enable_errors)) \
|
|
79 fputs("ERROR " DIAG_SOURCELOC ": " m_message "\n", stderr); \
|
|
80 } while(0)
|
|
81
|
|
82 /**
|
|
83 * Like diag_warn() but includes file and line number, meant for errors that
|
|
84 * should never happen (not for user typoing something).
|
|
85 */
|
|
86 #define DIAG_WARN_LOC(m_fmt) \
|
|
87 do { \
|
|
88 if (FUNGE_UNLIKELY(setting_enable_warnings)) \
|
|
89 fputs("WARN " DIAG_SOURCELOC ": " m_message "\n", stderr); \
|
|
90 } while(0)
|
|
91
|
|
92 /**
|
|
93 * Like diag_fatal_format() but includes file and line number, meant for errors
|
|
94 * that should never happen (not for user typoing something).
|
|
95 */
|
|
96 #define DIAG_FATAL_FORMAT_LOC(m_fmt, ...) \
|
|
97 do { \
|
|
98 fprintf(stderr, "FATAL " DIAG_SOURCELOC ": " m_fmt "\n", __VA_ARGS__); \
|
|
99 exit(EXIT_FAILURE); \
|
|
100 } while(0)
|
|
101
|
|
102 /**
|
|
103 * Like diag_error_format() but includes file and line number, meant for errors
|
|
104 * that should never happen (not for user typoing something).
|
|
105 */
|
|
106 #define DIAG_ERROR_FORMAT_LOC(m_fmt, ...) \
|
|
107 do { \
|
|
108 if (FUNGE_UNLIKELY(setting_enable_errors)) \
|
|
109 fprintf(stderr, "ERROR " DIAG_SOURCELOC ": " m_fmt "\n", __VA_ARGS__); \
|
|
110 } while(0)
|
|
111
|
|
112 /**
|
|
113 * Like diag_warn_format() but includes file and line number, meant for errors
|
|
114 * that should never happen (not for user typoing something).
|
|
115 */
|
|
116 #define DIAG_WARN_FORMAT_LOC(m_fmt, ...) \
|
|
117 do { \
|
|
118 if (FUNGE_UNLIKELY(setting_enable_warnings)) \
|
|
119 fprintf(stderr, "WARN " DIAG_SOURCELOC ": " m_fmt "\n", __VA_ARGS__); \
|
|
120 } while(0)
|
|
121
|
|
122
|
|
123
|
|
124 /**
|
|
125 * Prints "FATAL: message" and exits.
|
|
126 * @param message Message to print
|
|
127 */
|
|
128 FUNGE_ATTR_FAST FUNGE_ATTR_COLD FUNGE_ATTR_NONNULL FUNGE_ATTR_NORET
|
|
129 void diag_fatal(const char* message);
|
|
130
|
|
131 /**
|
|
132 * Prints "ERROR: message" and returns.
|
|
133 * @param message Message to print
|
|
134 */
|
|
135 FUNGE_ATTR_FAST FUNGE_ATTR_NONNULL
|
|
136 void diag_error(const char* message);
|
|
137
|
|
138 /**
|
|
139 * Prints "WARN: message" and returns.
|
|
140 * @param message Message to print
|
|
141 */
|
|
142 FUNGE_ATTR_FAST FUNGE_ATTR_NONNULL
|
|
143 void diag_warn(const char* message);
|
|
144
|
|
145 /**
|
|
146 * Prints "FATAL: formatted message" and exits.
|
|
147 * @param format Format string to use.
|
|
148 */
|
|
149 FUNGE_ATTR_FAST FUNGE_ATTR_COLD FUNGE_ATTR_NONNULL FUNGE_ATTR_NORET
|
|
150 FUNGE_ATTR_FORMAT(printf,1,2)
|
|
151 void diag_fatal_format(const char* format, ...);
|
|
152
|
|
153 /**
|
|
154 * Prints "ERROR: formatted message" and returns.
|
|
155 * @param format Format string to use.
|
|
156 */
|
|
157 FUNGE_ATTR_FAST FUNGE_ATTR_NONNULL FUNGE_ATTR_FORMAT(printf,1,2)
|
|
158 void diag_error_format(const char* format, ...);
|
|
159
|
|
160 /**
|
|
161 * Prints "WARN: formatted message" and returns.
|
|
162 * @param format Format string to use.
|
|
163 */
|
|
164 FUNGE_ATTR_FAST FUNGE_ATTR_NONNULL FUNGE_ATTR_FORMAT(printf,1,2)
|
|
165 void diag_warn_format(const char* format, ...);
|
|
166
|
|
167 /*@}*/
|
|
168
|
|
169 #endif
|