1 /* -*- Mode: C; indent-tabs-mode: t; tab-width: 4 -*-
2 // ---------------------------------------------------------------------------
4 // Copyright (C) Stephanie Gawroriski <xer@multiphasicapps.net>
5 // ---------------------------------------------------------------------------
6 // SquirrelJME is under the Mozilla Public License Version 2.0.
7 // See license.mkd for licensing and copyright information.
8 // -------------------------------------------------------------------------*/
16 #ifndef SQUIRRELJME_DEBUG_H
17 #define SQUIRRELJME_DEBUG_H
21 #include "sjme/stdTypes.h"
22 #include "sjme/error.h"
26 #ifndef SJME_CXX_IS_EXTERNED
27 #define SJME_CXX_IS_EXTERNED
28 #define SJME_CXX_SQUIRRELJME_DEBUG_H
30 #endif /* #ifdef SJME_CXX_IS_EXTERNED */
31 #endif /* #ifdef __cplusplus */
33 /*--------------------------------------------------------------------------*/
35 #define SJME_DEBUG_DECL_FILE_LINE_FUNC \
36 sjme_attrInNullable sjme_lpcstr file, \
37 sjme_attrInValue int line, \
38 sjme_attrInNullable sjme_lpcstr func
40 /** File, line, and function. */
41 #define SJME_DEBUG_FILE_LINE_FUNC_ALWAYS __FILE__, __LINE__, __func__
43 #if defined(SJME_CONFIG_RELEASE)
45 #define SJME_DEBUG_ONLY_COMMA
47 /** Optional declaration for debugging. */
48 #define SJME_DEBUG_DECL_FILE_LINE_FUNC_OPTIONAL
50 /** File, line, and function. */
51 #define SJME_DEBUG_FILE_LINE_FUNC NULL, -1, NULL
53 /** Copy of file line and function. */
54 #define SJME_DEBUG_FILE_LINE_COPY
56 /** Only emitted in debugging. */
57 #define SJME_ONLY_IN_DEBUG_EXPR(expr) do {} while(0)
59 /** Only emitted in debugging. */
60 #define SJME_ONLY_IN_DEBUG_PP(expr)
62 /** Release/Debug ternary. */
63 #define SJME_DEBUG_TERNARY(debug, release) release
65 /** Debug identifier. */
66 #define SJME_DEBUG_IDENTIFIER(ident) ident
69 #define SJME_DEBUG_ONLY_COMMA ,
71 /** Optional declaration for debugging. */
72 #define SJME_DEBUG_DECL_FILE_LINE_FUNC_OPTIONAL \
73 SJME_DEBUG_DECL_FILE_LINE_FUNC
75 /** File, line, and function. */
76 #define SJME_DEBUG_FILE_LINE_FUNC SJME_DEBUG_FILE_LINE_FUNC_ALWAYS
78 /** Copy of file line and function. */
79 #define SJME_DEBUG_FILE_LINE_COPY file, line, func
81 /** Only emitted in debugging. */
82 #define SJME_ONLY_IN_DEBUG_EXPR(expr) expr
84 /** Only emitted in debugging. */
85 #define SJME_ONLY_IN_DEBUG_PP(expr) expr
87 /** Release/Debug ternary. */
88 #define SJME_DEBUG_TERNARY(debug, release) debug
90 /** Debug identifier. */
91 #define SJME_DEBUG_IDENTIFIER(ident) ident##R
95 * Prints a debug message.
97 * @param file The file printing from.
98 * @param line The line printing from.
99 * @param func The function printing from.
100 * @param isBlank Is this blank?
101 * @param message The @c printf style message.
102 * @param ... Any @c printf style arguments.
105 void sjme_messageR(SJME_DEBUG_DECL_FILE_LINE_FUNC
,
106 sjme_attrInValue sjme_jboolean isBlank
,
107 sjme_attrInNullable sjme_attrFormatArg sjme_lpcstr message
, ...)
108 sjme_attrFormatOuter(4, 5);
111 * Hex dumps the given data.
113 * @param inData The data to dump.
114 * @param inLen The data length.
117 void sjme_message_hexDump(
118 sjme_attrInNullable sjme_buffer inData
,
119 sjme_attrInPositive sjme_jint inLen
);
121 #if defined(SJME_CONFIG_DEBUG)
123 * Prints a debug message.
125 * @param file The file printing from.
126 * @param line The line printing from.
127 * @param func The function printing from.
128 * @param isBlank Is this blank?
129 * @param message The @c printf style message.
130 * @param args Any @c printf style arguments.
133 void sjme_messageV(SJME_DEBUG_DECL_FILE_LINE_FUNC
,
134 sjme_attrInValue sjme_jboolean isBlank
,
135 sjme_attrInNullable sjme_attrFormatArg sjme_lpcstr message
,
140 * Prints a debug message
142 * @param message The @c printf style message.
143 * @param ... Any @c printf style arguments.
146 #define sjme_message(...) SJME_ONLY_IN_DEBUG_EXPR( \
147 sjme_messageR(SJME_DEBUG_FILE_LINE_FUNC, SJME_JNI_FALSE, __VA_ARGS__))
150 * Indicates a fatal error and exits the program.
152 * @param file The file printing from.
153 * @param line The line printing from.
154 * @param func The function printing from.
155 * @param message The @c printf style message.
156 * @param ... Any @c printf style arguments.
157 * @return Never returns.
160 sjme_errorCode
sjme_dieR(SJME_DEBUG_DECL_FILE_LINE_FUNC
,
161 sjme_attrInNullable sjme_attrFormatArg sjme_lpcstr message
, ...)
162 sjme_attrReturnNever
sjme_attrFormatOuter(3, 4);
165 * Indicates a fatal error and exits the program.
167 * @param message The @c printf style message.
168 * @param ... Any @c printf style arguments.
169 * @return Never returns.
172 #define sjme_die(...) sjme_dieR(SJME_DEBUG_FILE_LINE_FUNC, __VA_ARGS__)
175 * Indicates a fatal error and exits the program.
177 * @param message The @c printf style message.
178 * @param ... Any @c printf style arguments.
179 * @return Never returns.
182 #define sjme_dieP(...) ((sjme_pointer)((intptr_t)sjme_dieR(\
183 SJME_DEBUG_FILE_LINE_FUNC, __VA_ARGS__)))
186 * Indicates a To-Do and then terminates the program.
188 * @param file The file printing from.
189 * @param line The line printing from.
190 * @param func The function printing from.
191 * @param message The @c printf style message.
192 * @param ... Any @c printf style arguments.
193 * @return Never returns.
196 void sjme_todoR(SJME_DEBUG_DECL_FILE_LINE_FUNC
,
197 sjme_attrInNullable sjme_attrFormatArg sjme_lpcstr message
, ...)
198 sjme_attrReturnNever
sjme_attrFormatOuter(3, 4);
201 * Indicates a To-Do and then terminates the program.
203 * @param message The @c printf style message.
204 * @param ... Any @c printf style arguments.
205 * @return Never returns.
208 #define sjme_todo(...) sjme_todoR(SJME_DEBUG_FILE_LINE_FUNC_ALWAYS, \
212 * Potentially debug aborts.
216 void sjme_debug_abort(void);
219 * Shorted the path of the specified file for debug printing purposes.
221 * @param file The file to shorten.
222 * @return The resultant shortened file.
225 sjme_lpcstr
sjme_debug_shortenFile(sjme_lpcstr file
);
228 * Allows for optional debug abort when a fatal error is hit.
230 * @param error The error to return.
231 * @return The @c error value.
234 sjme_errorCode
sjme_error_fatalR(SJME_DEBUG_DECL_FILE_LINE_FUNC
,
235 sjme_attrInValue sjme_errorCode error
);
238 * Allows for optional debug abort when a fatal error is hit.
240 * @param error The error to return.
241 * @return The @c error value.
244 #define sjme_error_fatal(error) \
245 sjme_error_fatalR(SJME_DEBUG_FILE_LINE_FUNC_ALWAYS, error)
248 * Allows for optional debug abort when unimplemented code is hit.
250 * @param context Any value.
251 * @return Always @c SJME_ERROR_NOT_IMPLEMENTED .
254 sjme_errorCode
sjme_error_notImplementedR(SJME_DEBUG_DECL_FILE_LINE_FUNC
,
255 sjme_attrInValue sjme_intPointer context
);
258 * Allows for optional debug abort when unimplemented code is hit.
260 * @param context Any value.
261 * @return Always @c SJME_ERROR_NOT_IMPLEMENTED .
264 #define sjme_error_notImplemented(context) \
265 sjme_error_notImplementedR(SJME_DEBUG_FILE_LINE_FUNC_ALWAYS, \
266 (sjme_intPointer)(context))
269 * Allows for optional debug abort when out of memory is hit.
271 * @param inPool The pool the allocation was within, if applicable.
272 * @param context Any value.
273 * @return Always @c SJME_ERROR_OUT_OF_MEMORY .
276 sjme_errorCode
sjme_error_outOfMemoryR(SJME_DEBUG_DECL_FILE_LINE_FUNC
,
277 sjme_attrInNullable sjme_alloc_pool
* inPool
,
278 sjme_attrInValue sjme_intPointer context
);
281 * Allows for optional debug abort when out of memory is hit.
283 * @param inPool The pool the allocation was within, if applicable.
284 * @param context Any value.
285 * @return Always @c SJME_ERROR_OUT_OF_MEMORY .
288 #define sjme_error_outOfMemory(inPool, context) \
289 sjme_error_outOfMemoryR(SJME_DEBUG_FILE_LINE_FUNC_ALWAYS, \
290 (inPool), (sjme_intPointer)(context))
293 * Handles specific debug abort scenarios.
295 * @return Return @c SJME_JNI_TRUE if it was handled and abort should be
296 * cancelled, otherwise @c SJME_JNI_FALSE will continue aborting.
299 typedef sjme_jboolean (*sjme_debug_abortHandlerFunc
)(void);
302 * Handler for specific debug exit scenarios.
304 * @param exitCode The exit code.
305 * @return Return @c SJME_JNI_TRUE if it was handled.
308 typedef sjme_jboolean (*sjme_debug_exitHandlerFunc
)(int exitCode
);
311 * Emits a dangling reference message.
313 * @param fullMessage The message to emit.
314 * @param partMessage Partial message, without any prepend.
315 * @return Return @c SJME_JNI_TRUE if the message is handled, otherwise
316 * a standard @c fprintf to @c stderr will be used.
319 typedef sjme_jboolean (*sjme_debug_messageHandlerFunc
)(sjme_lpcstr fullMessage
,
320 sjme_lpcstr partMessage
);
323 * The set of functions to use for debugging functions.
327 typedef struct sjme_debug_handlerFunctions
329 /** The handler for debug aborts. */
330 sjme_debug_abortHandlerFunc abort
;
332 /** The handler for debug exits. */
333 sjme_debug_exitHandlerFunc exit
;
335 /** The dangling message implementation to use. */
336 sjme_debug_messageHandlerFunc message
;
337 } sjme_debug_handlerFunctions
;
339 /*--------------------------------------------------------------------------*/
343 #ifdef SJME_CXX_SQUIRRELJME_DEBUG_H
345 #undef SJME_CXX_SQUIRRELJME_DEBUG_H
346 #undef SJME_CXX_IS_EXTERNED
347 #endif /* #ifdef SJME_CXX_SQUIRRELJME_DEBUG_H */
348 #endif /* #ifdef __cplusplus */
350 #endif /* SQUIRRELJME_DEBUG_H */