From: Sebastian Harl Date: Tue, 10 Dec 2013 17:56:59 +0000 (+0100) Subject: Moved core/error to utils/error. X-Git-Tag: sysdb-0.1.0~317 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=ddb933096618a6bceded29e4dc2b37cb72134366;p=sysdb.git Moved core/error to utils/error. Later, we'll use the module from the daemon and client. --- diff --git a/src/Makefile.am b/src/Makefile.am index 5a18c7b..4bbc3fb 100644 --- a/src/Makefile.am +++ b/src/Makefile.am @@ -12,7 +12,6 @@ pkginclude_HEADERS = include/sysdb.h pkgcoreincludedir = $(pkgincludedir)/core pkgcoreinclude_HEADERS = \ include/core/data.h \ - include/core/error.h \ include/core/object.h \ include/core/plugin.h \ include/core/store.h \ @@ -26,6 +25,7 @@ pkgutilsincludedir = $(pkgincludedir)/utils pkgutilsinclude_HEADERS = \ include/utils/channel.h \ include/utils/dbi.h \ + include/utils/error.h \ include/utils/llist.h \ include/utils/proto.h \ include/utils/strbuf.h \ @@ -54,13 +54,13 @@ libsysdb_la_SOURCES = \ core/plugin.c include/core/plugin.h \ core/store.c include/core/store.h \ include/core/data.h \ - core/error.c include/core/error.h \ frontend/connection.c include/frontend/connection.h \ include/frontend/connection-private.h \ frontend/sock.c include/frontend/sock.h \ frontend/session.c \ frontend/query.c \ utils/channel.c include/utils/channel.h \ + utils/error.c include/utils/error.h \ utils/llist.c include/utils/llist.h \ utils/proto.c include/utils/proto.h \ utils/strbuf.c include/utils/strbuf.h \ diff --git a/src/backend/collectd/unixsock.c b/src/backend/collectd/unixsock.c index c01b0f3..766bbce 100644 --- a/src/backend/collectd/unixsock.c +++ b/src/backend/collectd/unixsock.c @@ -28,7 +28,7 @@ #include "sysdb.h" #include "core/plugin.h" #include "core/store.h" -#include "core/error.h" +#include "utils/error.h" #include "utils/unixsock.h" #include "liboconfig/utils.h" diff --git a/src/backend/mk-livestatus.c b/src/backend/mk-livestatus.c index d672b36..bf45da6 100644 --- a/src/backend/mk-livestatus.c +++ b/src/backend/mk-livestatus.c @@ -28,7 +28,7 @@ #include "sysdb.h" #include "core/plugin.h" #include "core/store.h" -#include "core/error.h" +#include "utils/error.h" #include "utils/unixsock.h" #include "liboconfig/utils.h" diff --git a/src/backend/puppet/store-configs.c b/src/backend/puppet/store-configs.c index 4343920..a9a0ff7 100644 --- a/src/backend/puppet/store-configs.c +++ b/src/backend/puppet/store-configs.c @@ -28,8 +28,8 @@ #include "sysdb.h" #include "core/plugin.h" #include "core/store.h" -#include "core/error.h" #include "utils/dbi.h" +#include "utils/error.h" #include "liboconfig/utils.h" diff --git a/src/core/error.c b/src/core/error.c deleted file mode 100644 index 7d14a25..0000000 --- a/src/core/error.c +++ /dev/null @@ -1,283 +0,0 @@ -/* - * SysDB - src/core/error.c - * Copyright (C) 2013 Sebastian 'tokkee' Harl - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -#include "core/error.h" -#include "core/plugin.h" -#include "utils/strbuf.h" - -#include - -#include -#include -#include - -#include - -/* - * private data types - */ - -typedef struct { - int prio; - sdb_strbuf_t *msg; - _Bool logged; -} sdb_error_ctx_t; -#define SDB_ERROR_INIT { -1, NULL, 1 } - -/* - * private variables - */ - -static sdb_error_ctx_t default_error_ctx = SDB_ERROR_INIT; - -static pthread_key_t error_ctx_key; -static _Bool error_ctx_key_initialized = 0; - -/* - * private helper functions - */ - -static void -sdb_error_ctx_destructor(void *p) -{ - sdb_error_ctx_t *ctx = p; - - if (! ctx) - return; - - sdb_strbuf_destroy(ctx->msg); - free(ctx); -} /* sdb_error_ctx_destructor */ - -static void -sdb_error_ctx_init(void) -{ - if (error_ctx_key_initialized) - return; - - pthread_key_create(&error_ctx_key, sdb_error_ctx_destructor); - error_ctx_key_initialized = 1; -} /* sdb_error_init */ - -static sdb_error_ctx_t * -sdb_error_ctx_create(void) -{ - sdb_error_ctx_t *ctx; - - ctx = malloc(sizeof(*ctx)); - if (! ctx) - return NULL; - - *ctx = default_error_ctx; - ctx->msg = sdb_strbuf_create(64); - if (! ctx->msg) { - free(ctx); - return NULL; - } - - if (! error_ctx_key_initialized) - sdb_error_ctx_init(); - pthread_setspecific(error_ctx_key, ctx); - return ctx; -} /* sdb_error_ctx_create */ - -static sdb_error_ctx_t * -sdb_error_get_ctx(void) -{ - sdb_error_ctx_t *ctx; - - if (! error_ctx_key_initialized) - sdb_error_ctx_init(); - ctx = pthread_getspecific(error_ctx_key); - - if (! ctx) - ctx = sdb_error_ctx_create(); - if (! ctx) - return NULL; - return ctx; -} /* sdb_error_get_ctx */ - -static int -sdb_error_vprintf(const char *fmt, va_list ap) -{ - sdb_error_ctx_t *ctx; - - ctx = sdb_error_get_ctx(); - if (! ctx) - return -1; - - ctx->logged = 0; - return (int)sdb_strbuf_vsprintf(ctx->msg, fmt, ap); -} /* sdb_error_vprintf */ - -static int -sdb_error_vappend(const char *fmt, va_list ap) -{ - sdb_error_ctx_t *ctx; - - ctx = sdb_error_get_ctx(); - if (! ctx) - return -1; - - ctx->logged = 0; - return (int)sdb_strbuf_vappend(ctx->msg, fmt, ap); -} /* sdb_error_vappend */ - -static int -sdb_do_log(int prio) -{ - sdb_error_ctx_t *ctx; - int ret; - - ctx = sdb_error_get_ctx(); - if (! ctx) - return -1; - - if (prio >= 0) - ctx->prio = prio; - - if (ctx->logged) - return 0; - - ret = sdb_plugin_log(prio, sdb_strbuf_string(ctx->msg)); - ctx->logged = 1; - return ret; -} /* sdb_do_log */ - -/* - * public API - */ - -int -sdb_log(int prio, const char *fmt, ...) -{ - va_list ap; - int ret; - - va_start(ap, fmt); - ret = sdb_error_vprintf(fmt, ap); - va_end(ap); - - if (ret > 0) - sdb_do_log(prio); - return ret; -} /* sdb_log */ - -int -sdb_error_set(const char *fmt, ...) -{ - va_list ap; - int ret; - - va_start(ap, fmt); - ret = sdb_error_vprintf(fmt, ap); - va_end(ap); - - return ret; -} /* sdb_error_set */ - -int -sdb_error_append(const char *fmt, ...) -{ - va_list ap; - int ret; - - va_start(ap, fmt); - ret = sdb_error_vappend(fmt, ap); - va_end(ap); - - return ret; -} /* sdb_error_append */ - -int -sdb_error_chomp(void) -{ - sdb_error_ctx_t *ctx; - - ctx = sdb_error_get_ctx(); - if (! ctx) - return -1; - - sdb_strbuf_chomp(ctx->msg); - return 0; -} /* sdb_error_chomp */ - -int -sdb_error_log(int prio) -{ - return sdb_do_log(prio); -} /* sdb_error_log */ - -const char * -sdb_error_get(void) -{ - sdb_error_ctx_t *ctx; - - ctx = sdb_error_get_ctx(); - if (! ctx) - return "success"; - return sdb_strbuf_string(ctx->msg); -} /* sdb_error_get */ - -int -sdb_error_get_prio(void) -{ - sdb_error_ctx_t *ctx; - - ctx = sdb_error_get_ctx(); - if (! ctx) - return -1; - return ctx->prio; -} /* sdb_error_get_prio */ - -char * -sdb_strerror(int errnum, char *strerrbuf, size_t buflen) -{ -#if STRERROR_R_CHAR_P - { - char *tmp = strerror_r(errnum, strerrbuf, buflen); - if (*strerrbuf = '\0') { - if (tmp && (tmp != strerrbuf) && (*tmp != '\0')) - strncpy(strerrbuf, tmp, buflen); - else - snprintf(strerrbuf, buflen, "unknown error #%i " - "(strerror_r(3) did not return an error message)", - errnum); - } - } -#else - if (strerror_r(errnum, strerrbuf, buflen)) - snprintf(strerrbuf, buflen, "unknown error #%i " - "(strerror_r(3) failed)", errnum); -#endif - - strerrbuf[buflen - 1] = '\0'; - return strerrbuf; -} /* sdb_strerror */ - -/* vim: set tw=78 sw=4 ts=4 noexpandtab : */ - diff --git a/src/core/plugin.c b/src/core/plugin.c index 09d5cce..8200338 100644 --- a/src/core/plugin.c +++ b/src/core/plugin.c @@ -27,8 +27,8 @@ #include "sysdb.h" #include "core/plugin.h" -#include "core/error.h" #include "core/time.h" +#include "utils/error.h" #include "utils/llist.h" #include "utils/strbuf.h" diff --git a/src/core/store.c b/src/core/store.c index 587c527..8f5cae3 100644 --- a/src/core/store.c +++ b/src/core/store.c @@ -27,8 +27,8 @@ #include "sysdb.h" #include "core/store.h" -#include "core/error.h" #include "core/plugin.h" +#include "utils/error.h" #include "utils/llist.h" #include diff --git a/src/daemon/config.c b/src/daemon/config.c index beb0217..906f4c5 100644 --- a/src/daemon/config.c +++ b/src/daemon/config.c @@ -27,8 +27,8 @@ #include "sysdb.h" #include "core/plugin.h" -#include "core/error.h" #include "core/time.h" +#include "utils/error.h" #include "daemon/config.h" diff --git a/src/daemon/sysdbd.c b/src/daemon/sysdbd.c index 67d4515..6952c1c 100644 --- a/src/daemon/sysdbd.c +++ b/src/daemon/sysdbd.c @@ -32,7 +32,7 @@ #include "sysdb.h" #include "core/plugin.h" #include "core/store.h" -#include "core/error.h" +#include "utils/error.h" #include "frontend/sock.h" diff --git a/src/frontend/connection.c b/src/frontend/connection.c index 096a759..636396a 100644 --- a/src/frontend/connection.c +++ b/src/frontend/connection.c @@ -26,9 +26,9 @@ */ #include "sysdb.h" -#include "core/error.h" #include "core/object.h" #include "frontend/connection-private.h" +#include "utils/error.h" #include "utils/strbuf.h" #include "utils/proto.h" diff --git a/src/frontend/query.c b/src/frontend/query.c index fa493a6..b3bf05e 100644 --- a/src/frontend/query.c +++ b/src/frontend/query.c @@ -27,9 +27,9 @@ #include "sysdb.h" -#include "core/error.h" #include "core/store.h" #include "frontend/connection-private.h" +#include "utils/error.h" #include "utils/strbuf.h" #include diff --git a/src/frontend/sock.c b/src/frontend/sock.c index cac49e1..4cf1d79 100644 --- a/src/frontend/sock.c +++ b/src/frontend/sock.c @@ -26,12 +26,12 @@ */ #include "sysdb.h" -#include "core/error.h" #include "core/object.h" #include "frontend/connection-private.h" #include "frontend/sock.h" #include "utils/channel.h" +#include "utils/error.h" #include "utils/llist.h" #include "utils/strbuf.h" diff --git a/src/include/core/error.h b/src/include/core/error.h deleted file mode 100644 index 1a55c3b..0000000 --- a/src/include/core/error.h +++ /dev/null @@ -1,139 +0,0 @@ -/* - * SysDB - src/include/core/error.h - * Copyright (C) 2013 Sebastian 'tokkee' Harl - * All rights reserved. - * - * Redistribution and use in source and binary forms, with or without - * modification, are permitted provided that the following conditions - * are met: - * 1. Redistributions of source code must retain the above copyright - * notice, this list of conditions and the following disclaimer. - * 2. Redistributions in binary form must reproduce the above copyright - * notice, this list of conditions and the following disclaimer in the - * documentation and/or other materials provided with the distribution. - * - * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS - * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED - * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR - * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR - * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, - * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, - * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; - * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, - * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR - * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF - * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. - */ - -/* - * SysDB error handling: - * Error handling in SysDB is done on a by-thread basis, that is, each thread - * will use its own memory region to store information about the last reported - * error. - * Once the error message has been passed to SysDB, it will log the entire - * message at once. The message will be sent to all registered log functions. - */ - -#ifndef SDB_CORE_ERROR_H -#define SDB_CORE_ERROR_H 1 - -#include - -#ifdef __cplusplus -extern "C" { -#endif - -/* max length of any error message */ -#ifndef SDB_MAX_ERROR -# define SDB_MAX_ERROR 4096 -#endif /* ! SDB_MAX_ERROR */ - -/* On Linux systems and possibly others, this should be the same as the LOG_ - * constants defined by syslog. */ -enum { - SDB_LOG_EMERG = 0, - SDB_LOG_ERR = 3, - SDB_LOG_WARNING = 4, - SDB_LOG_NOTICE = 5, - SDB_LOG_INFO = 6, - SDB_LOG_DEBUG = 7, -}; -#define SDB_LOG_PRIO_TO_STRING(prio) \ - (((prio) == SDB_LOG_EMERG) ? "EMERG" \ - : ((prio) == SDB_LOG_ERR) ? "ERROR" \ - : ((prio) == SDB_LOG_WARNING) ? "WARNING" \ - : ((prio) == SDB_LOG_NOTICE) ? "NOTICE" \ - : ((prio) == SDB_LOG_INFO) ? "INFO" \ - : ((prio) == SDB_LOG_DEBUG) ? "DEBUG" : "UNKNOWN") - -/* - * sdb_log: - * Log the specified message. The string will be formatted in printf-style - * using the specified format and arguments and logged with the specified - * priority. In addition, the error message will be stored as the current - * error message. This function is basically the same as calling sdb_error_set - * and sdb_error_log. XXX: SDB_LOG_EMERG might, at some point and/or depending - * on configuration, try a clean shut-down of the process. - */ -int -sdb_log(int prio, const char *fmt, ...); - -/* - * sdb_error_set, sdb_error_append: - * Compose the current error message. The string will be formatted in printf- - * style using the specified format and arguments. No automatic logging will - * be done. - */ -int -sdb_error_set(const char *fmt, ...); -int -sdb_error_append(const char *fmt, ...); - -/* - * sdb_error_chomp: - * Remove all consecutive newline characters at the end of the error message. - */ -int -sdb_error_chomp(void); - -/* - * sdb_error_log: - * Log the current error message with the specified priority. See sdb_log for - * more information. - */ -int -sdb_error_log(int prio); - -/* - * sdb_error_get: - * Get the current error message. The string returned by this function is - * owned by SysDB and might point to static memory -- do not modify or free - * it. - */ -const char * -sdb_error_get(void); - -/* - * sdb_error_get_prio: - * Get the priority of the last logged error message -- see the SDB_LOG_ - * constants for details. - */ -int -sdb_error_get_prio(void); - -/* - * sdb_strerror: - * This is a wrapper around the system's strerror function which ensures that - * a pointer to the formatted error message is returned. - */ -char * -sdb_strerror(int errnum, char *strerrbuf, size_t buflen); - -#ifdef __cplusplus -} /* extern "C" */ -#endif - -#endif /* ! SDB_CORE_ERROR_H */ - -/* vim: set tw=78 sw=4 ts=4 noexpandtab : */ - diff --git a/src/include/utils/error.h b/src/include/utils/error.h new file mode 100644 index 0000000..aaa288c --- /dev/null +++ b/src/include/utils/error.h @@ -0,0 +1,139 @@ +/* + * SysDB - src/include/utils/error.h + * Copyright (C) 2013 Sebastian 'tokkee' Harl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +/* + * SysDB error handling: + * Error handling in SysDB is done on a by-thread basis, that is, each thread + * will use its own memory region to store information about the last reported + * error. + * Once the error message has been passed to SysDB, it will log the entire + * message at once. The message will be sent to all registered log functions. + */ + +#ifndef SDB_UTILS_ERROR_H +#define SDB_UTILS_ERROR_H 1 + +#include + +#ifdef __cplusplus +extern "C" { +#endif + +/* max length of any error message */ +#ifndef SDB_MAX_ERROR +# define SDB_MAX_ERROR 4096 +#endif /* ! SDB_MAX_ERROR */ + +/* On Linux systems and possibly others, this should be the same as the LOG_ + * constants defined by syslog. */ +enum { + SDB_LOG_EMERG = 0, + SDB_LOG_ERR = 3, + SDB_LOG_WARNING = 4, + SDB_LOG_NOTICE = 5, + SDB_LOG_INFO = 6, + SDB_LOG_DEBUG = 7, +}; +#define SDB_LOG_PRIO_TO_STRING(prio) \ + (((prio) == SDB_LOG_EMERG) ? "EMERG" \ + : ((prio) == SDB_LOG_ERR) ? "ERROR" \ + : ((prio) == SDB_LOG_WARNING) ? "WARNING" \ + : ((prio) == SDB_LOG_NOTICE) ? "NOTICE" \ + : ((prio) == SDB_LOG_INFO) ? "INFO" \ + : ((prio) == SDB_LOG_DEBUG) ? "DEBUG" : "UNKNOWN") + +/* + * sdb_log: + * Log the specified message. The string will be formatted in printf-style + * using the specified format and arguments and logged with the specified + * priority. In addition, the error message will be stored as the current + * error message. This function is basically the same as calling sdb_error_set + * and sdb_error_log. XXX: SDB_LOG_EMERG might, at some point and/or depending + * on configuration, try a clean shut-down of the process. + */ +int +sdb_log(int prio, const char *fmt, ...); + +/* + * sdb_error_set, sdb_error_append: + * Compose the current error message. The string will be formatted in printf- + * style using the specified format and arguments. No automatic logging will + * be done. + */ +int +sdb_error_set(const char *fmt, ...); +int +sdb_error_append(const char *fmt, ...); + +/* + * sdb_error_chomp: + * Remove all consecutive newline characters at the end of the error message. + */ +int +sdb_error_chomp(void); + +/* + * sdb_error_log: + * Log the current error message with the specified priority. See sdb_log for + * more information. + */ +int +sdb_error_log(int prio); + +/* + * sdb_error_get: + * Get the current error message. The string returned by this function is + * owned by SysDB and might point to static memory -- do not modify or free + * it. + */ +const char * +sdb_error_get(void); + +/* + * sdb_error_get_prio: + * Get the priority of the last logged error message -- see the SDB_LOG_ + * constants for details. + */ +int +sdb_error_get_prio(void); + +/* + * sdb_strerror: + * This is a wrapper around the system's strerror function which ensures that + * a pointer to the formatted error message is returned. + */ +char * +sdb_strerror(int errnum, char *strerrbuf, size_t buflen); + +#ifdef __cplusplus +} /* extern "C" */ +#endif + +#endif /* ! SDB_UTILS_ERROR_H */ + +/* vim: set tw=78 sw=4 ts=4 noexpandtab : */ + diff --git a/src/plugins/cname/dns.c b/src/plugins/cname/dns.c index d989e52..6f5d518 100644 --- a/src/plugins/cname/dns.c +++ b/src/plugins/cname/dns.c @@ -27,7 +27,7 @@ #include "sysdb.h" #include "core/plugin.h" -#include "core/error.h" +#include "utils/error.h" #include #include diff --git a/src/plugins/syslog.c b/src/plugins/syslog.c index b548988..a487521 100644 --- a/src/plugins/syslog.c +++ b/src/plugins/syslog.c @@ -27,7 +27,7 @@ #include "sysdb.h" #include "core/plugin.h" -#include "core/error.h" +#include "utils/error.h" #include #include diff --git a/src/utils/dbi.c b/src/utils/dbi.c index db93857..2692618 100644 --- a/src/utils/dbi.c +++ b/src/utils/dbi.c @@ -25,8 +25,8 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "core/error.h" #include "utils/dbi.h" +#include "utils/error.h" #include diff --git a/src/utils/error.c b/src/utils/error.c new file mode 100644 index 0000000..5b9b448 --- /dev/null +++ b/src/utils/error.c @@ -0,0 +1,283 @@ +/* + * SysDB - src/utils/error.c + * Copyright (C) 2013 Sebastian 'tokkee' Harl + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions + * are met: + * 1. Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * 2. Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS + * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED + * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR + * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDERS OR + * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, + * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, + * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; + * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, + * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR + * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF + * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + +#include "core/plugin.h" +#include "utils/error.h" +#include "utils/strbuf.h" + +#include + +#include +#include +#include + +#include + +/* + * private data types + */ + +typedef struct { + int prio; + sdb_strbuf_t *msg; + _Bool logged; +} sdb_error_ctx_t; +#define SDB_ERROR_INIT { -1, NULL, 1 } + +/* + * private variables + */ + +static sdb_error_ctx_t default_error_ctx = SDB_ERROR_INIT; + +static pthread_key_t error_ctx_key; +static _Bool error_ctx_key_initialized = 0; + +/* + * private helper functions + */ + +static void +sdb_error_ctx_destructor(void *p) +{ + sdb_error_ctx_t *ctx = p; + + if (! ctx) + return; + + sdb_strbuf_destroy(ctx->msg); + free(ctx); +} /* sdb_error_ctx_destructor */ + +static void +sdb_error_ctx_init(void) +{ + if (error_ctx_key_initialized) + return; + + pthread_key_create(&error_ctx_key, sdb_error_ctx_destructor); + error_ctx_key_initialized = 1; +} /* sdb_error_init */ + +static sdb_error_ctx_t * +sdb_error_ctx_create(void) +{ + sdb_error_ctx_t *ctx; + + ctx = malloc(sizeof(*ctx)); + if (! ctx) + return NULL; + + *ctx = default_error_ctx; + ctx->msg = sdb_strbuf_create(64); + if (! ctx->msg) { + free(ctx); + return NULL; + } + + if (! error_ctx_key_initialized) + sdb_error_ctx_init(); + pthread_setspecific(error_ctx_key, ctx); + return ctx; +} /* sdb_error_ctx_create */ + +static sdb_error_ctx_t * +sdb_error_get_ctx(void) +{ + sdb_error_ctx_t *ctx; + + if (! error_ctx_key_initialized) + sdb_error_ctx_init(); + ctx = pthread_getspecific(error_ctx_key); + + if (! ctx) + ctx = sdb_error_ctx_create(); + if (! ctx) + return NULL; + return ctx; +} /* sdb_error_get_ctx */ + +static int +sdb_error_vprintf(const char *fmt, va_list ap) +{ + sdb_error_ctx_t *ctx; + + ctx = sdb_error_get_ctx(); + if (! ctx) + return -1; + + ctx->logged = 0; + return (int)sdb_strbuf_vsprintf(ctx->msg, fmt, ap); +} /* sdb_error_vprintf */ + +static int +sdb_error_vappend(const char *fmt, va_list ap) +{ + sdb_error_ctx_t *ctx; + + ctx = sdb_error_get_ctx(); + if (! ctx) + return -1; + + ctx->logged = 0; + return (int)sdb_strbuf_vappend(ctx->msg, fmt, ap); +} /* sdb_error_vappend */ + +static int +sdb_do_log(int prio) +{ + sdb_error_ctx_t *ctx; + int ret; + + ctx = sdb_error_get_ctx(); + if (! ctx) + return -1; + + if (prio >= 0) + ctx->prio = prio; + + if (ctx->logged) + return 0; + + ret = sdb_plugin_log(prio, sdb_strbuf_string(ctx->msg)); + ctx->logged = 1; + return ret; +} /* sdb_do_log */ + +/* + * public API + */ + +int +sdb_log(int prio, const char *fmt, ...) +{ + va_list ap; + int ret; + + va_start(ap, fmt); + ret = sdb_error_vprintf(fmt, ap); + va_end(ap); + + if (ret > 0) + sdb_do_log(prio); + return ret; +} /* sdb_log */ + +int +sdb_error_set(const char *fmt, ...) +{ + va_list ap; + int ret; + + va_start(ap, fmt); + ret = sdb_error_vprintf(fmt, ap); + va_end(ap); + + return ret; +} /* sdb_error_set */ + +int +sdb_error_append(const char *fmt, ...) +{ + va_list ap; + int ret; + + va_start(ap, fmt); + ret = sdb_error_vappend(fmt, ap); + va_end(ap); + + return ret; +} /* sdb_error_append */ + +int +sdb_error_chomp(void) +{ + sdb_error_ctx_t *ctx; + + ctx = sdb_error_get_ctx(); + if (! ctx) + return -1; + + sdb_strbuf_chomp(ctx->msg); + return 0; +} /* sdb_error_chomp */ + +int +sdb_error_log(int prio) +{ + return sdb_do_log(prio); +} /* sdb_error_log */ + +const char * +sdb_error_get(void) +{ + sdb_error_ctx_t *ctx; + + ctx = sdb_error_get_ctx(); + if (! ctx) + return "success"; + return sdb_strbuf_string(ctx->msg); +} /* sdb_error_get */ + +int +sdb_error_get_prio(void) +{ + sdb_error_ctx_t *ctx; + + ctx = sdb_error_get_ctx(); + if (! ctx) + return -1; + return ctx->prio; +} /* sdb_error_get_prio */ + +char * +sdb_strerror(int errnum, char *strerrbuf, size_t buflen) +{ +#if STRERROR_R_CHAR_P + { + char *tmp = strerror_r(errnum, strerrbuf, buflen); + if (*strerrbuf = '\0') { + if (tmp && (tmp != strerrbuf) && (*tmp != '\0')) + strncpy(strerrbuf, tmp, buflen); + else + snprintf(strerrbuf, buflen, "unknown error #%i " + "(strerror_r(3) did not return an error message)", + errnum); + } + } +#else + if (strerror_r(errnum, strerrbuf, buflen)) + snprintf(strerrbuf, buflen, "unknown error #%i " + "(strerror_r(3) failed)", errnum); +#endif + + strerrbuf[buflen - 1] = '\0'; + return strerrbuf; +} /* sdb_strerror */ + +/* vim: set tw=78 sw=4 ts=4 noexpandtab : */ + diff --git a/src/utils/proto.c b/src/utils/proto.c index 85a4cb8..29783ef 100644 --- a/src/utils/proto.c +++ b/src/utils/proto.c @@ -25,8 +25,8 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "utils/error.h" #include "utils/proto.h" -#include "core/error.h" #include #include diff --git a/src/utils/unixsock.c b/src/utils/unixsock.c index 256cf9f..59bf563 100644 --- a/src/utils/unixsock.c +++ b/src/utils/unixsock.c @@ -25,8 +25,8 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ +#include "utils/error.h" #include "utils/unixsock.h" -#include "core/error.h" #include #include