aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/src/log.h
blob: 6c0c5178a3c240e075abc42f13f63fd2597ff5a8 (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
/*
 * Copyright (c) 2022 Egor Tensin <egor@tensin.name>
 * This file is part of the "cimple" project.
 * For details, see https://github.com/egor-tensin/cimple.
 * Distributed under the MIT License.
 */

#ifndef __LOG_H__
#define __LOG_H__

#include "compiler.h"

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

#define LOG_LVL_DEBUG 0
#define LOG_LVL_INFO  1
#define LOG_LVL_ERR   2

extern int g_log_lvl;

int log_entry_start(int lvl, FILE* dest);
void log_entry_end(FILE* dest);

#define log_debug(...)                                       \
	do {                                                 \
		if (!log_entry_start(LOG_LVL_DEBUG, stdout)) \
			break;                               \
		fprintf(stdout, __VA_ARGS__);                \
		log_entry_end(stdout);                       \
	} while (0)

#define log(...)                                            \
	do {                                                \
		if (!log_entry_start(LOG_LVL_INFO, stdout)) \
			break;                              \
		fprintf(stdout, __VA_ARGS__);               \
		log_entry_end(stdout);                      \
	} while (0)

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

#define log_err(...)                                       \
	do {                                               \
		if (!log_entry_start(LOG_LVL_ERR, stderr)) \
			break;                             \
		_log_err_file_loc();                       \
		fprintf(stderr, __VA_ARGS__);              \
		log_entry_end(stderr);                     \
	} while (0)

#define log_errno(fn)                                      \
	do {                                               \
		if (!log_entry_start(LOG_LVL_ERR, stderr)) \
			break;                             \
		_log_err_file_loc();                       \
		perror(fn);                                \
		log_entry_end(stderr);                     \
	} while (0)

#define log_errno_if(expr, fn)                    \
	do {                                      \
		int CONCAT(ret, __LINE__) = expr; \
		if (CONCAT(ret, __LINE__) < 0)    \
			log_errno(fn);            \
	} while (0)

#define pthread_errno(var, fn) \
	do {                   \
		errno = var;   \
		log_errno(fn); \
		var = -var;    \
	} while (0)

#define pthread_errno_if(expr, fn)                                \
	do {                                                      \
		int CONCAT(ret, __LINE__) = expr;                 \
		if (CONCAT(ret, __LINE__))                        \
			pthread_errno(CONCAT(ret, __LINE__), fn); \
	} while (0)

#endif