ctdb-logging: Split ringbuffer handling code from ctdb_collect_log
authorAmitay Isaacs <amitay@gmail.com>
Tue, 10 Jun 2014 04:52:19 +0000 (14:52 +1000)
committerMartin Schwenke <martins@samba.org>
Thu, 12 Jun 2014 03:40:10 +0000 (05:40 +0200)
Signed-off-by: Amitay Isaacs <amitay@gmail.com>
Reviewed-by: Martin Schwenke <martin@meltin.net>
ctdb/common/ctdb_logging.c
ctdb/include/ctdb_private.h

index baaf700799315b16500742a3c9d6bd429ec5d75d..03ac26895810f44e2329dfb9c035472282620fa7 100644 (file)
@@ -103,7 +103,8 @@ void ctdb_log_ringbuffer_free(void)
        log_ringbuf_size = 0;
 }
 
-void ctdb_collect_log(struct ctdb_context *ctdb, struct ctdb_get_log_addr *log_addr)
+TDB_DATA ctdb_log_ringbuffer_collect_log(TALLOC_CTX *mem_ctx,
+                                        enum debug_level max_level)
 {
        TDB_DATA data;
        FILE *f;
@@ -118,14 +119,15 @@ void ctdb_collect_log(struct ctdb_context *ctdb, struct ctdb_get_log_addr *log_a
        /* dump to a file, then send the file as a blob */
        f = tmpfile();
        if (f == NULL) {
-               DEBUG(DEBUG_ERR,(__location__ " Unable to open tmpfile - %s\n", strerror(errno)));
-               return;
+               DEBUG(DEBUG_ERR,(__location__ " Unable to open tmpfile - %s\n",
+                                strerror(errno)));
+               return tdb_null;
        }
 
        for (i=0; i<ringbuf_count; i++) {
                tmp_entry = (first_entry + i) % log_ringbuf_size;
 
-               if (log_entries[tmp_entry].level > log_addr->level) {
+               if (log_entries[tmp_entry].level > max_level) {
                        continue;
                }
 
@@ -143,23 +145,35 @@ void ctdb_collect_log(struct ctdb_context *ctdb, struct ctdb_get_log_addr *log_a
        if (fsize < 0) {
                fclose(f);
                DEBUG(DEBUG_ERR, ("Cannot get file size for log entries\n"));
-               return;
+               return tdb_null;
        }
        rewind(f);
        data.dptr = talloc_size(NULL, fsize);
        if (data.dptr == NULL) {
                fclose(f);
-               CTDB_NO_MEMORY_VOID(ctdb, data.dptr);
+               DEBUG(DEBUG_ERR, (__location__ " Memory allocation error\n"));
+               return tdb_null;
        }
        data.dsize = fread(data.dptr, 1, fsize, f);
        fclose(f);
 
        DEBUG(DEBUG_ERR,("Marshalling log entries into a blob of %d bytes\n", (int)data.dsize));
 
+       return data;
+}
+
+void ctdb_collect_log(struct ctdb_context *ctdb, struct ctdb_get_log_addr *log_addr)
+{
+       TDB_DATA data;
+
+       data = ctdb_log_ringbuffer_collect_log(ctdb, log_addr->level);
+
        DEBUG(DEBUG_ERR,("Send log to %d:%d\n", (int)log_addr->pnn, (int)log_addr->srvid));
        ctdb_client_send_message(ctdb, log_addr->pnn, log_addr->srvid, data);
 
-       talloc_free(data.dptr);
+       if (data.dptr) {
+               talloc_free(data.dptr);
+       }
 }
 
 int32_t ctdb_control_get_log(struct ctdb_context *ctdb, TDB_DATA addr)
index 9e87976419d78fb75382839aadbb0c473713ba18..0481e434297547565123a77489dd11feb13e7afb 100644 (file)
@@ -1458,6 +1458,8 @@ struct ctdb_get_log_addr {
 
 extern int log_ringbuf_size;
 
+TDB_DATA ctdb_log_ringbuffer_collect_log(TALLOC_CTX *mem_ctx,
+                                        enum debug_level max_level);
 void ctdb_collect_log(struct ctdb_context *ctdb, struct ctdb_get_log_addr *log_addr);
 void ctdb_clear_log(struct ctdb_context *ctdb);
 int32_t ctdb_control_get_log(struct ctdb_context *ctdb, TDB_DATA addr);