[libvirt] [PATCH 1/8] daemon: Introduce daemonSetLoggingDefaults

Erik Skultety eskultet at redhat.com
Tue Nov 1 10:27:20 UTC 2016


This patch moves the code responsible for setting up logging defaults to a
separate function to enhance the readability a bit more. This code movement
is also meant as a preparation phase for a future refactor of the affected
hunks.

Signed-off-by: Erik Skultety <eskultet at redhat.com>
---
 daemon/libvirtd.c | 127 +++++++++++++++++++++++++++++-------------------------
 1 file changed, 68 insertions(+), 59 deletions(-)

diff --git a/daemon/libvirtd.c b/daemon/libvirtd.c
index cd25b50..9a5f193 100644
--- a/daemon/libvirtd.c
+++ b/daemon/libvirtd.c
@@ -657,6 +657,70 @@ daemonSetupNetworking(virNetServerPtr srv,
 }
 
 
+static int
+daemonSetupLoggingDefaults(bool godaemon, bool privileged)
+{
+    if (virLogGetOutputs() == 0 &&
+        (godaemon || !isatty(STDIN_FILENO))) {
+        char *tmp;
+        if (access("/run/systemd/journal/socket", W_OK) >= 0) {
+            virLogPriority priority = virLogGetDefaultPriority();
+
+            /* By default we don't want to log too much stuff into journald as
+             * it may employ rate limiting and thus block libvirt execution. */
+            if (priority == VIR_LOG_DEBUG)
+                priority = VIR_LOG_INFO;
+
+            if (virAsprintf(&tmp, "%d:journald", priority) < 0)
+                goto error;
+            virLogSetOutputs(tmp);
+            VIR_FREE(tmp);
+        }
+    }
+
+    if (virLogGetOutputs() == 0) {
+        char *tmp = NULL;
+
+        if (godaemon) {
+            if (privileged) {
+                if (virAsprintf(&tmp, "%d:file:%s/log/libvirt/libvirtd.log",
+                                virLogGetDefaultPriority(),
+                                LOCALSTATEDIR) == -1)
+                    goto error;
+            } else {
+                char *logdir = virGetUserCacheDirectory();
+                mode_t old_umask;
+
+                if (!logdir)
+                    goto error;
+
+                old_umask = umask(077);
+                if (virFileMakePath(logdir) < 0) {
+                    umask(old_umask);
+                    goto error;
+                }
+                umask(old_umask);
+
+                if (virAsprintf(&tmp, "%d:file:%s/libvirtd.log",
+                                virLogGetDefaultPriority(), logdir) == -1) {
+                    VIR_FREE(logdir);
+                    goto error;
+                }
+                VIR_FREE(logdir);
+            }
+        } else {
+            if (virAsprintf(&tmp, "%d:stderr", virLogGetDefaultPriority()) < 0)
+                goto error;
+        }
+        virLogSetOutputs(tmp);
+        VIR_FREE(tmp);
+    }
+
+    return 0;
+ error:
+    return -1;
+}
+
 /*
  * Set up the logging environment
  * By default if daemonized all errors go to the logfile libvirtd.log,
@@ -706,67 +770,12 @@ daemonSetupLogging(struct daemonConfig *config,
      * If no defined outputs, and either running
      * as daemon or not on a tty, then first try
      * to direct it to the systemd journal
-     * (if it exists)....
+     * (if it exists), otherwise fallback to libvirtd.log. If both not running
+     * as daemon and having a tty, use stderr as default.
      */
     if (virLogGetNbOutputs() == 0 &&
-        (godaemon || !isatty(STDIN_FILENO))) {
-        char *tmp;
-        if (access("/run/systemd/journal/socket", W_OK) >= 0) {
-            virLogPriority priority = virLogGetDefaultPriority();
-
-            /* By default we don't want to log too much stuff into journald as
-             * it may employ rate limiting and thus block libvirt execution. */
-            if (priority == VIR_LOG_DEBUG)
-                priority = VIR_LOG_INFO;
-
-            if (virAsprintf(&tmp, "%d:journald", priority) < 0)
-                goto error;
-            virLogSetOutputs(tmp);
-            VIR_FREE(tmp);
-        }
-    }
-
-    /*
-     * otherwise direct to libvirtd.log when running
-     * as daemon. Otherwise the default output is stderr.
-     */
-    if (virLogGetNbOutputs() == 0) {
-        char *tmp = NULL;
-
-        if (godaemon) {
-            if (privileged) {
-                if (virAsprintf(&tmp, "%d:file:%s/log/libvirt/libvirtd.log",
-                                virLogGetDefaultPriority(),
-                                LOCALSTATEDIR) == -1)
-                    goto error;
-            } else {
-                char *logdir = virGetUserCacheDirectory();
-                mode_t old_umask;
-
-                if (!logdir)
-                    goto error;
-
-                old_umask = umask(077);
-                if (virFileMakePath(logdir) < 0) {
-                    umask(old_umask);
-                    goto error;
-                }
-                umask(old_umask);
-
-                if (virAsprintf(&tmp, "%d:file:%s/libvirtd.log",
-                                virLogGetDefaultPriority(), logdir) == -1) {
-                    VIR_FREE(logdir);
-                    goto error;
-                }
-                VIR_FREE(logdir);
-            }
-        } else {
-            if (virAsprintf(&tmp, "%d:stderr", virLogGetDefaultPriority()) < 0)
-                goto error;
-        }
-        virLogSetOutputs(tmp);
-        VIR_FREE(tmp);
-    }
+        daemonSetupLoggingDefaults(godaemon, privileged) < 0)
+        goto error;
 
     return 0;
 
-- 
2.5.5




More information about the libvir-list mailing list