From: Sebastian Harl Date: Tue, 10 Dec 2013 18:13:37 +0000 (+0100) Subject: error utils: Make the logger callback configurable. X-Git-Tag: sysdb-0.1.0~316 X-Git-Url: https://git.tokkee.org/?p=sysdb.git;a=commitdiff_plain;h=44a0aa91ac18582aa7415943dc37d06220576443 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. --- 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, ...) {