persistent: add a ctdb_db context to the ctdb_persistent_state struct.
[sahlberg/ctdb.git] / common / ctdb_logging.c
index efb47c6d9a959a7c13a8ac80703ec799b61a6760..dee4dfdb364eda731fea6512a83cb917ef418035 100644 (file)
 */
 
 #include "includes.h"
-#include "lib/events/events.h"
+#include "lib/tevent/tevent.h"
 #include "lib/tdb/include/tdb.h"
 #include "system/time.h"
 #include "../include/ctdb_private.h"
-#include "../include/ctdb.h"
+#include "../include/ctdb_client.h"
+
+int log_ringbuf_size;
+
+#define MAX_LOG_SIZE 128
+
+static int first_entry;
+static int last_entry;
 
 struct ctdb_log_entry {
        int32_t level;
        struct timeval t;
-       char *message;
+       char message[MAX_LOG_SIZE];
 };
 
-#define MAX_LOG_ENTRIES 500000
-static int first_entry;
-static int last_entry;
 
-static struct ctdb_log_entry log_entries[MAX_LOG_ENTRIES];
+static struct ctdb_log_entry *log_entries;
 
 /*
  * this function logs all messages for all levels to a ringbuffer
@@ -42,30 +46,33 @@ static struct ctdb_log_entry log_entries[MAX_LOG_ENTRIES];
 static void log_ringbuffer_v(const char *format, va_list ap)
 {
        int ret;
-       char *s = NULL;
 
-       if (log_entries[last_entry].message != NULL) {
-               free(log_entries[last_entry].message);
-               log_entries[last_entry].message = NULL;
+       if (log_entries == NULL && log_ringbuf_size != 0) {
+               /* Hope this works. We cant log anything if it doesnt anyway */
+               log_entries = malloc(sizeof(struct ctdb_log_entry) * log_ringbuf_size);
+       }
+       if (log_entries == NULL) {
+               return;
        }
 
-       ret = vasprintf(&s, format, ap);
+       log_entries[last_entry].message[0] = '\0';
+
+       ret = vsnprintf(&log_entries[last_entry].message[0], MAX_LOG_SIZE, format, ap);
        if (ret == -1) {
                return;
        }
 
        log_entries[last_entry].level = this_log_level;
        log_entries[last_entry].t = timeval_current();
-       log_entries[last_entry].message = s;
 
        last_entry++;
-       if (last_entry >= MAX_LOG_ENTRIES) {
+       if (last_entry >= log_ringbuf_size) {
                last_entry = 0;
        }
        if (first_entry == last_entry) {
                first_entry++;
        }
-       if (first_entry >= MAX_LOG_ENTRIES) {
+       if (first_entry >= log_ringbuf_size) {
                first_entry = 0;
        }
 }
@@ -102,9 +109,13 @@ static void ctdb_collect_log(struct ctdb_context *ctdb, struct ctdb_get_log_addr
                struct tm *tm;
                char tbuf[100];
 
+               if (log_entries == NULL) {
+                       break;
+               }
+
                if (log_entries[tmp_entry].level > log_addr->level) {
                        tmp_entry++;
-                       if (tmp_entry >= MAX_LOG_ENTRIES) {
+                       if (tmp_entry >= log_ringbuf_size) {
                                tmp_entry = 0;
                        }
                        continue;
@@ -118,7 +129,7 @@ static void ctdb_collect_log(struct ctdb_context *ctdb, struct ctdb_get_log_addr
                }
 
                tmp_entry++;
-               if (tmp_entry >= MAX_LOG_ENTRIES) {
+               if (tmp_entry >= log_ringbuf_size) {
                        tmp_entry = 0;
                }
        }
@@ -133,7 +144,7 @@ static void ctdb_collect_log(struct ctdb_context *ctdb, struct ctdb_get_log_addr
        DEBUG(DEBUG_ERR,("Marshalling log entries into a blob of %d bytes\n", (int)data.dsize));
 
        DEBUG(DEBUG_ERR,("Send log to %d:%d\n", (int)log_addr->pnn, (int)log_addr->srvid));
-       ctdb_send_message(ctdb, log_addr->pnn, log_addr->srvid, data);
+       ctdb_client_send_message(ctdb, log_addr->pnn, log_addr->srvid, data);
 
        talloc_free(data.dptr);
 }
@@ -146,14 +157,14 @@ int32_t ctdb_control_get_log(struct ctdb_context *ctdb, TDB_DATA addr)
        /* spawn a child process to marshall the huge log blob and send it back
           to the ctdb tool using a MESSAGE
        */
-       child = fork();
+       child = ctdb_fork(ctdb);
        if (child == (pid_t)-1) {
                DEBUG(DEBUG_ERR,("Failed to fork a log collector child\n"));
                return -1;
        }
 
        if (child == 0) {
-               if (switch_from_server_to_client(ctdb) != 0) {
+               if (switch_from_server_to_client(ctdb, "log-collector") != 0) {
                        DEBUG(DEBUG_CRIT, (__location__ "ERROR: failed to switch log collector child into client mode.\n"));
                        _exit(1);
                }