libctdb: add logging infrastructure
authorRusty Russell <rusty@rustcorp.com.au>
Fri, 4 Jun 2010 10:57:03 +0000 (20:27 +0930)
committerRusty Russell <rusty@rustcorp.com.au>
Fri, 4 Jun 2010 10:57:03 +0000 (20:27 +0930)
This is based on Ronnie's work, merged with mine.  That means
errors are all my fault.

Differences from Ronnie's:
1) use syslog's LOG_ levels directly.
2) typesafe arg to log function, and use it (eg stderr) in helper function.
3) store fn in ctdb context, and expose ctdb_log_level directly thru API.

Signed-off-by: Rusty Russell <rusty@rustcorp.com.au>
Makefile.in
include/ctdb.h
libctdb/ctdb.c
libctdb/libctdb_private.h
libctdb/logging.c [new file with mode: 0644]
libctdb/tst.c

index dd7864ea5230a40a64e4d79dfd1d62f7b4077da6..d2e0dea921549934d4b1867eb413dcfb9d0665e7 100755 (executable)
@@ -47,7 +47,8 @@ CTDB_COMMON_OBJ =  common/ctdb_io.o common/ctdb_util.o \
        common/ctdb_logging.c
 
 CTDB_LIB_OBJ = libctdb/ctdb.o libctdb/io_elem.o libctdb/local_tdb.o \
-       libctdb/messages.o libctdb/sync.o libctdb/control.o
+       libctdb/messages.o libctdb/sync.o libctdb/control.o \
+       libctdb/logging.o
 
 CTDB_TCP_OBJ = tcp/tcp_connect.o tcp/tcp_io.o tcp/tcp_init.o
 
index 5ad8736a7eee89a7516eb624b2ad6ec3c1dafadd..1d0a08611c1e6614602f19938b0338ccc026ee8c 100644 (file)
 #include <sys/types.h>
 #include <stdint.h>
 #include <stdbool.h>
+#include <stdarg.h>
+#include <stdio.h>
 #include <tdb.h>
 
+/* The type of the first arg should match the arg given to ctdb_connect() */
+typedef void (*ctdb_log_fn_t)(void *log_priv,
+                             int severity, const char *format, va_list ap);
+
+
 /* All *_send() functions are guaranteed to be non-blocking and fully
  * asynchronous.  The non-_send variants are synchronous.
  */
@@ -34,7 +41,8 @@
  * Returns a ctdb context if successful or NULL.
  *
  */
-struct ctdb_connection *ctdb_connect(const char *addr);
+struct ctdb_connection *ctdb_connect(const char *addr,
+                                    ctdb_log_fn_t log_fn, void *log_priv);
 
 int ctdb_get_fd(struct ctdb_connection *ctdb);
 
