From: Sebastian Harl Date: Sat, 20 Jul 2013 00:06:35 +0000 (-0700) Subject: plugin: Added formatting log functions. X-Git-Tag: sysdb-0.1.0~401 X-Git-Url: https://git.tokkee.org/?a=commitdiff_plain;h=01e2000a3c5befdf4068fd2091da746eff1f7a38;p=sysdb.git plugin: Added formatting log functions. --- diff --git a/src/core/plugin.c b/src/core/plugin.c index 0a69bff..c248d37 100644 --- a/src/core/plugin.c +++ b/src/core/plugin.c @@ -30,6 +30,7 @@ #include "core/error.h" #include "core/time.h" #include "utils/llist.h" +#include "utils/strbuf.h" #include @@ -661,6 +662,9 @@ sdb_plugin_log(int prio, const char *msg) sdb_llist_iter_t *iter; int ret = -1; + if (! msg) + return 0; + if (! log_list) return fprintf(stderr, "[%s] %s\n", SDB_LOG_PRIO_TO_STRING(prio), msg); @@ -681,5 +685,46 @@ sdb_plugin_log(int prio, const char *msg) return ret; } /* sdb_plugin_log */ +int +sdb_plugin_vlogf(int prio, const char *fmt, va_list ap) +{ + sdb_strbuf_t *buf; + int ret; + + if (! fmt) + return 0; + + buf = sdb_strbuf_create(64); + if (! buf) { + ret = fprintf(stderr, "[%s] ", SDB_LOG_PRIO_TO_STRING(prio)); + ret += vfprintf(stderr, fmt, ap); + return ret; + } + + if (sdb_strbuf_vsprintf(buf, fmt, ap) < 0) { + sdb_strbuf_destroy(buf); + return -1; + } + + ret = sdb_plugin_log(prio, sdb_strbuf_string(buf)); + sdb_strbuf_destroy(buf); + return ret; +} /* sdb_plugin_vlogf */ + +int +sdb_plugin_logf(int prio, const char *fmt, ...) +{ + va_list ap; + int ret; + + if (! fmt) + return 0; + + va_start(ap, fmt); + ret = sdb_plugin_vlogf(prio, fmt, ap); + va_end(ap); + return ret; +} /* sdb_plugin_logf */ + /* vim: set tw=78 sw=4 ts=4 noexpandtab : */ diff --git a/src/include/core/plugin.h b/src/include/core/plugin.h index 79a171a..bef5e0b 100644 --- a/src/include/core/plugin.h +++ b/src/include/core/plugin.h @@ -34,6 +34,8 @@ #include "liboconfig/oconfig.h" +#include + #ifdef __cplusplus extern "C" { #endif @@ -240,6 +242,15 @@ sdb_plugin_collector_loop(sdb_plugin_loop_t *loop); int sdb_plugin_log(int prio, const char *msg); +/* + * sdb_plugin_logf: + * Log a formatted message. See sdb_plugin_log for more information. + */ +int +sdb_plugin_vlogf(int prio, const char *fmt, va_list ap); +int +sdb_plugin_logf(int prio, const char *fmt, ...); + #ifdef __cplusplus } /* extern "C" */ #endif