Code

plugin: Make sdb_plugin_info_t public.
[sysdb.git] / src / utils / error.c
index fc360ebaa98b3f0741907e0d85fa79744f59add3..f40e31ada656f24e4719783bbe279cf49cfb7be2 100644 (file)
  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  */
 
+#if HAVE_CONFIG_H
+#      include "config.h"
+#endif /* HAVE_CONFIG_H */
+
 #include "utils/error.h"
 #include "utils/strbuf.h"
 
@@ -56,6 +60,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
  */
@@ -162,9 +168,12 @@ sdb_do_log(int prio)
        if (ctx->logged)
                return 0;
 
-       ret = fprintf(stderr, "[%s] %s\n",
-                       SDB_LOG_PRIO_TO_STRING(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 */
@@ -173,6 +182,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, ...)
 {
@@ -214,6 +229,19 @@ sdb_error_append(const char *fmt, ...)
        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)
 {