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