summary | shortlog | log | commit | commitdiff | tree
raw | patch | inline | side by side (parent: 9ff971e)
raw | patch | inline | side by side (parent: 9ff971e)
author | Sebastian Harl <sh@tokkee.org> | |
Wed, 20 Mar 2013 06:29:48 +0000 (23:29 -0700) | ||
committer | Sebastian Harl <sh@tokkee.org> | |
Wed, 20 Mar 2013 06:29:48 +0000 (23:29 -0700) |
src/utils/error.c | patch | blob | history |
diff --git a/src/utils/error.c b/src/utils/error.c
index 6bb17d5821f5ea68259606cbc6bcd8312d55b04e..fc360ebaa98b3f0741907e0d85fa79744f59add3 100644 (file)
--- a/src/utils/error.c
+++ b/src/utils/error.c
*/
#include "utils/error.h"
+#include "utils/strbuf.h"
#include <pthread.h>
typedef struct {
int prio;
- char msg[SDB_MAX_ERROR];
+ sdb_strbuf_t *msg;
_Bool logged;
} sdb_error_ctx_t;
-#define SDB_ERROR_INIT { -1, "", 1 }
+#define SDB_ERROR_INIT { -1, NULL, 1 }
/*
* private variables
*/
static void
-sdb_error_ctx_destructor(void *ctx)
+sdb_error_ctx_destructor(void *p)
{
+ sdb_error_ctx_t *ctx = p;
+
if (! ctx)
return;
+
+ sdb_strbuf_destroy(ctx->msg);
free(ctx);
} /* sdb_error_ctx_destructor */
return NULL;
*ctx = default_error_ctx;
+ ctx->msg = sdb_strbuf_create(64);
+ if (! ctx->msg) {
+ free(ctx);
+ return NULL;
+ }
if (! error_ctx_key_initialized)
sdb_error_ctx_init();
} /* sdb_error_get_ctx */
static int
-sdb_error_clear(void)
+sdb_error_vprintf(const char *fmt, va_list ap)
{
sdb_error_ctx_t *ctx;
if (! ctx)
return -1;
- ctx->prio = -1;
- ctx->msg[0] = '\0';
- ctx->logged = 1;
- return 0;
-} /* sdb_error_clear */
+ ctx->logged = 0;
+ return (int)sdb_strbuf_vsprintf(ctx->msg, fmt, ap);
+} /* sdb_error_vprintf */
static int
sdb_error_vappend(const char *fmt, va_list ap)
{
sdb_error_ctx_t *ctx;
- size_t len;
ctx = sdb_error_get_ctx();
if (! ctx)
return -1;
- len = strlen(ctx->msg);
- if (len >= sizeof(ctx->msg))
- return 0; /* nothing written */
-
ctx->logged = 0;
- return vsnprintf(ctx->msg + len, sizeof(ctx->msg) - len, fmt, ap);
+ return (int)sdb_strbuf_vappend(ctx->msg, fmt, ap);
} /* sdb_error_vappend */
static int
return 0;
ret = fprintf(stderr, "[%s] %s\n",
- SDB_LOG_PRIO_TO_STRING(prio), ctx->msg);
+ SDB_LOG_PRIO_TO_STRING(prio),
+ sdb_strbuf_string(ctx->msg));
ctx->logged = 1;
return ret;
} /* sdb_do_log */
va_list ap;
int ret;
- if (sdb_error_clear())
- return -1;
-
va_start(ap, fmt);
- ret = sdb_error_vappend(fmt, ap);
+ ret = sdb_error_vprintf(fmt, ap);
va_end(ap);
- sdb_do_log(prio);
+ if (ret > 0)
+ sdb_do_log(prio);
return ret;
} /* sdb_log */
va_list ap;
int ret;
- if (sdb_error_clear())
- return -1;
-
va_start(ap, fmt);
- ret = sdb_error_vappend(fmt, ap);
+ ret = sdb_error_vprintf(fmt, ap);
va_end(ap);
return ret;
ctx = sdb_error_get_ctx();
if (! ctx)
return "success";
- return ctx->msg;
+ return sdb_strbuf_string(ctx->msg);
} /* sdb_error_get */
int