libctdb: connect TDB logging to our logging
authorRusty Russell <rusty@rustcorp.com.au>
Tue, 8 Jun 2010 08:39:42 +0000 (18:09 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Tue, 8 Jun 2010 08:39:42 +0000 (18:09 +0930)
A simple connector function, made a bit more complex because TDB adds
a '\n' and we don't.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
libctdb/ctdb.c
libctdb/libctdb_private.h
libctdb/logging.c

index 8b248f18ec8e7983ed5eabfa9dae6d99f5a99554..df429493573bad30a7b80dd8c81a9c8e21726d3e 100644 (file)
@@ -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;
index 51b9305a086e8c33f36972777bc5dfcf6d4a7a26..8ecfb0ac7dc14b976187c218048db19132657df6 100644 (file)
@@ -7,6 +7,7 @@
 #include <ctdb.h>
 #include <ctdb_protocol.h>
 #include <syslog.h>
+#include <tdb.h>
 
 #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 */
index 05aaa035ee27a6135fdc54b81c60dc860a68b4e1..83f47742d75a108c96c26c6b1ee5ebae95a3880a 100644 (file)
@@ -20,6 +20,7 @@
 #include <errno.h>
 #include <ctdb.h>
 #include <string.h>
+#include <tdb.h>
 #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)
 {