-
Notifications
You must be signed in to change notification settings - Fork 0
/
log.h
46 lines (33 loc) · 1.57 KB
/
log.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
42
43
44
45
46
#ifndef LOG_H
#define LOG_H
#include "util.h"
#include <stdarg.h>
#include <stdio.h>
#define DECL_LOG_FN(NAME) \
PRINTF_ARGS(0) void NAME(const char *format, ...); \
PRINTF_ARGS(0) void NAME##sl(const char *format, ...); \
PRINTF_ARGS(1) void f##NAME(FILE *file, const char *format, ...); \
PRINTF_ARGS(1) void f##NAME##sl(FILE *file, const char *format, ...);
void enable_log(void);
void disable_log(void);
bool log_enabled(void);
DECL_LOG_FN(err)
DECL_LOG_FN(warn)
DECL_LOG_FN(info)
DECL_LOG_FN(debug)
DECL_LOG_FN(trace)
DECL_LOG_FN(slog)
#undef DECL_LOG_FN
#define BEGIN_STAGE(name) slog("\n\n********** " name " **********\n")
#define BEGIN_SUB_STAGE(name) slog("\n\n>> " name " \n")
#define _DBG_FORMAT_STR(val, specifier) #val ": " specifier "\n"
#define _GENERIC_DBG_FORMAT_SPECIFIER(val) \
_Generic((val), \
char *: _DBG_FORMAT_STR(val, "%s"), \
int: _DBG_FORMAT_STR(val, "%d"), \
size_t: _DBG_FORMAT_STR(val, "%zu"), \
float: _DBG_FORMAT_STR(val, "%f"), \
double: _DBG_FORMAT_STR(val, "%lf"), \
default: _DBG_FORMAT_STR(val, "unknown type for `DEBUG` macro"))
#define DEBUG(val) fprintf(stderr, _GENERIC_DBG_FORMAT_SPECIFIER(val), (val))
#endif