]> xenbits.xensource.com Git - libvirt.git/commitdiff
log: include hostname in initial log message
authorDaniel P. Berrange <berrange@redhat.com>
Fri, 4 Dec 2015 17:35:54 +0000 (17:35 +0000)
committerDaniel P. Berrange <berrange@redhat.com>
Thu, 10 Dec 2015 18:05:49 +0000 (18:05 +0000)
On the very first log message we send to any output, we include
the libvirt version number and package string. In some bug reports
we have been given libvirtd.log files that came from a different
host than the corresponding /var/log/libvirt/qemu log files. So
extend the initial log message to include the hostname too.

eg on first log message we would now see:

 $ libvirtd
 2015-12-04 17:35:36.610+0000: 20917: info : libvirt version: 1.3.0
 2015-12-04 17:35:36.610+0000: 20917: info : hostname: dhcp-1-180.lcy.redhat.com
 2015-12-04 17:35:36.610+0000: 20917: error : qemuMonitorIO:687 : internal error: End of file from monitor

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
src/libvirt_private.syms
src/util/virlog.c
src/util/virutil.c
src/util/virutil.h

index 63d8618fa10d0cbb5b3abd2b64ddc2b476e525c9..55822aef9916fe619babaf049249de23d3e50ae0 100644 (file)
@@ -2393,6 +2393,7 @@ virGetGroupID;
 virGetGroupList;
 virGetGroupName;
 virGetHostname;
+virGetHostnameQuiet;
 virGetListenFDs;
 virGetSCSIHostNameByParentaddr;
 virGetSCSIHostNumber;
