24 #define _DEFAULT_SOURCE
39 # define _(String) dgettext (GETTEXT_PACKAGE, String)
41 # define N_(String) gettext_noop (String)
43 # define N_(String) (String)
46 # define _(String) (String)
47 # define N_(String) (String)
50 #ifndef DISABLE_DEBUGGING
84 static int logfuncid = 0;
108 va_copy (xargs, args);
116 strsize = vsnprintf (NULL, 0, format, xargs) + 1;
119 str = malloc(strsize);
124 vsnprintf (str, strsize, format, args);
156 #define HEXDUMP_OFFSET_WIDTH 4
162 #define HEXDUMP_BLOCK_DISTANCE 2
165 #define HEXDUMP_INIT_X (HEXDUMP_OFFSET_WIDTH + HEXDUMP_BLOCK_DISTANCE)
168 #define HEXDUMP_INIT_Y (HEXDUMP_INIT_X + 3 * 16 - 1 + HEXDUMP_BLOCK_DISTANCE)
171 #define HEXDUMP_LINE_WIDTH (HEXDUMP_INIT_Y + 16)
174 #define HEXDUMP_MIDDLE (HEXDUMP_INIT_X + 3 * 8 - 1)
181 #define HEXDUMP_COMPLETE_LINE {\
182 curline[HEXDUMP_OFFSET_WIDTH - 4] = hexchars[(index >> 12) & 0xf]; \
183 curline[HEXDUMP_OFFSET_WIDTH - 3] = hexchars[(index >> 8) & 0xf]; \
184 curline[HEXDUMP_OFFSET_WIDTH - 2] = hexchars[(index >> 4) & 0xf]; \
185 curline[HEXDUMP_OFFSET_WIDTH - 1] = '0'; \
186 curline[HEXDUMP_OFFSET_WIDTH + 0] = ' '; \
187 curline[HEXDUMP_OFFSET_WIDTH + 1] = ' '; \
188 curline[HEXDUMP_MIDDLE] = '-'; \
189 curline[HEXDUMP_INIT_Y-2] = ' '; \
190 curline[HEXDUMP_INIT_Y-1] = ' '; \
191 curline[HEXDUMP_LINE_WIDTH] = '\n'; \
192 curline = curline + (HEXDUMP_LINE_WIDTH + 1);}
205 gp_log_data (
const char *domain,
const char *data,
unsigned int size,
const char *format, ...)
208 static const char hexchars[16] =
"0123456789abcdef";
209 char *curline, *
result = 0, *msg = 0;
212 unsigned int index, original_size = size;
215 va_start (args, format);
219 GP_LOG_E (
"Malloc for expanding format string '%s' failed.", format);
233 if (size > 1024*1024) {
244 for (index = 0; index < size; ++index) {
245 value = (
unsigned char)data[index];
246 curline[x] = hexchars[value >> 4];
247 curline[x+1] = hexchars[value & 0xf];
249 curline[y++] = ((value>=32)&&(value<127))?value:
'.';
251 if ((index & 0xf) == 0xf) {
257 if ((index & 0xf) != 0) {
270 if (size == original_size)
273 gp_log (
GP_LOG_DATA, domain,
"%s (hexdump of the first %d of %d bytes)\n%s", msg, size, original_size,
result);
280 #undef HEXDUMP_COMPLETE_LINE
281 #undef HEXDUMP_MIDDLE
282 #undef HEXDUMP_LINE_WIDTH
283 #undef HEXDUMP_INIT_Y
284 #undef HEXDUMP_INIT_X
285 #undef HEXDUMP_BLOCK_DISTANCE
286 #undef HEXDUMP_OFFSET_WIDTH
310 GP_LOG_E (
"Malloc for expanding format string '%s' failed.", format);
335 va_start (args, format);
347 file = strrchr(file,
'/') ? strrchr(file,
'/') + 1 : file;
348 snprintf(domain,
sizeof(domain),
"%s [%s:%d]", func, file, line);
350 va_start (args, format);
363 #ifdef gp_log_add_func
364 #undef gp_log_add_func
366 #ifdef gp_log_remove_func
367 #undef gp_log_remove_func
378 #ifdef gp_log_with_source_location
379 #undef gp_log_with_source_location
395 gp_log_data (
const char *domain,
const char *data,
unsigned int size,
const char *format, ...)
417 #ifdef _GPHOTO2_INTERNAL_CODE
423 gpi_enum_to_string(
unsigned int _enum,
424 const StringFlagItem *map)
427 for (i=0; map[i].str != NULL; i++) {
428 if (_enum == map[i].flag) {
440 gpi_string_to_enum(
const char *str,
442 const StringFlagItem *map)
445 for (i=0; map[i].str != NULL; i++) {
446 if (0==strcmp(map[i].str, str)) {
447 (*result) = map[i].flag;
455 gpi_flags_to_string_list(
unsigned int flags,
456 const StringFlagItem *map,
457 string_item_func func,
void *data)
460 for (i=0; map[i].str != NULL; i++) {
461 if ((flags == 0) && (map[i].flag == 0)) {
462 func(map[i].str, data);
464 }
else if (0 != (flags & map[i].flag)) {
465 func(map[i].str, data);
471 gpi_string_to_flag(
const char *str,
472 const StringFlagItem *map)
475 for (i=0; map[i].str != NULL; i++) {
476 if (0==strcmp(map[i].str, str)) {
484 gpi_string_or_to_flags(
const char *str,
486 const StringFlagItem *map)
490 for (i=0; map[i].str != NULL; i++) {
491 if (0==strcmp(map[i].str, str)) {
492 (*flags) |= map[i].flag;
504 gpi_string_list_to_flags(
const char *str[],
505 const StringFlagItem *map)
508 unsigned int flags = 0;
509 for (i=0; str[i] != NULL; i++) {
510 flags |= gpi_string_to_flag(str[i], map);
void gp_logv(GPLogLevel level, const char *domain, const char *format, va_list args)
Log a debug or error message with va_list.
int gp_log_remove_func(int id)
Remove a logging receiving function.
#define HEXDUMP_COMPLETE_LINE
static LogFunc * log_funcs
char * gpi_vsnprintf(const char *format, va_list args)
void gp_log(GPLogLevel level, const char *domain, const char *format,...)
Log a debug or error message.
int gp_log_add_func(GPLogLevel level, GPLogFunc func, void *data)
Add a function to get logging information.
void gp_log_with_source_location(GPLogLevel level, const char *file, int line, const char *func, const char *format,...)
void gp_log_data(const char *domain, const char *data, unsigned int size, const char *format,...)
Log data.
#define HEXDUMP_LINE_WIDTH
static unsigned int log_funcs_count
void(* GPLogFunc)(GPLogLevel level, const char *domain, const char *str, void *data)
Logging function hook.
GPLogLevel
Logging level Specifies the logging severity level.
@ GP_LOG_DATA
Log message is a data hex dump.
#define GP_OK
Everything is OK.
#define GP_ERROR_BAD_PARAMETERS
Bad parameters passed.
Internal logging function entry.