libclod
C library for interacting with NBTs, region files, LOD data and other things.
Loading...
Searching...
No Matches
debug.h
1#ifndef LIBCLOD_CLOD_DEBUG_H
2#define LIBCLOD_CLOD_DEBUG_H
3
4#include <clod/lib.h>
5#include <clod/stream.h>
6#include <clod/string.h>
7#include <clod/sys/sys.h>
8
9static inline int debug_print(
10 const char *context,
11 const char *file,
12 const int line,
13 const char *func,
14 const char *msg,
15 ...
16) {
17 struct clod_string buff = CLOD_STRING_NEW(256);
19 clod_string_cat(&buff, CLOD_STRING_C(":"));
21 clod_string_cat(&buff, CLOD_STRING_C(":"));
22 clod_string_put_uint(&buff, (uintmax_t)line, CLOD_STRING_DIGIT_ALPHABET, 10, 0, 0);
23 clod_string_cat(&buff, CLOD_STRING_C(" "));
25 clod_string_cat(&buff, CLOD_STRING_C("():\n\t"));
26
27 va_list args;
28 va_start(args, msg);
29 clod_string_vformat(&buff, clod_string_from_cstr(msg), args);
30 va_end(args);
31
32 if (buff.len == buff.cap) buff.len--;
33 buff.ptr[buff.len++] = '\n';
34
35 clod_stderr->write(clod_stderr, &buff);
36 return 1;
37}
38
39#if defined(__GNUC__)
40#define _debug_assume(expr) ((expr) ? (void)0 : __builtin_unreachable())
41#elif defined(_MSC_VER)
42#define _debug_assume(expr) __assume(expr)
43#endif
44
45#define CLOD_TEST 1
46
47#define debug(context, msg, ...) (!(context) ? (void)0 : (debug_print(#context, __FILE__, __LINE__, __func__, msg __VA_OPT__(,) __VA_ARGS__), (void)0))
48#define fatal(context, msg, ...) (!(context) ? (void)0 : (debug_print(#context, __FILE__, __LINE__, __func__, msg __VA_OPT__(,) __VA_ARGS__), clod_exit(1), _debug_assume(0)))
49#define assert_fatal(context, expr, msg, ...) (!(context) ? _debug_assume(expr) : (expr) ? (void)0 : (debug_print(#context, __FILE__, __LINE__, __func__, "Assertion \""#expr"\" failed: "msg __VA_OPT__(,) __VA_ARGS__), clod_exit(1), _debug_assume(0)))
50
51#endif
Sized string helpers.
#define CLOD_STRING_C(cstr)
String literal constant.
Definition string.h:43
size_t clod_string_cat(struct clod_string *dst, struct clod_string src)
Definition string.c:17
CLOD_API size_t clod_string_put_uint(struct clod_string *dst, uintmax_t val, struct clod_string alphabet, unsigned char base, unsigned char min_digits, unsigned char max_digits)
Definition serialise.c:50
struct clod_string clod_string_from_cstr(const char *cstr)
Make a string from a C string.
Definition string.c:5
#define CLOD_STRING_NEW(size)
Create a new empty string with the given capacity on the stack.
Definition string.h:46
char * ptr
Definition string.h:28
ptrdiff_t len
Definition string.h:32
ptrdiff_t cap
Definition string.h:36
Definition file.c:13