diff --git a/libgphoto2_port/ChangeLog b/libgphoto2_port/ChangeLog index 3b57e8bce..e483e204e 100644 --- a/libgphoto2_port/ChangeLog +++ b/libgphoto2_port/ChangeLog @@ -1,3 +1,11 @@ +2001-10-04 Lutz Müller + + * include/Makefile.am + * include/gphoto2-port-debug.h + * libgphoto2_port/Makefile.am + * libgphoto2_port/debug.c: New files + * libgphoto2_port/gp_port.c: Move debugging stuff out of here. + 2001-10-04 Lutz Müller * include/gphoto2-port.h: diff --git a/libgphoto2_port/include/Makefile.am b/libgphoto2_port/include/Makefile.am index 0d7e638bf..0ae887a2a 100644 --- a/libgphoto2_port/include/Makefile.am +++ b/libgphoto2_port/include/Makefile.am @@ -1,8 +1,10 @@ gphoto2_port_incdir = $(includedir)/gphoto2 -gphoto2_port_inc_HEADERS = gphoto2-port.h \ - gphoto2-portability.h \ - gphoto2-port-serial.h \ - gphoto2-port-parallel.h \ - gphoto2-port-usb.h \ - gphoto2-port-ieee1394.h \ - gphoto2-port-network.h +gphoto2_port_inc_HEADERS =\ + gphoto2-port.h \ + gphoto2-port-debug.h \ + gphoto2-portability.h \ + gphoto2-port-serial.h \ + gphoto2-port-parallel.h \ + gphoto2-port-usb.h \ + gphoto2-port-ieee1394.h \ + gphoto2-port-network.h diff --git a/libgphoto2_port/include/gphoto2-port-debug.h b/libgphoto2_port/include/gphoto2-port-debug.h new file mode 100644 index 000000000..ee77d2eaa --- /dev/null +++ b/libgphoto2_port/include/gphoto2-port-debug.h @@ -0,0 +1,37 @@ +/* gphoto2-port-debug.h + * + * Copyright (C) 2001 Lutz Müller + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#ifndef __GPHOTO2_PORT_DEBUG_H__ +#define __GPHOTO2_PORT_DEBUG_H__ + +void gp_port_debug_set_level (int level); +void gp_port_debug_printf (int target_debug_level, int debug_level, + char *format, ...); + +/* Custom debugging function */ +typedef void (* GPPortDebugFunc) (const char *msg, void *data); +void gp_port_debug_set_func (GPPortDebugFunc func, void *data); + +/* History */ +void gp_port_debug_history_append (const char *msg); +int gp_port_debug_history_set_size (unsigned int size); +const char *gp_port_debug_history_get (void); + +#endif /* __GPHOTO2_PORT_DEBUG_H__ */ diff --git a/libgphoto2_port/include/gphoto2-port.h b/libgphoto2_port/include/gphoto2-port.h index 481648ea2..10aadf202 100644 --- a/libgphoto2_port/include/gphoto2-port.h +++ b/libgphoto2_port/include/gphoto2-port.h @@ -177,9 +177,6 @@ struct gp_port { /* Core functions -------------------------------------------------------------- */ - void gp_port_debug_printf (int target_debug_level, int debug_level, char *format, ...); - /* issues debugging messages */ - char *gp_port_result_as_string (int result); /* Returns a string describing the error */ diff --git a/libgphoto2_port/libgphoto2_port/Makefile.am b/libgphoto2_port/libgphoto2_port/Makefile.am index cbca96023..a70bc0afc 100644 --- a/libgphoto2_port/libgphoto2_port/Makefile.am +++ b/libgphoto2_port/libgphoto2_port/Makefile.am @@ -10,6 +10,7 @@ LDFLAGS = @LDFLAGS@ -g -ldl lib_LTLIBRARIES = libgphoto2_port.la libgphoto2_port_la_LDFLAGS = -version-info @LIBGPPORT_VERSION_INFO@ libgphoto2_port_la_SOURCES = \ - gp_port.c \ - library.c library.h \ - portability.c + gp_port.c \ + debug.c \ + library.c library.h \ + portability.c diff --git a/libgphoto2_port/libgphoto2_port/debug.c b/libgphoto2_port/libgphoto2_port/debug.c new file mode 100644 index 000000000..9be3c3ddb --- /dev/null +++ b/libgphoto2_port/libgphoto2_port/debug.c @@ -0,0 +1,132 @@ +/* debug.c + * + * Copyright (C) 2001 Lutz Müller + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Lesser General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + * Lesser General Public License for more details. + * + * You should have received a copy of the GNU Lesser General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 59 Temple Place - Suite 330, + * Boston, MA 02111-1307, USA. + */ + +#include "gphoto2-port-debug.h" + +#include +#include +#include +#include + +#include + +int glob_debug_level = 0; + +static GPPortDebugFunc debug_func = NULL; +static void *debug_func_data = NULL; + +static unsigned int debug_history_size = 0; +static char *debug_history = NULL; + +void +gp_port_debug_set_level (int level) +{ + glob_debug_level = level; +} + +void +gp_port_debug_set_func (GPPortDebugFunc func, void *data) +{ + debug_func = func; + debug_func_data = data; +} + +void +gp_port_debug_printf (int target_debug_level, int debug_level, + char *format, ...) +{ + char buffer[2048]; + va_list arg; + + va_start (arg, format); + vsprintf (buffer, format, arg); + va_end (arg); + + gp_port_debug_history_append (buffer); + + if ((debug_level > 0) && (debug_level >= target_debug_level)) { + if (debug_func) { + + /* Custom debug function */ + debug_func (buffer, debug_func_data); + + } else { + + /* Own implementation */ + fprintf(stderr, "gp_port: "); + va_start(arg, format); + vfprintf(stderr, format, arg); + va_end(arg); + fprintf(stderr, "\n"); + } + } +} + +void +gp_port_debug_history_append (const char *msg) +{ + int i; + + /* Create history if needed */ + if (!debug_history) { + debug_history_size = 1024; + debug_history = malloc (sizeof (char) * debug_history_size); + memset (debug_history, 0, debug_history_size); + } + + /* Do we have to forget parts of the debug history? */ + while (debug_history_size - strlen (debug_history) < + strlen (msg) + 1) { + + /* Determine len of first entry */ + for (i = 0; debug_history[i] != '\0'; i++) + if (debug_history[i] == '\n') + break; + + /* Remove the first entry */ + memmove (debug_history, debug_history + i + 1, + strlen (debug_history) - i); + } + + strcat (debug_history, msg); + strcat (debug_history, "\n"); +} + +int +gp_port_debug_history_set_size (unsigned int size) +{ + char *new_debug_history; + + new_debug_history = realloc (debug_history, sizeof (char) * size); + + if (!new_debug_history) + return (GP_ERROR); + + debug_history_size = size; + debug_history = new_debug_history; + + return (GP_OK); +} + +const char * +gp_port_debug_history_get (void) +{ + return (debug_history); +} diff --git a/libgphoto2_port/libgphoto2_port/gp_port.c b/libgphoto2_port/libgphoto2_port/gp_port.c index 39bf15f04..cbb771efb 100644 --- a/libgphoto2_port/libgphoto2_port/gp_port.c +++ b/libgphoto2_port/libgphoto2_port/gp_port.c @@ -24,9 +24,13 @@ #include #include #include -#include "../include/gphoto2-port.h" + +#include +#include #include "library.h" +extern int glob_debug_level; + #ifdef ENABLE_NLS # include # undef _ @@ -46,22 +50,6 @@ int device_count; int initialized = 0; -/* Toggle to turn on/off debugging */ -int glob_debug_level=0; - -void gp_port_debug_printf (int target_debug_level, int debug_level, char *format, ...) -{ - va_list arg; - if ((debug_level > 0)&&(debug_level >= target_debug_level)) { - - fprintf(stderr, "gp_port: "); - va_start(arg, format); - vfprintf(stderr, format, arg); - va_end(arg); - fprintf(stderr, "\n"); - } -} - #define GP_ERR_RES(num,str) \ if (result == num) \ return (N_(str)); @@ -114,7 +102,7 @@ int gp_port_init(int debug) gp_port_debug_printf(GP_DEBUG_LOW, glob_debug_level, "Initializing..."); /* Enumerate all the available devices */ device_count = 0; - glob_debug_level = debug; + gp_port_debug_set_level (debug); initialized = 1; return (gp_port_library_list(device_list, &device_count)); } diff --git a/libgphoto2_port/libgphoto2_port/library.c b/libgphoto2_port/libgphoto2_port/library.c index 6b916b56f..bfbf5305a 100644 --- a/libgphoto2_port/libgphoto2_port/library.c +++ b/libgphoto2_port/libgphoto2_port/library.c @@ -1,6 +1,7 @@ #include #include +#include #include "gphoto2-port.h" #include "library.h"