aboutsummaryrefslogtreecommitdiffstats
path: root/src/err.h
blob: 256c5a9f63810e594a89b1b3e51661cb2eba41ef (plain)
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
#ifndef CONFCONF_ERR_H
#define CONFCONF_ERR_H

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

#define ERR(...) \
	do { \
		fprintf(stderr, "\x1B[1;31merror:\x1B[0m " __VA_ARGS__); \
		fprintf(stderr, "\n"); \
		exit(EXIT_FAILURE); \
	} while (0)

#define WARN(...) \
	do { \
		fprintf(stderr, "\x1B[1;35mwarning:\x1B[0m " __VA_ARGS__); \
		fprintf(stderr, "\n"); \
	} while (0)

#define TRY(cond, ...) \
	do { \
		if (!(cond)) { \
			ERR(__VA_ARGS__); \
		} \
	} while (0)

#define TRYALLOC(dest, count) \
	do { \
		(dest) = malloc((count) * sizeof(*(dest))); \
		TRY((dest) != NULL, "could not allocate memory"); \
	} while (0)

#define TRYREALLOC(dest, count) \
	do { \
		(dest) = realloc((dest), (count) * sizeof(*(dest))); \
		TRY((dest) != NULL, "could not allocate memory"); \
	} while (0)

#endif