aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/log.h
blob: 72a63c40fb53fe7297bb3061e858fa2bbee1a0dd (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
#ifndef __LOG_H__
#define __LOG_H__

#include <errno.h>
#include <libgen.h>
#include <netdb.h>
#include <stdio.h>

#define CONCAT_INNER(a, b) a##b
#define CONCAT(a, b) CONCAT_INNER(a, b)

#define print_errno(s)                                                                             \
	do {                                                                                       \
		fprintf(stderr, "%s(%d): ", basename(__FILE__), __LINE__);                         \
		perror(s);                                                                         \
	} while (0)

#define check_errno(expr, s)                                                                       \
	do {                                                                                       \
		int CONCAT(ret, __LINE__) = expr;                                                  \
		if (CONCAT(ret, __LINE__) < 0)                                                     \
			print_errno(s);                                                            \
	} while (0)

#define pthread_print_errno(var, s)                                                                \
	do {                                                                                       \
		errno = var;                                                                       \
		print_errno(s);                                                                    \
		var = -var;                                                                        \
	} while (0)

#define pthread_check(expr, s)                                                                     \
	do {                                                                                       \
		int CONCAT(ret, __LINE__) = expr;                                                  \
		if (CONCAT(ret, __LINE__))                                                         \
			pthread_print_errno(CONCAT(ret, __LINE__), s);                             \
	} while (0)

#define print_error(...)                                                                           \
	do {                                                                                       \
		fprintf(stderr, "%s(%d): ", basename(__FILE__), __LINE__);                         \
		fprintf(stderr, __VA_ARGS__);                                                      \
	} while (0)

#define print_log(...) printf(__VA_ARGS__)

#endif