Debian Codegraph @

src/utils/logger.c

raw
1 #include "utils/logger.h"
2 #include "utils/color.h"
3 #include <stdarg.h>
4 #include <stdbool.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7
8 #define LOG_CYAN(str) ((log_color_enable) ? (CYANCLR str CLR) : str)
9 #define LOG_BLUE(str) ((log_color_enable) ? (BLUECLR str CLR) : str)
10 #define LOG_YELLOW(str) ((log_color_enable) ? (YELLOWCLR str CLR) : str)
11 #define LOG_RED(str) ((log_color_enable) ? (REDCLR str CLR) : str)
12
13 #define DEBUG_PREFIX LOG_CYAN ("[DEBUG]: ")
14 #define INFO LOG_BLUE ("[INFO]: ")
15 #define WARN LOG_YELLOW ("[WARN]: ")
16 #define ERR LOG_RED ("[ERROR]: ")
17
18 #ifdef DEBUG
19 void
20 debug_print (const char *caller_func, const char *fmt, ...)
21 {
22 va_list args;
23 va_start (args, fmt);
24 fprintf (stderr, DEBUG_PREFIX);
25
26 fprintf (stderr, "in %s: ", caller_func);
27
28 vfprintf (stderr, fmt, args);
29 putc ('\n', stderr);
30 va_end (args);
31 fflush (stderr);
32 }
33 #endif
34
35 void
36 info_print (const char *caller_func, const char *fmt, ...)
37 {
38 va_list args;
39 va_start (args, fmt);
40 fprintf (stderr, INFO);
41
42 #ifdef DEBUG
43 fprintf (stderr, "in %s: ", caller_func);
44 #else
45 (void)caller_func;
46 #endif
47
48 vfprintf (stderr, fmt, args);
49 putc ('\n', stderr);
50 va_end (args);
51 fflush (stderr);
52 }
53
54 void
55 warn_print (const char *caller_func, const char *fmt, ...)
56 {
57 va_list args;
58 va_start (args, fmt);
59 fprintf (stderr, WARN);
60
61 #ifdef DEBUG
62 fprintf (stderr, "in %s: ", caller_func);
63 #else
64 (void)caller_func;
65 #endif
66
67 vfprintf (stderr, fmt, args);
68 putc ('\n', stderr);
69 va_end (args);
70 fflush (stderr);
71 }
72
73 void
74 error_print (const char *caller_func, const char *fmt, ...)
75 {
76 va_list args;
77 va_start (args, fmt);
78 fprintf (stderr, ERR);
79
80 #ifdef DEBUG
81 fprintf (stderr, "in %s: ", caller_func);
82 #else
83 (void)caller_func;
84 #endif
85
86 vfprintf (stderr, fmt, args);
87 putc ('\n', stderr);
88 va_end (args);
89 exit (1);
90 }