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

#include <errno.h>
#include <libgen.h>
#include <netdb.h>
#include <stdio.h>
#include <sys/time.h>
#include <time.h>

static inline void log_print_timestamp(FILE *dest)
{
	struct timeval tv;
	struct tm tm;
	char buf[64];
	size_t used = 0;

	if (gettimeofday(&tv, NULL) < 0)
		return;
	if (!gmtime_r(&tv.tv_sec, &tm))
		return;

	buf[0] = '\0';
	used += strftime(buf + used, sizeof(buf) - used, "[%F %T", &tm);
	used += snprintf(buf + used, sizeof(buf) - used, ".%03ld] ", tv.tv_usec / 1000);
	fprintf(dest, "%s", buf);
}

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

static inline void log_prefix(FILE *dest)
{
	log_print_timestamp(dest);
}

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

#define print_errno(s)                                                                             \
	do {                                                                                       \
		log_error_prefix();                                                                \
		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 {                                                                                       \
		log_error_prefix();                                                                \
		fprintf(stderr, __VA_ARGS__);                                                      \
	} while (0)

#define print_log(...)                                                                             \
	do {                                                                                       \
		log_prefix(stdout);                                                                \
		printf(__VA_ARGS__);                                                               \
	} while (0)

#endif