libgphoto2 photo camera library (libgphoto2) API 2.5.30

gphoto2-port-log.h
Go to the documentation of this file.
1
21#ifndef LIBGPHOTO2_GPHOTO2_PORT_LOG_H
22#define LIBGPHOTO2_GPHOTO2_PORT_LOG_H
23
24#include <stdarg.h>
25
26#ifdef __cplusplus
27 extern "C" {
28#endif /* __cplusplus */
29
34 typedef enum {
35 GP_LOG_ERROR = 0,
36 GP_LOG_VERBOSE = 1,
37 GP_LOG_DEBUG = 2,
38 GP_LOG_DATA = 3
39} GPLogLevel;
40
50#define GP_LOG_ALL GP_LOG_DATA
51
65 typedef void (* GPLogFunc) (GPLogLevel level, const char *domain, const char *str, void *data);
66
67#ifndef DISABLE_DEBUGGING
68
69 int gp_log_add_func (GPLogLevel level, GPLogFunc func, void *data);
70 int gp_log_remove_func (int id);
71
72/* Logging */
73 void gp_log (GPLogLevel level, const char *domain,
74 const char *format, ...)
75#ifdef __GNUC__
76 __attribute__((__format__(printf,3,4)))
77#endif
78;
79 void gp_log_with_source_location(
80 GPLogLevel level, const char *file, int line, const char *func,
81 const char *format, ...)
82#ifdef __GNUC__
83 __attribute__((__format__(printf,5,6)))
84#endif
85;
86 void gp_logv (GPLogLevel level, const char *domain, const char *format,
87 va_list args)
88#ifdef __GNUC__
89 __attribute__((__format__(printf,3,0)))
90#endif
91;
92 void gp_log_data (const char *domain, const char *data, unsigned int size,
93 const char *format, ...)
94#ifdef __GNUC__
95__attribute__((__format__(printf,4,5)))
96#endif
97;
98
99/*
100 * GP_DEBUG:
101 * msg: message to log
102 * params: params to message
103 *
104 * Logs message at log level #GP_LOG_DEBUG by calling #gp_log() with
105 * an automatically generated domain
106 * You have to define GP_MODULE as "mymod" for your module
107 * mymod before using #GP_DEBUG().
108 */
109
110#ifdef _GPHOTO2_INTERNAL_CODE
111#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
112#define GP_DEBUG(...) \
113 gp_log(GP_LOG_DEBUG, GP_MODULE "/" __FILE__, __VA_ARGS__)
114
115/*
116 * GP_LOG_D/E:
117 * simple helper macros for convenient and consistent logging of error
118 * and debug messages including information about the source location.
119 */
120#define GP_LOG_D(...) gp_log(GP_LOG_DEBUG, __func__, __VA_ARGS__)
121#define GP_LOG_E(...) gp_log_with_source_location(GP_LOG_ERROR, __FILE__, __LINE__, __func__, __VA_ARGS__)
122#define GP_LOG_DATA(DATA, SIZE, MSG, ...) gp_log_data(__func__, DATA, SIZE, MSG, ##__VA_ARGS__)
123
124#elif defined(__GNUC__) && __GNUC__ >= 2
125#define GP_DEBUG(msg, params...) \
126 gp_log(GP_LOG_DEBUG, GP_MODULE "/" __FILE__, msg, ##params)
127/*
128 * GP_LOG_D/E:
129 * simple helper macros for convenient and consistent logging of error
130 * and debug messages including information about the source location.
131 */
132#define GP_LOG_D(...) gp_log(GP_LOG_DEBUG, __func__, __VA_ARGS__)
133#define GP_LOG_E(...) gp_log_with_source_location(GP_LOG_ERROR, __FILE__, __LINE__, __func__, __VA_ARGS__)
134#define GP_LOG_DATA(DATA, SIZE, MSG, ...) gp_log_data(__func__, DATA, SIZE, MSG, ##__VA_ARGS__)
135
136#else
137# ifdef __GNUC__
138# warning Disabling GP_DEBUG because variadic macros are not allowed
139# endif
140#define GP_DEBUG (void)
141#define GP_LOG_D(...) /* no-op */
142#define GP_LOG_E(...) /* no-op */
143#define GP_LOG_DATA(DATA, SIZE, ...) /* no-op */
144#endif
145#endif /* _GPHOTO2_INTERNAL_CODE */
146
147#else /* DISABLE_DEBUGGING */
148
149/* Stub these functions out if debugging is disabled */
150#define gp_log_add_func(level, func, data) (0)
151#define gp_log_remove_func(id) (0)
152#define gp_log(level, domain, format, args...)
153#define gp_log_with_source_location(level, file, line, func, format, ...)
154#define gp_logv(level, domain, format, args)
155#define gp_log_data(domain, data, size)
156
157#ifdef _GPHOTO2_INTERNAL_CODE
158#if defined(__STDC_VERSION__) && __STDC_VERSION__ >= 199901L
159#define GP_DEBUG(...) /* no-op */
160#define GP_LOG_D(...) /* no-op */
161#define GP_LOG_E(...) /* no-op */
162#define GP_LOG_DATA(DATA, SIZE, ...) /* no-op */
163
164#elif defined(__GNUC__)
165#define GP_DEBUG(msg, params...) /* no-op */
166#define GP_LOG_D(...) /* no-op */
167#define GP_LOG_E(...) /* no-op */
168#define GP_LOG_DATA(DATA, SIZE, ...) /* no-op */
169#else
170#define GP_DEBUG (void)
171#define GP_LOG_D (void /* no-op */
172#define GP_LOG_E (void) /* no-op */
173#define GP_LOG_DATA(void) /* no-op */
174#endif
175#endif /* _GPHOTO2_INTERNAL_CODE */
176
177#endif /* DISABLE_DEBUGGING */
178
179#ifdef _GPHOTO2_INTERNAL_CODE
180
181 typedef struct StringFlagItem {
182 char *str;
183 unsigned int flag;
184 } StringFlagItem;
185
186 typedef void (*string_item_func) (const char *str, void *data);
187
188 const char *
189 gpi_enum_to_string(const unsigned int _enum,
190 const StringFlagItem *map);
191
192 int
193 gpi_string_to_enum(const char *str,
194 unsigned int *result,
195 const StringFlagItem *map);
196
197 void
198 gpi_flags_to_string_list(const unsigned int flags,
199 const StringFlagItem *map,
200 string_item_func func, void *data);
201
202 int
203 gpi_string_or_to_flags(const char *str,
204 unsigned int *flags,
205 const StringFlagItem *map);
206
207 unsigned int
208 gpi_string_to_flag(const char *str,
209 const StringFlagItem *map);
210
211 unsigned int
212 gpi_string_list_to_flags(const char *str[],
213 const StringFlagItem *map);
214
215 /* Allocates a sufficiently large buffer and interpolates the format
216 * string with the proveded va_list args. The returned memory has to
217 * be freed by the caller. */
218 char*
219 gpi_vsnprintf (const char* format, va_list args);
220
221#define C_MEM(MEM) do {\
222 if ((MEM) == NULL) {\
223 GP_LOG_E ("Out of memory: '%s' failed.", #MEM);\
224 return GP_ERROR_NO_MEMORY;\
225 }\
226} while(0)
227
228#define C_PARAMS(PARAMS) do {\
229 if (!(PARAMS)) {\
230 GP_LOG_E ("Invalid parameters: '%s' is NULL/FALSE.", #PARAMS);\
231 return GP_ERROR_BAD_PARAMETERS;\
232 }\
233} while(0)
234
235#define C_PARAMS_MSG(PARAMS, MSG, ...) do {\
236 if (!(PARAMS)) {\
237 GP_LOG_E ("Invalid parameters: " #MSG " ('%s' is NULL/FALSE.)", ##__VA_ARGS__, #PARAMS);\
238 return GP_ERROR_BAD_PARAMETERS;\
239 }\
240} while(0)
241
242#endif /* _GPHOTO2_INTERNAL_CODE */
243#ifdef __cplusplus
244}
245#endif /* __cplusplus */
246
247#endif /* !defined(LIBGPHOTO2_GPHOTO2_PORT_LOG_H) */
gp_logv
void gp_logv(GPLogLevel level, const char *domain, const char *format, va_list args)
Log a debug or error message with va_list.
Definition: gphoto2-port-log.c:287
gp_log_remove_func
int gp_log_remove_func(int id)
Remove a logging receiving function.
Definition: gphoto2-port-log.c:126
GPLogFunc
void(* GPLogFunc)(GPLogLevel level, const char *domain, const char *str, void *data)
Logging function hook.
Definition: gphoto2-port-log.h:65
gp_log
void gp_log(GPLogLevel level, const char *domain, const char *format,...)
Log a debug or error message.
Definition: gphoto2-port-log.c:319
gp_log_add_func
int gp_log_add_func(GPLogLevel level, GPLogFunc func, void *data)
Add a function to get logging information.
Definition: gphoto2-port-log.c:70
GPLogLevel
GPLogLevel
Logging level Specifies the logging severity level.
Definition: gphoto2-port-log.h:34
GP_LOG_VERBOSE
@ GP_LOG_VERBOSE
Log message is an verbose debug information.
Definition: gphoto2-port-log.h:36
GP_LOG_ERROR
@ GP_LOG_ERROR
Log message is an error information.
Definition: gphoto2-port-log.h:35
GP_LOG_DATA
@ GP_LOG_DATA
Log message is a data hex dump.
Definition: gphoto2-port-log.h:38
GP_LOG_DEBUG
@ GP_LOG_DEBUG
Log message is an debug information.
Definition: gphoto2-port-log.h:37
gp_log_data
void gp_log_data(const char *domain, const char *data, unsigned int size, const char *format,...)
Log data.
Definition: gphoto2-port-log.c:193