From e169d689b98fa965a147a0ce9fa62ec3e7ecd5df Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 8 Jun 2010 18:09:42 +0930 Subject: [PATCH] libctdb: connect TDB logging to our logging A simple connector function, made a bit more complex because TDB adds a '\n' and we don't. Signed-off-by: Rusty Russell (This used to be ctdb commit ae5b89dca00ca080c70868430fa54ba07bd6f5f4) --- ctdb/libctdb/ctdb.c | 12 ++++---- ctdb/libctdb/libctdb_private.h | 13 +++++++-- ctdb/libctdb/logging.c | 52 ++++++++++++++++++++++++++++++++++ 3 files changed, 68 insertions(+), 9 deletions(-) diff --git a/ctdb/libctdb/ctdb.c b/ctdb/libctdb/ctdb.c index 8b248f18ec8..df429493573 100644 --- a/ctdb/libctdb/ctdb.c +++ b/ctdb/libctdb/ctdb.c @@ -494,6 +494,7 @@ struct ctdb_db *ctdb_attachdb_recv(struct ctdb_connection *ctdb, struct ctdb_reply_control *reply; struct ctdb_db *db = req->priv_data; uint32_t tdb_flags = db->tdb_flags; + struct tdb_logging_context log; /* Never sent the dbpath request? We've failed. */ if (!dbpath_req) { @@ -515,8 +516,10 @@ struct ctdb_db *ctdb_attachdb_recv(struct ctdb_connection *ctdb, tdb_flags = db->persistent ? TDB_DEFAULT : TDB_NOSYNC; tdb_flags |= TDB_DISALLOW_NESTING; - /* FIXME: Setup logging to go through our logging. */ - db->tdb = tdb_open((char *)reply->data, 0, tdb_flags, O_RDWR, 0); + log.log_fn = ctdb_tdb_log_bridge; + log.log_private = ctdb; + db->tdb = tdb_open_ex((char *)reply->data, 0, tdb_flags, O_RDWR, 0, + &log, NULL); if (db->tdb == NULL) { DEBUG(db->ctdb, LOG_ERR, "ctdb_attachdb_recv: failed to tdb_open %s", @@ -844,10 +847,7 @@ bool ctdb_writerecord(struct ctdb_db *ctdb_db, DEBUG(ctdb_db->ctdb, LOG_ALERT, "ctdb_writerecord: record changed under lock?"); break; - default: - /* FIXME: replace with proper tdb logging. */ - DEBUG(ctdb_db->ctdb, LOG_CRIT, - "ctdb_writerecord: tdb error."); + default: /* TDB already logged. */ break; } return false; diff --git a/ctdb/libctdb/libctdb_private.h b/ctdb/libctdb/libctdb_private.h index 51b9305a086..8ecfb0ac7dc 100644 --- a/ctdb/libctdb/libctdb_private.h +++ b/ctdb/libctdb/libctdb_private.h @@ -7,6 +7,7 @@ #include #include #include +#include #ifndef offsetof #define offsetof(t,f) ((unsigned int)&((t *)0)->f) @@ -22,9 +23,6 @@ #define DEBUG(ctdb, lvl, format, args...) do { if (lvl <= ctdb_log_level) { ctdb_do_debug(ctdb, lvl, format , ## args ); }} while(0) -void ctdb_do_debug(struct ctdb_connection *, int, const char *format, ...) - PRINTF_ATTRIBUTE(3, 4) COLD_ATTRIBUTE; - struct message_handler_info; struct ctdb_reply_call; @@ -92,4 +90,13 @@ struct ctdb_reply_control *unpack_reply_control(struct ctdb_connection *ctdb, void ctdb_cancel_callback(struct ctdb_connection *ctdb, struct ctdb_request *req, void *unused); + +/* logging.c */ +void ctdb_tdb_log_bridge(struct tdb_context *tdb, + enum tdb_debug_level level, + const char *format, ...) PRINTF_ATTRIBUTE(3, 4); + +void ctdb_do_debug(struct ctdb_connection *, int, const char *format, ...) + PRINTF_ATTRIBUTE(3, 4) COLD_ATTRIBUTE; + #endif /* _LIBCTDB_PRIVATE_H */ diff --git a/ctdb/libctdb/logging.c b/ctdb/libctdb/logging.c index 05aaa035ee2..83f47742d75 100644 --- a/ctdb/libctdb/logging.c +++ b/ctdb/libctdb/logging.c @@ -20,6 +20,7 @@ #include #include #include +#include #include "libctdb_private.h" int ctdb_log_level = LOG_WARNING; @@ -34,6 +35,57 @@ void ctdb_do_debug(struct ctdb_connection *ctdb, va_end(ap); } +/* Attach tdb logging to our ctdb logging. */ +void ctdb_tdb_log_bridge(struct tdb_context *tdb, + enum tdb_debug_level level, + const char *format, ...) +{ + va_list ap; + int sev; + struct ctdb_connection *ctdb = tdb_get_logging_private(tdb); + char *newformat; + + switch (level) { + case TDB_DEBUG_FATAL: + sev = LOG_CRIT; + break; + case TDB_DEBUG_ERROR: + sev = LOG_ERR; + break; + case TDB_DEBUG_WARNING: + sev = LOG_WARNING; + break; + case TDB_DEBUG_TRACE: + sev = LOG_DEBUG; + break; + default: + sev = LOG_CRIT; + } + + if (sev > ctdb_log_level) { + return; + } + + newformat = malloc(sizeof("TDB error: ") + strlen(format)); + if (!newformat) { + DEBUG(ctdb, LOG_ERR, + "memory allocation failure reporting tdb error %s", + format); + return; + } + + /* Prepend TDB error: and remove \n */ + strcpy(newformat, "TDB error: "); + strcat(newformat, format); + if (newformat[strlen(newformat)-1] == '\n') + newformat[strlen(newformat)-1] = '\0'; + + va_start(ap, format); + ctdb->log(ctdb->log_priv, sev, newformat, ap); + va_end(ap); + free(newformat); +} + /* Convenient log helper. */ void ctdb_log_file(FILE *outf, int priority, const char *format, va_list ap) { -- 2.34.1