James Kuszmaul | 82f6c04 | 2021-01-17 11:30:16 -0800 | [diff] [blame^] | 1 | /** |
| 2 | * @file re_dbg.h Interface to debugging module |
| 3 | * |
| 4 | * Copyright (C) 2010 Creytiv.com |
| 5 | */ |
| 6 | |
| 7 | #ifdef __cplusplus |
| 8 | extern "C" { |
| 9 | #endif |
| 10 | |
| 11 | /** Debug levels */ |
| 12 | enum { |
| 13 | DBG_EMERG = 0, /**< System is unusable */ |
| 14 | DBG_ALERT = 1, /**< Action must be taken immediately */ |
| 15 | DBG_CRIT = 2, /**< Critical conditions */ |
| 16 | DBG_ERR = 3, /**< Error conditions */ |
| 17 | DBG_WARNING = 4, /**< Warning conditions */ |
| 18 | DBG_NOTICE = 5, /**< Normal but significant condition */ |
| 19 | DBG_INFO = 6, /**< Informational */ |
| 20 | DBG_DEBUG = 7 /**< Debug-level messages */ |
| 21 | }; |
| 22 | |
| 23 | |
| 24 | /** |
| 25 | * @def DEBUG_MODULE |
| 26 | * |
| 27 | * Module name defined by application |
| 28 | */ |
| 29 | |
| 30 | /** |
| 31 | * @def DEBUG_LEVEL |
| 32 | * |
| 33 | * Debug level defined by application |
| 34 | */ |
| 35 | |
| 36 | #ifndef DEBUG_MODULE |
| 37 | # warning "DEBUG_MODULE is not defined" |
| 38 | #define DEBUG_MODULE "?" |
| 39 | #endif |
| 40 | |
| 41 | #ifndef DEBUG_LEVEL |
| 42 | # warning "DEBUG_LEVEL is not defined" |
| 43 | #define DEBUG_LEVEL 7 |
| 44 | #endif |
| 45 | |
| 46 | |
| 47 | /** |
| 48 | * @def DEBUG_WARNING(...) |
| 49 | * |
| 50 | * Print warning message |
| 51 | */ |
| 52 | |
| 53 | /** |
| 54 | * @def DEBUG_NOTICE(...) |
| 55 | * |
| 56 | * Print notice message |
| 57 | */ |
| 58 | |
| 59 | /** |
| 60 | * @def DEBUG_INFO(...) |
| 61 | * |
| 62 | * Print info message |
| 63 | */ |
| 64 | |
| 65 | /** |
| 66 | * @def DEBUG_PRINTF(...) |
| 67 | * |
| 68 | * Print debug message |
| 69 | */ |
| 70 | |
| 71 | |
| 72 | /* Check for ISO C99 variable argument macros */ |
| 73 | #if (defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L) \ |
| 74 | || (__GNUC__ >= 3) |
| 75 | |
| 76 | #if (DEBUG_LEVEL >= 4) |
| 77 | #define DEBUG_WARNING(...) \ |
| 78 | dbg_printf(DBG_WARNING, DEBUG_MODULE ": " __VA_ARGS__) |
| 79 | #else |
| 80 | #define DEBUG_WARNING(...) |
| 81 | #endif |
| 82 | |
| 83 | #if (DEBUG_LEVEL >= 5) |
| 84 | #define DEBUG_NOTICE(...) \ |
| 85 | dbg_printf(DBG_NOTICE, DEBUG_MODULE ": " __VA_ARGS__) |
| 86 | #else |
| 87 | #define DEBUG_NOTICE(...) |
| 88 | #endif |
| 89 | |
| 90 | #if (DEBUG_LEVEL >= 6) |
| 91 | #define DEBUG_INFO(...) \ |
| 92 | dbg_printf(DBG_INFO, DEBUG_MODULE ": " __VA_ARGS__) |
| 93 | #else |
| 94 | #define DEBUG_INFO(...) |
| 95 | #endif |
| 96 | |
| 97 | #if (DEBUG_LEVEL >= 7) |
| 98 | #define DEBUG_PRINTF(...) \ |
| 99 | dbg_printf(DBG_DEBUG, DEBUG_MODULE ": " __VA_ARGS__) |
| 100 | #else |
| 101 | #define DEBUG_PRINTF(...) |
| 102 | #endif |
| 103 | |
| 104 | /* GNU extensions for variable argument macros */ |
| 105 | #elif defined(__GNUC__) |
| 106 | |
| 107 | #if (DEBUG_LEVEL >= 4) |
| 108 | #define DEBUG_WARNING(a...) dbg_printf(DBG_WARNING, DEBUG_MODULE ": " a) |
| 109 | #else |
| 110 | #define DEBUG_WARNING(a...) |
| 111 | #endif |
| 112 | |
| 113 | #if (DEBUG_LEVEL >= 5) |
| 114 | #define DEBUG_NOTICE(a...) dbg_printf(DBG_NOTICE, DEBUG_MODULE ": " a) |
| 115 | #else |
| 116 | #define DEBUG_NOTICE(a...) |
| 117 | #endif |
| 118 | |
| 119 | #if (DEBUG_LEVEL >= 6) |
| 120 | #define DEBUG_INFO(a...) dbg_printf(DBG_INFO, DEBUG_MODULE ": " a) |
| 121 | #else |
| 122 | #define DEBUG_INFO(a...) |
| 123 | #endif |
| 124 | |
| 125 | #if (DEBUG_LEVEL >= 7) |
| 126 | #define DEBUG_PRINTF(a...) dbg_printf(DBG_DEBUG, DEBUG_MODULE ": " a) |
| 127 | #else |
| 128 | #define DEBUG_PRINTF(a...) |
| 129 | #endif |
| 130 | |
| 131 | /* No variable argument macros */ |
| 132 | #else |
| 133 | |
| 134 | #if (DEBUG_LEVEL >= 4) |
| 135 | #define DEBUG_WARNING dbg_warning |
| 136 | #else |
| 137 | #define DEBUG_WARNING dbg_noprintf |
| 138 | #endif |
| 139 | |
| 140 | #if (DEBUG_LEVEL >= 5) |
| 141 | #define DEBUG_NOTICE dbg_notice |
| 142 | #else |
| 143 | #define DEBUG_NOTICE dbg_noprintf |
| 144 | #endif |
| 145 | |
| 146 | #if (DEBUG_LEVEL >= 6) |
| 147 | #define DEBUG_INFO dbg_info |
| 148 | #else |
| 149 | #define DEBUG_INFO dbg_noprintf |
| 150 | #endif |
| 151 | |
| 152 | #if (DEBUG_LEVEL >= 7) |
| 153 | #define DEBUG_PRINTF dbg_noprintf |
| 154 | #else |
| 155 | #define DEBUG_PRINTF dbg_noprintf |
| 156 | #endif |
| 157 | |
| 158 | #endif |
| 159 | |
| 160 | |
| 161 | /** Debug flags */ |
| 162 | enum dbg_flags { |
| 163 | DBG_NONE = 0, /**< No debug flags */ |
| 164 | DBG_TIME = 1<<0, /**< Print timestamp flag */ |
| 165 | DBG_ANSI = 1<<1, /**< Print ANSI color codes */ |
| 166 | DBG_ALL = DBG_TIME|DBG_ANSI /**< All flags enabled */ |
| 167 | }; |
| 168 | |
| 169 | |
| 170 | /** |
| 171 | * Defines the debug print handler |
| 172 | * |
| 173 | * @param level Debug level |
| 174 | * @param p Debug string |
| 175 | * @param len String length |
| 176 | * @param arg Handler argument |
| 177 | */ |
| 178 | typedef void (dbg_print_h)(int level, const char *p, size_t len, void *arg); |
| 179 | |
| 180 | void dbg_init(int level, enum dbg_flags flags); |
| 181 | void dbg_close(void); |
| 182 | int dbg_logfile_set(const char *name); |
| 183 | void dbg_handler_set(dbg_print_h *ph, void *arg); |
| 184 | void dbg_printf(int level, const char *fmt, ...); |
| 185 | void dbg_noprintf(const char *fmt, ...); |
| 186 | void dbg_warning(const char *fmt, ...); |
| 187 | void dbg_notice(const char *fmt, ...); |
| 188 | void dbg_info(const char *fmt, ...); |
| 189 | const char *dbg_level_str(int level); |
| 190 | |
| 191 | #ifdef __cplusplus |
| 192 | } |
| 193 | #endif |