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