aboutsummaryrefslogtreecommitdiff
path: root/src/include/util.h
blob: 08654fd101d8d9cd226b9a2599403d949e09184a (plain) (blame)
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
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#ifndef UTIL_H
#define UTIL_H

#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <stdio.h>

/* Welcome to macro Hell. */

#define HIDE(x) do {x} while (0)

#define MIN(a, b) (a < b ? a : b)
#define MAX(a, b) (a > b ? a : b)

/* Log some file info (mostly for use in other LOG_* macros.) */
#define LOG_FINF HIDE( \
   fprintf(stderr, "%s:%s:%d: ", __FILE__, __func__, __LINE__); \
)

/* Call `f` on `x` if `x` exists. */
#define EDO(f, x) HIDE(if (x) {f(x);})

/* Log some debug information. */
#define LOG_DBGF(fmt, ...) HIDE( \
   fprintf(stderr, "\x1b[37m[\x1b[95;1m==\x1b[0m\x1b[37m]\x1b[0m\x1b[35m "); \
   LOG_FINF; \
   fprintf(stderr, fmt, ##__VA_ARGS__); \
   fprintf(stderr, "\x1b[0m\n"); \
)

#define LOG_DBG(body) HIDE( \
   fprintf(stderr, "\x1b[37m[\x1b[95;1m==\x1b[0m\x1b[37m]\x1b[0m\x1b[35m "); \
   LOG_FINF; \
   fprintf(stderr, body); \
   fprintf(stderr, "\x1b[0m\n"); \
)

/* c: */
#define LOG_YAYF(fmt, ...) HIDE( \
   fprintf(stderr, "\x1b[37m[\x1b[92;1m==\x1b[0m\x1b[37m]\x1b[32m "); \
   LOG_FINF; \
   fprintf(stderr, fmt, ##__VA_ARGS__); \
   fprintf(stderr, "\x1b[0m\n"); \
)

#define LOG_YAY(body) HIDE( \
   fprintf(stderr, "\x1b[37m[\x1b[92;1m==\x1b[0m\x1b[37m]\x1b[32m "); \
   LOG_FINF; \
   fprintf(stderr, body); \
   fprintf(stderr, "\x1b[0m\n"); \
)

/* Log some information. */
#define LOG_INFF(fmt, ...) HIDE( \
   fprintf(stderr, "\x1b[37m[\x1b[94;1m==\x1b[0m\x1b[37m]\x1b[0m "); \
   fprintf(stderr, fmt, ##__VA_ARGS__); \
   fprintf(stderr, "\x1b[0m\n"); \
)

#define LOG_INF(body) HIDE( \
   fprintf(stderr, "\x1b[37m[\x1b[94;1m==\x1b[0m\x1b[37m]\x1b[0m "); \
   fprintf(stderr, body); \
   fprintf(stderr, "\x1b[0m\n"); \
)

/* Log something with no formatting. */
#define LOG_RAWF(fmt, ...) HIDE( \
   fprintf(stderr, fmt, ##__VA_ARGS__); \
)

#define LOG_RAW(body) HIDE( \
   fprintf(stderr, body); \
)

/* Log a warning. */
#define LOG_WARF(fmt, ...) HIDE( \
   fprintf(stderr, "\x1b[37m[\x1b[93;1m==\x1b[0m\x1b[37m]\x1b[93;1m WARNING:\x1b[0m\x1b[33m "); \
   LOG_FINF; \
   fprintf(stderr, fmt, ##__VA_ARGS__); \
   fprintf(stderr, "\x1b[0m\n"); \
)

#define LOG_WAR(body) HIDE( \
   fprintf(stderr, "\x1b[37m[\x1b[93;1m==\x1b[0m\x1b[37m]\x1b[93;1m WARNING:\x1b[0m\x1b[33m "); \
   LOG_FINF; \
   fprintf(stderr, body); \
   fprintf(stderr, "\x1b[0m\n"); \
)

/* Log an error. */
#define LOG_ERRF(fmt, ...) HIDE( \
   fprintf(stderr, "\x1b[37m[\x1b[91;1m==\x1b[0m\x1b[37m]\x1b[91;1m ERROR:\x1b[0m\x1b[31m "); \
   LOG_FINF; \
   fprintf(stderr, fmt, ##__VA_ARGS__); \
   fprintf(stderr, "\x1b[0m\n"); \
)

#define LOG_ERR(body) HIDE( \
   fprintf(stderr, "\x1b[37m[\x1b[91;1m==\x1b[0m\x1b[37m]\x1b[91;1m ERROR:\x1b[0m\x1b[31m "); \
   LOG_FINF; \
   fprintf(stderr, body); \
   fprintf(stderr, "\x1b[0m\n"); \
)

/* Die and leave message. */
void die(const char*, ...);
#define DIEF(fmt, ...) HIDE( \
   fprintf(stderr, "\x1b[37m[\x1b[91;1m==\x1b[0m\x1b[37m]\x1b[91;1m CAUSE OF DEATH:\x1b[0m\x1b[31m "); \
   fprintf(stderr, fmt, ##__VA_ARGS__); \
   fprintf(stderr, "\x1b[0m\n"); \
   fprintf(stderr, "\x1b[37m[\x1b[91;1m==\x1b[0m\x1b[37m]\x1b[91;1m Exiting...\x1b[0m\n"); \
   exit(1); \
)

#define DIE(body) HIDE( \
   fprintf(stderr, "\x1b[37m[\x1b[91;1m==\x1b[0m\x1b[37m]\x1b[91;1m CAUSE OF DEATH:\x1b[0m\x1b[31m "); \
   fprintf(stderr, body); \
   fprintf(stderr, "\x1b[0m\n"); \
   fprintf(stderr, "\x1b[37m[\x1b[91;1m==\x1b[0m\x1b[37m]\x1b[91;1m Exiting...\x1b[0m\n"); \
   exit(1); \
)

/* If `calloc()` returns null, die. */
void* ecalloc(size_t, size_t);
/* If `malloc()` returns null, die. */
void* emalloc(size_t);
/* If `realloc()` returns null, die. */
void* erealloc(void*, size_t);

#endif