index 627f4cb2506dd261e69d6821b211ed8d91f2be5a..7d5a266dd4fdf335fd5f54611186375462942f31 100644 (file)
@@ -94,7 +94,7 @@ static int virLogNbFilters;
  * after filtering, multiple output can be used simultaneously
  */
 struct _virLogOutput {
-    bool logVersion;
+    bool logInitMessage;
     void *data;
     virLogOutputFunc f;
     virLogCloseFunc c;
@@ -402,7 +402,7 @@ virLogDefineOutput(virLogOutputFunc f,
         goto cleanup;
     }
     ret = virLogNbOutputs++;
-    virLogOutputs[ret].logVersion = true;
+    virLogOutputs[ret].logInitMessage = true;
     virLogOutputs[ret].f = f;
     virLogOutputs[ret].c = c;
     virLogOutputs[ret].data = data;
@@ -452,6 +452,32 @@ virLogVersionString(const char **rawmsg,
     return virLogFormatString(msg, 0, NULL, VIR_LOG_INFO, VIR_LOG_VERSION_STRING);
 }
 
+/* Similar to virGetHostname() but avoids use of error
+ * reporting APIs or logging APIs, to prevent recursion
+ */
+static int
+virLogHostnameString(const char **rawmsg,
+                     char **msg)
+{
+    char *hostname = virGetHostnameQuiet();
+    char *hoststr;
+
+    if (!hostname)
+        return -1;
+
+    if (virAsprintfQuiet(&hoststr, "hostname: %s", hostname) < 0) {
+        VIR_FREE(hostname);
+        return -1;
+    }
+
+    if (virLogFormatString(msg, 0, NULL, VIR_LOG_INFO, hoststr) < 0) {
+        VIR_FREE(hoststr);
+        return -1;
+    }
+    *rawmsg = hoststr;
+    return 0;
+}
+
 
 static void
 virLogSourceUpdate(virLogSourcePtr source)
@@ -533,7 +559,7 @@ virLogVMessage(virLogSourcePtr source,
                const char *fmt,
                va_list vargs)
 {
-    static bool logVersionStderr = true;
+    static bool logInitMessageStderr = true;
     char *str = NULL;
     char *msg = NULL;
     char timestamp[VIR_TIME_STRING_BUFLEN];
@@ -583,16 +609,22 @@ virLogVMessage(virLogSourcePtr source,
      */
     for (i = 0; i < virLogNbOutputs; i++) {
         if (priority >= virLogOutputs[i].priority) {
-            if (virLogOutputs[i].logVersion) {
-                const char *rawver;
-                char *ver = NULL;
-                if (virLogVersionString(&rawver, &ver) >= 0)
+            if (virLogOutputs[i].logInitMessage) {
+                const char *rawinitmsg;
+                char *initmsg = NULL;
+                if (virLogVersionString(&rawinitmsg, &initmsg) >= 0)
+                    virLogOutputs[i].f(&virLogSelf, VIR_LOG_INFO,
+                                       __FILE__, __LINE__, __func__,
+                                       timestamp, NULL, 0, rawinitmsg, initmsg,
+                                       virLogOutputs[i].data);
+                VIR_FREE(initmsg);
+                if (virLogHostnameString(&rawinitmsg, &initmsg) >= 0)
                     virLogOutputs[i].f(&virLogSelf, VIR_LOG_INFO,
                                        __FILE__, __LINE__, __func__,
-                                       timestamp, NULL, 0, rawver, ver,
+                                       timestamp, NULL, 0, rawinitmsg, initmsg,
                                        virLogOutputs[i].data);
-                VIR_FREE(ver);
-                virLogOutputs[i].logVersion = false;
+                VIR_FREE(initmsg);
+                virLogOutputs[i].logInitMessage = false;
             }
             virLogOutputs[i].f(source, priority,
                                filename, linenr, funcname,
@@ -601,16 +633,22 @@ virLogVMessage(virLogSourcePtr source,
         }
     }
     if (virLogNbOutputs == 0) {
-        if (logVersionStderr) {
-            const char *rawver;
-            char *ver = NULL;
-            if (virLogVersionString(&rawver, &ver) >= 0)
+        if (logInitMessageStderr) {
+            const char *rawinitmsg;
+            char *initmsg = NULL;
+            if (virLogVersionString(&rawinitmsg, &initmsg) >= 0)
+                virLogOutputToFd(&virLogSelf, VIR_LOG_INFO,
+                                 __FILE__, __LINE__, __func__,
+                                 timestamp, NULL, 0, rawinitmsg, initmsg,
+                                 (void *) STDERR_FILENO);
+            VIR_FREE(initmsg);
+            if (virLogHostnameString(&rawinitmsg, &initmsg) >= 0)
                 virLogOutputToFd(&virLogSelf, VIR_LOG_INFO,
                                  __FILE__, __LINE__, __func__,
-                                 timestamp, NULL, 0, rawver, ver,
+                                 timestamp, NULL, 0, rawinitmsg, initmsg,
                                  (void *) STDERR_FILENO);
-            VIR_FREE(ver);
-            logVersionStderr = false;
+            VIR_FREE(initmsg);
+            logInitMessageStderr = false;
         }
         virLogOutputToFd(source, priority,
                          filename, linenr, funcname,
index fe65faf2762b5fa26e83e9b4458b0bc6db86dcad..177f9d75fb1d79efcc183caa9d8b3fe07d93ad63 100644 (file)
@@ -662,16 +662,18 @@ char *virIndexToDiskName(int idx, const char *prefix)
  *         we got from getaddrinfo().  Return the value from gethostname()
  *         and hope for the best.
  */
-char *virGetHostname(void)
+static char *
+virGetHostnameImpl(bool quiet)
 {
     int r;
-    char hostname[HOST_NAME_MAX+1], *result;
+    char hostname[HOST_NAME_MAX+1], *result = NULL;
     struct addrinfo hints, *info;
 
     r = gethostname(hostname, sizeof(hostname));
     if (r == -1) {
-        virReportSystemError(errno,
-                             "%s", _("failed to determine host name"));
+        if (!quiet)
+            virReportSystemError(errno,
+                                 "%s", _("failed to determine host name"));
         return NULL;
     }
     NUL_TERMINATE(hostname);
@@ -683,7 +685,7 @@ char *virGetHostname(void)
          * string as-is; it's up to callers to check whether "localhost"
          * is allowed.
          */
-        ignore_value(VIR_STRDUP(result, hostname));
+        ignore_value(VIR_STRDUP_QUIET(result, hostname));
         goto cleanup;
     }
 
@@ -696,9 +698,10 @@ char *virGetHostname(void)
     hints.ai_family = AF_UNSPEC;
     r = getaddrinfo(hostname, NULL, &hints, &info);
     if (r != 0) {
-        VIR_WARN("getaddrinfo failed for '%s': %s",
-                 hostname, gai_strerror(r));
-        ignore_value(VIR_STRDUP(result, hostname));
+        if (!quiet)
+            VIR_WARN("getaddrinfo failed for '%s': %s",
+                     hostname, gai_strerror(r));
+        ignore_value(VIR_STRDUP_QUIET(result, hostname));
         goto cleanup;
     }
 
@@ -711,18 +714,34 @@ char *virGetHostname(void)
          * localhost.  Ignore the canonicalized name and just return the
          * original hostname
          */
-        ignore_value(VIR_STRDUP(result, hostname));
+        ignore_value(VIR_STRDUP_QUIET(result, hostname));
     else
         /* Caller frees this string. */
-        ignore_value(VIR_STRDUP(result, info->ai_canonname));
+        ignore_value(VIR_STRDUP_QUIET(result, info->ai_canonname));
 
     freeaddrinfo(info);
 
  cleanup:
+    if (!result)
+        virReportOOMError();
     return result;
 }
 
 
+char *
+virGetHostname(void)
+{
+    return virGetHostnameImpl(false);
+}
+
+
+char *
+virGetHostnameQuiet(void)
+{
+    return virGetHostnameImpl(true);
+}
+
+
 char *
 virGetUserDirectory(void)
 {
index 02387e0a14caada0e3f6156b35b3dff1996cf38c..535807c4f7a43df518189600045ac7a146365127 100644 (file)
@@ -132,6 +132,7 @@ static inline int pthread_sigmask(int how,
 # endif
 
 char *virGetHostname(void);
+char *virGetHostnameQuiet(void);
 
 char *virGetUserDirectory(void);
 char *virGetUserDirectoryByUID(uid_t uid);