X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Futils%2Ferror.c;h=f40e31ada656f24e4719783bbe279cf49cfb7be2;hb=9c2c414cffe741f3c0737d6af02a059766ce52fd;hp=fc360ebaa98b3f0741907e0d85fa79744f59add3;hpb=bcf49768cccf7b536d6c4c741afce40b817b3309;p=sysdb.git diff --git a/src/utils/error.c b/src/utils/error.c index fc360eb..f40e31a 100644 --- a/src/utils/error.c +++ b/src/utils/error.c @@ -25,6 +25,10 @@ * 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) {