-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdebug.h
41 lines (35 loc) · 927 Bytes
/
debug.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#ifndef DEBUG_H
#define DEBUG_H
// A marginally-clever debugging function that expends to a no-op
// when NDEBUG is defined and otherwise prints its string formatting
// arguments to stderr.
#ifdef NDEBUG
#define debug(fmt, ...)
#define debug_c(expr, fmt, ...)
#else
#include <stdarg.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
static void debug(const char* fmt, ...) {
va_list argptr ;
va_start(argptr, fmt) ;
char* fmt_newline ;
asprintf(&fmt_newline, "%s\n", fmt) ;
vfprintf(stderr, fmt_newline, argptr) ;
free(fmt_newline) ;
fflush(stderr) ;
}
static void debug_c(bool expr, const char* fmt, ...) {
if (expr) {
va_list argptr ;
va_start(argptr, fmt) ;
char* fmt_newline ;
asprintf(&fmt_newline, "%s\n", fmt) ;
vfprintf(stderr, fmt_newline, argptr) ;
free(fmt_newline) ;
fflush(stderr) ;
}
}
#endif
#endif