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