Code

plugin: Added formatting log functions.
authorSebastian Harl <sh@tokkee.org>
Sat, 20 Jul 2013 00:06:35 +0000 (17:06 -0700)
committerSebastian Harl <sh@tokkee.org>
Sat, 20 Jul 2013 00:06:35 +0000 (17:06 -0700)
src/core/plugin.c
src/include/core/plugin.h

index 0a69bff2364a50e2b7566e4f4d0c4aaa6ec5b74c..c248d37eff6c15f9abce00d3d0b53b0b636a580e 100644 (file)
@@ -30,6 +30,7 @@
 #include "core/error.h"
 #include "core/time.h"
 #include "utils/llist.h"
+#include "utils/strbuf.h"
 
 #include <assert.h>
 
@@ -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 : */
 
index 79a171ab16330ed047b2aef2afc043e8217f4d98..bef5e0b4ccb35251f261ff8af4defe54b898d2bf 100644 (file)
@@ -34,6 +34,8 @@
 
 #include "liboconfig/oconfig.h"
 
+#include <stdarg.h>
+
 #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