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