]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
log: handler: Add new API to append to logging files
authorPeter Krempa <pkrempa@redhat.com>
Tue, 7 Jun 2016 14:09:09 +0000 (16:09 +0200)
committerPeter Krempa <pkrempa@redhat.com>
Tue, 7 Jun 2016 16:10:29 +0000 (18:10 +0200)
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
src/logging/log_handler.h

index 4c082238201faaf3b73361040a3c98dfda3099b1..a8cb6cf878ab4b31a25fc42d679ed151c3deb071 100644 (file)
@@ -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)
 {
index 54a9cd9288eeaab4ed3a7ba52bef82a75d7e68b4..4607e4556ddc9fc64c321618d0b3dd7adf0b3697 100644 (file)
@@ -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__ */