1 /** @file sanei_debug.h 2 * Support for printing debug messages. 3 * 4 * Use the functions of this header file to print debug or warning messages. 5 */ 6 7 #ifndef _SANEI_DEBUG_H 8 #define _SANEI_DEBUG_H 9 10 #include <sane/sanei.h> 11 12 #include "hilog/log.h" 13 14 #ifdef __cplusplus 15 extern "C" { 16 #endif 17 18 /** @name Public macros 19 * These macros can be used in backends and other SANE-related 20 * code. 21 * 22 * Before including sanei_debug.h, the following macros must be set: 23 * 24 * - BACKEND_NAME - The name of your backend without double-quotes (must be set in any case) 25 * - STUBS - If this is defined, no macros will be included. Used in 26 * backends consisting of more than one .c file. 27 * - DEBUG_DECLARE_ONLY - Generates prototypes instead of functions. Used in 28 * backends consisting of more than one .c file. 29 * - DEBUG_NOT_STATIC - Doesn't generate static functions. Used in header files if 30 * they are include in more than one .c file. 31 * 32 * @{ 33 */ 34 35 /** @def DBG_INIT() 36 * Initialize sanei_debug. 37 * 38 * Call this function before you use any DBG function. 39 */ 40 41 /** @def DBG(level, fmt, ...) 42 * Print a message at debug level `level' or higher using a printf-like 43 * function. Example: DBG(1, "sane_open: opening fd \%d\\n", fd). 44 * 45 * @param level debug level 46 * @param fmt format (see man 3 printf for details) 47 * @param ... additional arguments 48 */ 49 50 /** @def IF_DBG(x) 51 * Compile code only if debugging is enabled. 52 * 53 * Expands to x if debug support is enabled at compile-time. If NDEBUG is 54 * defined at compile-time this macro expands to nothing. 55 * 56 * @param x code to expand when debugging is enabled 57 */ 58 59 /** 60 * @def DBG_LEVEL 61 * Current debug level. 62 * 63 * You can only read this "variable". 64 */ 65 66 /** @def ENTRY(name) 67 * Expands to sane_BACKEND_NAME_name. 68 * 69 * Example: ENTRY(init) in mustek.c will expand to sane_mustek_init. 70 */ 71 72 /* @} */ 73 74 75 /** @hideinitializer*/ 76 #define ENTRY(name) PASTE(PASTE(PASTE(sane_,BACKEND_NAME),_),name) 77 78 #ifdef NDEBUG 79 80 extern void sanei_debug_ndebug (int level, const char *msg, ...); 81 82 # define DBG_LEVEL (0) 83 # define DBG_INIT() 84 #ifndef ENABLE_HILOG 85 # define DBG sanei_debug_ndebug 86 #else 87 # define DBG(level, ...) ((void)HiLogPrint(LOG_APP, LOG_INFO, 0, "sanekit", __VA_ARGS__)) 88 #endif 89 # define IF_DBG(x) 90 91 #else /* !NDEBUG */ 92 93 /** @hideinitializer*/ 94 # define DBG_LEVEL PASTE(sanei_debug_,BACKEND_NAME) 95 96 # if defined(BACKEND_NAME) && !defined(STUBS) 97 # ifdef DEBUG_DECLARE_ONLY 98 extern int DBG_LEVEL; 99 # else /* !DEBUG_DECLARE_ONLY */ 100 int DBG_LEVEL = 0; 101 # endif /* DEBUG_DECLARE_ONLY */ 102 # endif /* BACKEND_NAME && !STUBS */ 103 104 /** @hideinitializer*/ 105 # define DBG_INIT() \ 106 sanei_init_debug (STRINGIFY(BACKEND_NAME), &DBG_LEVEL) 107 108 /** @hideinitializer*/ 109 # define DBG_LOCAL PASTE(DBG_LEVEL,_call) 110 111 112 # ifndef STUBS 113 114 # ifdef DEBUG_DECLARE_ONLY 115 116 extern void DBG_LOCAL (int level, const char *msg, ...) 117 #ifdef __GNUC__ 118 __attribute__ ((format (printf, 2, 3))) 119 #endif 120 ; 121 122 # else /* !DEBUG_DECLARE_ONLY */ 123 124 # include <stdarg.h> 125 126 extern void sanei_debug_msg 127 (int level, int max_level, const char *be, const char *fmt, va_list ap); 128 129 #ifdef __GNUC__ 130 # ifndef DEBUG_NOT_STATIC 131 static 132 # endif /* !DEBUG_NOT_STATIC */ 133 void DBG_LOCAL (int level, const char *msg, ...) __attribute__ ((format (printf, 2, 3))); 134 #endif /* __GNUC__ */ 135 136 # ifndef DEBUG_NOT_STATIC 137 static 138 # endif /* !DEBUG_NOT_STATIC */ 139 void 140 DBG_LOCAL (int level, const char *msg, ...) 141 { 142 va_list ap; 143 144 va_start (ap, msg); 145 sanei_debug_msg (level, DBG_LEVEL, STRINGIFY(BACKEND_NAME), msg, ap); 146 va_end (ap); 147 } 148 149 # endif /* DEBUG_DECLARE_ONLY */ 150 151 # endif /* !STUBS */ 152 153 /** @hideinitializer*/ 154 # define DBG DBG_LOCAL 155 156 extern void sanei_init_debug (const char * backend, int * debug_level_var); 157 158 /** @hideinitializer*/ 159 # define IF_DBG(x) x 160 161 #endif /* NDEBUG */ 162 163 #ifdef __cplusplus 164 } // extern "C" 165 #endif 166 167 #endif /* _SANEI_DEBUG_H */ 168