aboutsummaryrefslogtreecommitdiff
path: root/deps/lightrec/debug.h
blob: 4facc221e108108a169d180948ae4660bd6af1a1 (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
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
/*
 * Copyright (C) 2014-2020 Paul Cercueil <paul@crapouillou.net>
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 */

#ifndef DEBUG_H
#define DEBUG_H

#include <stdio.h>
#include <unistd.h>

#define NOLOG_L 0
#define ERROR_L 1
#define WARNING_L 2
#define INFO_L 3
#define DEBUG_L 4

#ifndef LOG_LEVEL
#define LOG_LEVEL INFO_L
#endif

// -------------

#ifndef COLOR_DEBUG
#define COLOR_DEBUG   "\e[0;32m"
#endif
#ifndef COLOR_WARNING
#define COLOR_WARNING "\e[01;35m"
#endif
#ifndef COLOR_ERROR
#define COLOR_ERROR   "\e[01;31m"
#endif

#define COLOR_END "\e[0m"

#if (LOG_LEVEL >= DEBUG_L)
# ifdef COLOR_DEBUG
#  define pr_debug(str, ...) do {					\
	if (isatty(STDOUT_FILENO))					\
		fprintf(stdout, COLOR_DEBUG "DEBUG: " str COLOR_END,	\
			##__VA_ARGS__);					\
	else								\
		fprintf(stdout, "DEBUG: " str, ##__VA_ARGS__);		\
	} while (0)
# else
#  define pr_debug(...) \
    fprintf(stdout, "DEBUG: " __VA_ARGS__)
# endif
#else
#define pr_debug(...)
#endif

#if (LOG_LEVEL >= INFO_L)
# ifdef COLOR_INFO
#  define pr_info(str, ...) \
    fprintf(stdout, COLOR_INFO str COLOR_END, ##__VA_ARGS__)
# else
#  define pr_info(...) \
    fprintf(stdout, __VA_ARGS__)
# endif
#else
#define pr_info(...)
#endif

#if (LOG_LEVEL >= WARNING_L)
# ifdef COLOR_WARNING
#  define pr_warn(str, ...) do {					\
	if (isatty(STDERR_FILENO))					\
		fprintf(stderr, COLOR_WARNING "WARNING: " str COLOR_END,\
			##__VA_ARGS__);					\
	else								\
		fprintf(stderr, "WARNING: " str, ##__VA_ARGS__);	\
	} while (0)
# else
#  define pr_warn(...) \
    fprintf(stderr, "WARNING: " __VA_ARGS__)
# endif
#else
#define pr_warn(...)
#endif

#if (LOG_LEVEL >= ERROR_L)
# ifdef COLOR_ERROR
#  define pr_err(str, ...) do {						\
	if (isatty(STDERR_FILENO))					\
		fprintf(stderr, COLOR_ERROR "ERROR: " str COLOR_END,	\
			##__VA_ARGS__);					\
	else								\
		fprintf(stderr, "ERROR: " str, ##__VA_ARGS__);		\
	} while (0)
# else
#  define pr_err(...) \
    fprintf(stderr, "ERROR: " __VA_ARGS__)
# endif
#else
#define pr_err(...)
#endif

#endif