diff options
author | Marcus Meissner <marcus@jet.franken.de> | 2009-10-03 20:26:01 +0000 |
---|---|---|
committer | Marcus Meissner <marcus@jet.franken.de> | 2009-10-03 20:26:01 +0000 |
commit | 3905d55bf09969157dff2998f7681aa325768419 (patch) | |
tree | 23dfb7a5f22a4186536119d8c9a51f7174a01a2b /libgphoto2_port/libgphoto2_port | |
parent | 5534eb9a2144f2b235fe6f86182bf5b726bb6eff (diff) | |
download | libgphoto2-3905d55bf09969157dff2998f7681aa325768419.tar.gz |
2009-10-03 Marcus Meissner <meissner@suse.de>
* gphoto2/gphoto2-port-log.h, libgphoto2_port/gphoto2-port-log.c:
Add gp_log_simple_add_func, gp_log_simple_remove_func that
get passed simple strings to print.
git-svn-id: https://svn.code.sf.net/p/gphoto/code/trunk/libgphoto2@12481 67ed7778-7388-44ab-90cf-0a291f65f57c
Diffstat (limited to 'libgphoto2_port/libgphoto2_port')
-rw-r--r-- | libgphoto2_port/libgphoto2_port/gphoto2-port-log.c | 98 | ||||
-rw-r--r-- | libgphoto2_port/libgphoto2_port/libgphoto2_port.ver | 2 |
2 files changed, 100 insertions, 0 deletions
diff --git a/libgphoto2_port/libgphoto2_port/gphoto2-port-log.c b/libgphoto2_port/libgphoto2_port/gphoto2-port-log.c index e9e257497..bda2d5005 100644 --- a/libgphoto2_port/libgphoto2_port/gphoto2-port-log.c +++ b/libgphoto2_port/libgphoto2_port/gphoto2-port-log.c @@ -58,10 +58,19 @@ typedef struct { GPLogFunc func; /**< Internal function pointer to call */ void *data; /**< Private data supplied by caller */ } LogFunc; +typedef struct { + unsigned int id; /**< Internal id */ + GPLogLevel level; /**< Internal loglevel */ + GPLogSimpleFunc func; /**< Internal function pointer to call */ + void *data; /**< Private data supplied by caller */ +} LogSimpleFunc; static LogFunc *log_funcs = NULL; static unsigned int log_funcs_count = 0; +static LogSimpleFunc *log_simple_funcs = NULL; +static unsigned int log_simple_funcs_count = 0; + /** * \brief Add a function to get logging information * @@ -124,6 +133,67 @@ gp_log_remove_func (int id) } /** + * \brief Add a function to get logging information + * + * \param level the maximum level of logging it will get, up to and including the passed value + * \param func a #GPLogSimpleFunc + * \param data data + * + * Adds a log function that will be called for each log message that is flagged + * with a log level that appears in given log level. This function returns + * an id that you can use for removing the log function again (using + * #gp_log_remove_func). + * + * \return an id or a gphoto2 error code + **/ +int +gp_log_simple_add_func (GPLogLevel level, GPLogSimpleFunc func, void *data) +{ + LogSimpleFunc *new_log_funcs; + + if (!func) + return (GP_ERROR_BAD_PARAMETERS); + + if (!log_simple_funcs) + new_log_funcs = malloc (sizeof (LogSimpleFunc)); + else + new_log_funcs = realloc (log_simple_funcs, sizeof (LogSimpleFunc) * + (log_simple_funcs_count + 1)); + if (!new_log_funcs) + return (GP_ERROR_NO_MEMORY); + + log_simple_funcs = new_log_funcs; + log_simple_funcs_count++; + + log_simple_funcs[log_simple_funcs_count - 1].id = log_simple_funcs_count; + log_simple_funcs[log_simple_funcs_count - 1].level = level; + log_simple_funcs[log_simple_funcs_count - 1].func = func; + log_simple_funcs[log_simple_funcs_count - 1].data = data; + + return (log_simple_funcs_count); +} + +/** + * \brief Remove a logging receiving function + * \param id an id (return value of #gp_log_add_func) + * + * Removes the log function with given id. + * + * \return a gphoto2 error code + **/ +int +gp_log_simple_remove_func (int id) +{ + if (id < 1 || id > log_simple_funcs_count) + return (GP_ERROR_BAD_PARAMETERS); + + memmove (log_simple_funcs + id - 1, log_simple_funcs + id, log_simple_funcs_count - id); + log_simple_funcs_count--; + + return (GP_OK); +} + +/** * Width of offset field in characters. Note that HEXDUMP_COMPLETE_LINE * needs to be changed when this value is changed. */ @@ -277,6 +347,34 @@ gp_logv (GPLogLevel level, const char *domain, const char *format, #endif } } + + if (log_simple_funcs_count) { +#ifdef HAVE_VA_COPY + va_list xargs; +#endif + int strsize = 1000; + char *str = malloc(strsize); + int n; + + if (!str) return; +#ifdef HAVE_VA_COPY + va_copy (xargs, args); +#endif + n = vsnprintf (str, strsize, format, xargs); + if (n+1>strsize) { + free (str); + str = malloc(n+1); + if (!str) return; + strsize = n+1; +#ifdef HAVE_VA_COPY + va_copy (xargs, args); +#endif + n = vsnprintf (str, strsize, format, xargs); + } + for (i = 0; i < log_simple_funcs_count; i++) + if (log_simple_funcs[i].level >= level) + log_simple_funcs[i].func (level, domain, str, log_simple_funcs[i].data); + } } /** diff --git a/libgphoto2_port/libgphoto2_port/libgphoto2_port.ver b/libgphoto2_port/libgphoto2_port/libgphoto2_port.ver index aab84a844..3e1a90f5d 100644 --- a/libgphoto2_port/libgphoto2_port/libgphoto2_port.ver +++ b/libgphoto2_port/libgphoto2_port/libgphoto2_port.ver @@ -4,6 +4,8 @@ LIBGPHOTO2_5_0 { gp_log_add_func; gp_log_data; gp_log_remove_func; + gp_log_simple_add_func; + gp_log_simple_remove_func; gp_logv; gp_port_check_int; gp_port_check_int_fast; |