From 44a0aa91ac18582aa7415943dc37d06220576443 Mon Sep 17 00:00:00 2001 From: Sebastian Harl Date: Tue, 10 Dec 2013 19:13:37 +0100 Subject: [PATCH] error utils: Make the logger callback configurable. Don't depend on the plugin module anymore. Rather, add sdb_error_set_logger() which may be used to set a callback to be used for all logging. Default to printing to stderr. sysdbd now uses that to register sdb_plugin_log as logger callback. --- src/daemon/sysdbd.c | 2 ++ src/include/utils/error.h | 9 +++++++++ src/utils/error.c | 16 ++++++++++++++-- 3 files changed, 25 insertions(+), 2 deletions(-) diff --git a/src/daemon/sysdbd.c b/src/daemon/sysdbd.c index 6952c1c..21350f3 100644 --- a/src/daemon/sysdbd.c +++ b/src/daemon/sysdbd.c @@ -186,6 +186,8 @@ main(int argc, char **argv) struct sigaction sa_intterm; int status; + sdb_error_set_logger(sdb_plugin_log); + while (42) { int opt = getopt(argc, argv, "C:DhV"); diff --git a/src/include/utils/error.h b/src/include/utils/error.h index aaa288c..e170a7d 100644 --- a/src/include/utils/error.h +++ b/src/include/utils/error.h @@ -66,6 +66,15 @@ enum { : ((prio) == SDB_LOG_INFO) ? "INFO" \ : ((prio) == SDB_LOG_DEBUG) ? "DEBUG" : "UNKNOWN") +/* + * sdb_error_set_logger: + * Set the logging callback to be used for logging messages. By default (or + * when explicitely setting the logger to NULL), logs will be written to the + * stderr channel. + */ +void +sdb_error_set_logger(int (*f)(int, const char *)); + /* * sdb_log: * Log the specified message. The string will be formatted in printf-style diff --git a/src/utils/error.c b/src/utils/error.c index 5b9b448..6bf64c2 100644 --- a/src/utils/error.c +++ b/src/utils/error.c @@ -25,7 +25,6 @@ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. */ -#include "core/plugin.h" #include "utils/error.h" #include "utils/strbuf.h" @@ -57,6 +56,8 @@ 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; +static int (*logger)(int prio, const char *msg) = NULL; + /* * private helper functions */ @@ -163,7 +164,12 @@ sdb_do_log(int prio) if (ctx->logged) return 0; - ret = sdb_plugin_log(prio, sdb_strbuf_string(ctx->msg)); + if (logger) + ret = logger(prio, sdb_strbuf_string(ctx->msg)); + else + ret = fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), + sdb_strbuf_string(ctx->msg)); + ctx->logged = 1; return ret; } /* sdb_do_log */ @@ -172,6 +178,12 @@ sdb_do_log(int prio) * public API */ +void +sdb_error_set_logger(int (*f)(int, const char *)) +{ + logger = f; +} /* sdb_error_set_logger */ + int sdb_log(int prio, const char *fmt, ...) { -- 2.30.2