Commit | Line | Data |
---|---|---|
98fa08a5 | 1 | /* SPDX-License-Identifier: LGPL-2.1-or-later */ |
d16005f8 | 2 | /* |
98fa08a5 | 3 | * Copyright (C) 2014-2021 Paul Cercueil <paul@crapouillou.net> |
d16005f8 PC |
4 | */ |
5 | ||
6 | #ifndef DEBUG_H | |
7 | #define DEBUG_H | |
8 | ||
9 | #include <stdio.h> | |
10 | #include <unistd.h> | |
11 | ||
12 | #define NOLOG_L 0 | |
13 | #define ERROR_L 1 | |
14 | #define WARNING_L 2 | |
15 | #define INFO_L 3 | |
16 | #define DEBUG_L 4 | |
17 | ||
18 | #ifndef LOG_LEVEL | |
19 | #define LOG_LEVEL INFO_L | |
20 | #endif | |
21 | ||
22 | // ------------- | |
23 | ||
24 | #ifndef COLOR_DEBUG | |
25 | #define COLOR_DEBUG "\e[0;32m" | |
26 | #endif | |
27 | #ifndef COLOR_WARNING | |
28 | #define COLOR_WARNING "\e[01;35m" | |
29 | #endif | |
30 | #ifndef COLOR_ERROR | |
31 | #define COLOR_ERROR "\e[01;31m" | |
32 | #endif | |
33 | ||
34 | #define COLOR_END "\e[0m" | |
35 | ||
36 | #if (LOG_LEVEL >= DEBUG_L) | |
37 | # ifdef COLOR_DEBUG | |
38 | # define pr_debug(str, ...) do { \ | |
39 | if (isatty(STDOUT_FILENO)) \ | |
40 | fprintf(stdout, COLOR_DEBUG "DEBUG: " str COLOR_END, \ | |
41 | ##__VA_ARGS__); \ | |
42 | else \ | |
43 | fprintf(stdout, "DEBUG: " str, ##__VA_ARGS__); \ | |
44 | } while (0) | |
45 | # else | |
46 | # define pr_debug(...) \ | |
47 | fprintf(stdout, "DEBUG: " __VA_ARGS__) | |
48 | # endif | |
49 | #else | |
50 | #define pr_debug(...) | |
51 | #endif | |
52 | ||
53 | #if (LOG_LEVEL >= INFO_L) | |
54 | # ifdef COLOR_INFO | |
55 | # define pr_info(str, ...) \ | |
56 | fprintf(stdout, COLOR_INFO str COLOR_END, ##__VA_ARGS__) | |
57 | # else | |
58 | # define pr_info(...) \ | |
59 | fprintf(stdout, __VA_ARGS__) | |
60 | # endif | |
61 | #else | |
62 | #define pr_info(...) | |
63 | #endif | |
64 | ||
65 | #if (LOG_LEVEL >= WARNING_L) | |
66 | # ifdef COLOR_WARNING | |
67 | # define pr_warn(str, ...) do { \ | |
68 | if (isatty(STDERR_FILENO)) \ | |
69 | fprintf(stderr, COLOR_WARNING "WARNING: " str COLOR_END,\ | |
70 | ##__VA_ARGS__); \ | |
71 | else \ | |
72 | fprintf(stderr, "WARNING: " str, ##__VA_ARGS__); \ | |
73 | } while (0) | |
74 | # else | |
75 | # define pr_warn(...) \ | |
76 | fprintf(stderr, "WARNING: " __VA_ARGS__) | |
77 | # endif | |
78 | #else | |
79 | #define pr_warn(...) | |
80 | #endif | |
81 | ||
82 | #if (LOG_LEVEL >= ERROR_L) | |
83 | # ifdef COLOR_ERROR | |
84 | # define pr_err(str, ...) do { \ | |
85 | if (isatty(STDERR_FILENO)) \ | |
86 | fprintf(stderr, COLOR_ERROR "ERROR: " str COLOR_END, \ | |
87 | ##__VA_ARGS__); \ | |
88 | else \ | |
89 | fprintf(stderr, "ERROR: " str, ##__VA_ARGS__); \ | |
90 | } while (0) | |
91 | # else | |
92 | # define pr_err(...) \ | |
93 | fprintf(stderr, "ERROR: " __VA_ARGS__) | |
94 | # endif | |
95 | #else | |
96 | #define pr_err(...) | |
97 | #endif | |
98 | ||
99 | #endif |