Workbench Library  0.1b
workbench_logger.h
Go to the documentation of this file.
1 
30 #pragma once
31 
32 #include <stdio.h>
33 #include <string.h>
34 #include <unistd.h>
35 
42 #define __FILENAME__ \
43  (strrchr(__FILE__, '/') ? strrchr(__FILE__, '/') + 1 : __FILE__)
49 #ifndef LOG_LEVEL
61 #define LOG_LEVEL 4
62 #endif
63 
67 #ifndef PRINT_FUNC
68 #define PRINT_FUNC (LOG_LEVEL > 3)
69 #endif
70 
75 #ifndef PRINT_FILE
76 #define PRINT_FILE (LOG_LEVEL > 2)
77 #endif
111 #define COLOR(text, color, style) "\033[" #style ";" #color "m" text "\033[0m"
112 
113 #define RED(text) COLOR(text, 31, 0)
114 #define GRN(text) COLOR(text, 32, 0)
115 #define YLW(text) COLOR(text, 33, 0)
116 #define PRL(text) COLOR(text, 35, 0)
117 #define CYN(text) COLOR(text, 36, 0)
129 #define log_d(message, ...) \
130  if (get_log_level() > 3) \
131  log_format(__stdoutp, CYN("dbg"), message __VA_OPT__(, ) __VA_ARGS__);
135 #define log_i(message, ...) \
136  if (get_log_level() > 2) \
137  log_format(__stdoutp, GRN("inf"), message __VA_OPT__(, ) __VA_ARGS__);
142 #define log_w(message, ...) \
143  if (get_log_level() > 1) \
144  log_format(__stderrp, YLW("wrn"), YLW(message) __VA_OPT__(, ) __VA_ARGS__);
148 #define log_e(message, ...) \
149  if (get_log_level() > 0) \
150  log_format(__stderrp, RED("err"), RED(message) __VA_OPT__(, ) __VA_ARGS__);
153 #if PRINT_FUNC == 1
154 #define _fun_print_tmpl "%s "
155 #define _fun_print_arg , __func__
156 #else
157 #define _fun_print_tmpl ""
158 #define _fun_print_arg
159 #endif
160 #if PRINT_FILE == 1
161 #define _file_print_tmpl "(%s:%d) "
162 #define _file_print_arg , __FILENAME__, __LINE__
163 #else
164 #define _file_print_tmpl ""
165 #define _file_print_arg
166 #endif
167 
194 #define log_format(file, tag, message, ...) \
195  fprintf(file, "[%s] " _fun_print_tmpl _file_print_tmpl message "\n", \
196  tag _fun_print_arg _file_print_arg __VA_OPT__(, ) __VA_ARGS__);