[libvirt] [PATCH 1/5] log: handler: Add new API to append to logging files

Peter Krempa pkrempa at redhat.com
Tue Jun 7 15:04:26 UTC 2016


For logging one-shot entries to the VM log file it's quite a waste to
hold open the file descriptor for logging that is provided by the
current API.

This new API will be ideal for logging one-shot entries to the file
e.g. at the point when we shut the VM down rather than having to add the
whole file-descriptor infrastructure.

Additionally this will allow to add the messages even after restart of
libvirtd since virtlogd doesn't allow to obtain a regular context with
filedescriptors while the VM is still active.
---
 src/logging/log_handler.c | 50 +++++++++++++++++++++++++++++++++++++++++++++++
 src/logging/log_handler.h |  8 ++++++++
 2 files changed, 58 insertions(+)

diff --git a/src/logging/log_handler.c b/src/logging/log_handler.c
index 4c08223..a8cb6cf 100644
--- a/src/logging/log_handler.c
+++ b/src/logging/log_handler.c
@@ -513,6 +513,56 @@ virLogHandlerDomainReadLogFile(virLogHandlerPtr handler,
 }


+int
+virLogHandlerDomainAppendLogFile(virLogHandlerPtr handler,
+                                 const char *driver ATTRIBUTE_UNUSED,
+                                 const unsigned char *domuuid ATTRIBUTE_UNUSED,
+                                 const char *domname ATTRIBUTE_UNUSED,
+                                 const char *path,
+                                 const char *message,
+                                 unsigned int flags)
+{
+    size_t i;
+    virRotatingFileWriterPtr writer = NULL;
+    virRotatingFileWriterPtr newwriter = NULL;
+    int ret = -1;
+
+    virCheckFlags(0, -1);
+
+    VIR_DEBUG("Appending to log '%s' message: '%s'", path, message);
+
+    virObjectLock(handler);
+
+    for (i = 0; i < handler->nfiles; i++) {
+        if (STREQ(virRotatingFileWriterGetPath(handler->files[i]->file), path)) {
+            writer = handler->files[i]->file;
+            break;
+        }
+    }
+
+    if (!writer) {
+        if (!(newwriter = virRotatingFileWriterNew(path,
+                                                   DEFAULT_FILE_SIZE,
+                                                   DEFAULT_MAX_BACKUP,
+                                                   false,
+                                                   DEFAULT_MODE)))
+            goto cleanup;
+
+        writer = newwriter;
+    }
+
+    if (virRotatingFileWriterAppend(writer, message, strlen(message)) < 0)
+        goto cleanup;
+
+    ret = 0;
+
+ cleanup:
+    virRotatingFileWriterFree(newwriter);
+    virObjectUnlock(handler);
+    return ret;
+}
+
+
 virJSONValuePtr
 virLogHandlerPreExecRestart(virLogHandlerPtr handler)
 {
diff --git a/src/logging/log_handler.h b/src/logging/log_handler.h
index 54a9cd9..4607e45 100644
--- a/src/logging/log_handler.h
+++ b/src/logging/log_handler.h
@@ -65,6 +65,14 @@ char *virLogHandlerDomainReadLogFile(virLogHandlerPtr handler,
                                      size_t maxlen,
                                      unsigned int flags);

+int virLogHandlerDomainAppendLogFile(virLogHandlerPtr handler,
+                                     const char *driver,
+                                     const unsigned char *domuuid,
+                                     const char *domname,
+                                     const char *path,
+                                     const char *message,
+                                     unsigned int flags);
+
 virJSONValuePtr virLogHandlerPreExecRestart(virLogHandlerPtr handler);

 #endif /** __VIR_LOG_HANDLER_H__ */
-- 
2.8.3




More information about the libvir-list mailing list