[libvirt] [PATCH] qemu: record timestamps in qemu guest log

Osier Yang jyang at redhat.com
Fri Oct 29 08:43:19 UTC 2010


Currently only record timestamps for domain start and shutdown, for
domain start, record timestamps before qemu command line, for domain
shutdown, just says it's shutting down.

* src/qemu/qemu_driver.c (qemudStartVMDaemon, qemudShutdownVMDaemon)
---
 src/qemu/qemu_driver.c |   62 ++++++++++++++++++++++++++++++++++++++++++++++-
 1 files changed, 60 insertions(+), 2 deletions(-)

diff --git a/src/qemu/qemu_driver.c b/src/qemu/qemu_driver.c
index 1eea3a9..89b4d11 100644
--- a/src/qemu/qemu_driver.c
+++ b/src/qemu/qemu_driver.c
@@ -3828,6 +3828,10 @@ static int qemudStartVMDaemon(virConnectPtr conn,
     char ebuf[1024];
     char *pidfile = NULL;
     int logfile = -1;
+    struct timeval cur_time;
+    struct tm time_info;
+    char timestr[100];
+    char *timestamp;
     qemuDomainObjPrivatePtr priv = vm->privateData;

     struct qemudHookData hookData;
@@ -4015,7 +4019,27 @@ static int qemudStartVMDaemon(virConnectPtr conn,
             goto cleanup;
     }

+    gettimeofday(&cur_time, NULL);
+    localtime_r(&cur_time.tv_sec, &time_info);
+
+    strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", &time_info);
+
+    if (virAsprintf(&timestamp, "%s.%3d: ",
+                    timestr, (int) cur_time.tv_usec / 1000) < 0) {
+        VIR_FREE(timestamp);
+        virReportOOMError();
+        goto cleanup;
+    }
+
+    if (safewrite(logfile, timestamp, strlen(timestamp)) < 0) {
+        VIR_WARN("Unable to write timestamp to logfile: %s",
+                 virStrerror(errno, ebuf, sizeof ebuf));
+    }
+
+    VIR_FREE(timestamp);
+
     tmp = progenv;
+
     while (*tmp) {
         if (safewrite(logfile, *tmp, strlen(*tmp)) < 0)
             VIR_WARN("Unable to write envv to logfile: %s",
@@ -4166,7 +4190,6 @@ cleanup:
     return -1;
 }

-
 static void qemudShutdownVMDaemon(struct qemud_driver *driver,
                                   virDomainObjPtr vm,
                                   int migrated) {
@@ -4176,6 +4199,42 @@ static void qemudShutdownVMDaemon(struct qemud_driver *driver,
     virErrorPtr orig_err;
     virDomainDefPtr def;
     int i;
+    int logfile = -1;
+    char timestr[100];
+    char *timestamp;
+    char ebuf[1024];
+    struct timeval cur_time;
+    struct tm time_info;
+
+    VIR_DEBUG0("Creating domain log file");
+    if ((logfile = qemudLogFD(driver, vm->def->name)) < 0) {
+        /* To not break the normal domain shutdown process, skip the
+         * timestamp log writing if failed on opening log file. */
+        VIR_WARN("Unable to open logfile: %s",
+                  virStrerror(errno, ebuf, sizeof ebuf));
+    } else {
+        gettimeofday(&cur_time, NULL);
+        localtime_r(&cur_time.tv_sec, &time_info);
+
+        strftime(timestr, sizeof(timestr), "%Y-%m-%d %H:%M:%S", &time_info);
+
+        if (virAsprintf(&timestamp, "%s.%3d: shutting down\n",
+                        timestr, (int) cur_time.tv_usec / 1000) < 0) {
+            VIR_FREE(timestamp);
+            virReportOOMError();
+        }
+
+        if (close(logfile) < 0)
+            VIR_WARN("Unable to close logfile: %s",
+                     virStrerror(errno, ebuf, sizeof ebuf));
+    }
+
+    if (safewrite(logfile, timestamp, strlen(timestamp)) < 0) {
+        VIR_WARN("Unable to write timestamp to logfile: %s",
+                 virStrerror(errno, ebuf, sizeof ebuf));
+    }
+
+    VIR_FREE(timestamp);

     VIR_DEBUG("Shutting down VM '%s' pid=%d migrated=%d",
               vm->def->name, vm->pid, migrated);
@@ -4315,7 +4374,6 @@ retry:
     }
 }

-
 static virDrvOpenStatus qemudOpen(virConnectPtr conn,
                                   virConnectAuthPtr auth ATTRIBUTE_UNUSED,
                                   int flags ATTRIBUTE_UNUSED) {
--
1.7.2.3




More information about the libvir-list mailing list