2525#ifndef _DEBUG_H
2626#define _DEBUG_H
2727
28- # include < stdio.h >
29-
30- enum {
31- ERR ,
32- INFO ,
33- DBG ,
34- TRACE ,
28+ enum log_level {
29+ LOG_LEVEL_NONE,
30+ LOG_LEVEL_ERROR,
31+ LOG_LEVEL_WARNING ,
32+ LOG_LEVEL_INFO ,
33+ LOG_LEVEL_DEBUG ,
34+ LOG_LEVEL_TRACE ,
3535};
3636
37- /* *
38- * Set it to 0 or higher with 0 being lowest number of messages
39- * 0 = Only errors show up
40- * 1 = Errors + Info messages
41- * 2 = Errors + Info messages + Debug messages
42- * 3 = Errors + Info messages + Debug messages + Trace calls
43- */
44- #ifdef DEBUGON
45- static int dbg_lvl = DBG;
46- #else
47- static int dbg_lvl = INFO;
48- #endif
37+ extern log_level dbg_lvl;
4938
50- #define _PRINT (prefix, fmt, ...) \
51- if (1 ) {\
52- printf (" %s" , prefix); \
53- printf (fmt, ##__VA_ARGS__); \
54- fflush (stdout); \
55- }
39+ void set_log_mode (const char * mode);
40+ void set_log_level (log_level level);
41+ void set_log_level (const char * log_level);
42+ void log_message (log_level level, const char * format, ...);
5643
57- #define PRINT (fmt, ...) _PRINT(" " , fmt, ##__VA_ARGS__)
58- #define ERR (fmt, ...) _PRINT(" [Error] " , fmt, ##__VA_ARGS__)
59- #define WARNING (fmt, ...) _PRINT(" [Warning] " , fmt, ##__VA_ARGS__)
60- #define DBG (fmt, ...) if (dbg_lvl >= DBG) _PRINT(" [DBG] " , fmt, ##__VA_ARGS__)
61- #define INFO (fmt, ...) if (dbg_lvl >= INFO) _PRINT(" [Info] " , fmt, ##__VA_ARGS__)
62- #define TRACE (fmt, ...) if (dbg_lvl >= TRACE) PRINT(fmt, ##__VA_ARGS__)
44+ #define PRINT (format, ...) \
45+ log_message (LOG_LEVEL_NONE, format, ##__VA_ARGS__)
46+
47+ #define ERR (format, ...) \
48+ log_message (LOG_LEVEL_ERROR, format, ##__VA_ARGS__)
49+
50+ #define WARNING (format, ...) \
51+ log_message (LOG_LEVEL_WARNING, format, ##__VA_ARGS__)
52+
53+ #define INFO (format, ...) \
54+ log_message (LOG_LEVEL_INFO, format, ##__VA_ARGS__)
55+
56+ #define DBG (format, ...) \
57+ log_message (LOG_LEVEL_DEBUG, format, ##__VA_ARGS__)
58+
59+ #define TRACE (format, ...) \
60+ log_message (LOG_LEVEL_TRACE, format, ##__VA_ARGS__)
6361
64- #ifdef __cplusplus
65- #define TRACING () tracer trace (__FUNCTION__);
66- #else
67- #define TRACING ()
68- #endif
6962
7063#ifdef __cplusplus
7164class tracer {
@@ -81,14 +74,10 @@ class tracer {
8174};
8275#endif
8376
84- inline int set_dbg_lvl (int lvl)
85- {
86- int ret = 1 ;
87- if (lvl >= ERR && lvl <= TRACE) {
88- dbg_lvl = lvl;
89- ret = 0 ;
90- }
91- return ret;
92- }
93-
77+ #ifdef __cplusplus
78+ #define TRACING () tracer trace (__FUNCTION__);
79+ #else
80+ #define TRACING ()
9481#endif
82+
83+ #endif // _DEBUG_H
0 commit comments