@@ -212,12 +220,26 @@ int ctdb_getrecmaster(struct ctdb_connection *ctdb,
 int ctdb_cancel(struct ctdb_connection *ctdb, struct ctdb_request *req);
 
 
+/*
+ * functions for logging errors
+ */
+extern int ctdb_log_level; /* LOG_WARNING and above by default. */
+void ctdb_log_file(FILE *, int severity, const char *format, va_list ap);
+
+
 /* These ugly macro wrappers make the callbacks typesafe. */
 #include <ctdb_typesafe_cb.h>
 #define ctdb_sendcb(cb, cbdata)                                                \
         typesafe_cb_preargs(void, (cb), (cbdata),                      \
                             struct ctdb_connection *, struct ctdb_request *)
 
+#define ctdb_connect(addr, log, logpriv)                               \
+       ctdb_connect((addr),                                            \
+                    typesafe_cb_postargs(void, (log), (logpriv),       \
+                                         int, const char *, va_list),  \
+                    (logpriv))
+
+
 #define ctdb_attachdb_send(ctdb, name, persistent, tdb_flags, cb, cbdata) \
        ctdb_attachdb_send((ctdb), (name), (persistent), (tdb_flags),   \
                           ctdb_sendcb((cb), (cbdata)), (cbdata))
index 771afec6cdde915e054d18bbaa32eef3f1ef9dff..b6ea121c5ecf1772d7bdc4b07b7192ab34d9696d 100644 (file)
@@ -34,6 +34,7 @@
 /* Remove type-safety macros. */
 #undef ctdb_attachdb_send
 #undef ctdb_readrecordlock_async
+#undef ctdb_connect
 
 /* FIXME: Could be in shared util code with rest of ctdb */
 static void close_noerr(int fd)
@@ -78,7 +79,8 @@ static void set_pnn(struct ctdb_connection *ctdb,
        ctdb_request_free(ctdb, req);
 }
 
-struct ctdb_connection *ctdb_connect(const char *addr)
+struct ctdb_connection *ctdb_connect(const char *addr,
+                                    ctdb_log_fn_t log_fn, void *log_priv)
 {
        struct ctdb_connection *ctdb;
        struct sockaddr_un sun;
@@ -92,6 +94,8 @@ struct ctdb_connection *ctdb_connect(const char *addr)
        ctdb->message_handlers = NULL;
        ctdb->next_id = 0;
        ctdb->broken = false;
+       ctdb->log = log_fn;
+       ctdb->log_priv = log_priv;
 
        memset(&sun, 0, sizeof(sun));
        sun.sun_family = AF_UNIX;
index b3486d2070f90eec161b526570e394a0051fcd87..9e8783e533a35ca3818b89c8a167aced93753325 100644 (file)
@@ -6,11 +6,25 @@
 #include <stdlib.h>
 #include <ctdb.h>
 #include <ctdb_protocol.h>
+#include <syslog.h>
 
 #ifndef offsetof
 #define offsetof(t,f) ((unsigned int)&((t *)0)->f)
 #endif
 
+#ifndef COLD_ATTRIBUTE
+#if (__GNUC__ > 4) || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3)
+#define COLD_ATTRIBUTE __attribute__((cold))
+#else
+#define COLD_ATTRIBUTE
+#endif
+#endif /* COLD_ATTRIBUTE */
+
+#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;
 
@@ -55,6 +69,9 @@ struct ctdb_connection {
        struct message_handler_info *message_handlers;
        /* PNN of this ctdb: valid by the time we do our first db connection. */
        uint32_t pnn;
+       /* Extra logging. */
+       ctdb_log_fn_t log;
+       void *log_priv;
 };
 
 /* ctdb.c */
diff --git a/libctdb/logging.c b/libctdb/logging.c
new file mode 100644 (file)
index 0000000..05aaa03
--- /dev/null
@@ -0,0 +1,56 @@
+/*
+   logging wrapper for libctdb
+
+   Copyright (C) Ronnie Sahlberg 2010
+
+   This program is free software; you can redistribute it and/or modify
+   it under the terms of the GNU General Public License as published by
+   the Free Software Foundation; either version 3 of the License, or
+   (at your option) any later version.
+
+   This program is distributed in the hope that it will be useful,
+   but WITHOUT ANY WARRANTY; without even the implied warranty of
+   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+   GNU General Public License for more details.
+
+   You should have received a copy of the GNU General Public License
+   along with this program; if not, see <http://www.gnu.org/licenses/>.
+*/
+#include <stdio.h>
+#include <errno.h>
+#include <ctdb.h>
+#include <string.h>
+#include "libctdb_private.h"
+
+int ctdb_log_level = LOG_WARNING;
+
+void ctdb_do_debug(struct ctdb_connection *ctdb,
+                  int severity, const char *format, ...)
+{
+        va_list ap;
+
+        va_start(ap, format);
+       ctdb->log(ctdb->log_priv, severity, format, ap);
+        va_end(ap);
+}
+
+/* Convenient log helper. */
+void ctdb_log_file(FILE *outf, int priority, const char *format, va_list ap)
+{
+       fprintf(outf, "%s:",
+               priority == LOG_EMERG ? "EMERG" :
+               priority == LOG_ALERT ? "ALERT" :
+               priority == LOG_CRIT ? "CRIT" :
+               priority == LOG_ERR ? "ERR" :
+               priority == LOG_WARNING ? "WARNING" :
+               priority == LOG_NOTICE ? "NOTICE" :
+               priority == LOG_INFO ? "INFO" :
+               priority == LOG_DEBUG ? "DEBUG" :
+               "Unknown Error Level");
+
+       vfprintf(outf, format, ap);
+       if (priority == LOG_ERR) {
+               fprintf(outf, " (%s)", strerror(errno));
+       }
+       fprintf(outf, "\n");
+}
index 461c81367a1c92bac18a08be405f92a37000d7bb..530125c792ab4d4ec38672b59c37be42e930ab66 100644 (file)
@@ -137,7 +137,8 @@ int main(int argc, char *argv[])
        bool rrl_cb_called = false;
 
 
-       ctdb_connection = ctdb_connect("/tmp/ctdb.socket");
+       ctdb_connection = ctdb_connect("/tmp/ctdb.socket",
+                                      ctdb_log_file, stderr);
        if (!ctdb_connection)
                err(1, "Connecting to /tmp/ctdb.socket");