]> xenbits.xensource.com Git - libvirt.git/commitdiff
daemon: Hook up the virLog{Get,Set}DefaultOutput to the daemon's init routine
authorErik Skultety <eskultet@redhat.com>
Mon, 31 Oct 2016 11:52:51 +0000 (12:52 +0100)
committerErik Skultety <eskultet@redhat.com>
Thu, 15 Dec 2016 09:36:23 +0000 (10:36 +0100)
Now that virLog{Get,Set}DefaultOutput routines are introduced we can wire them
up to the daemon's logging initialization code. Also, change the order of
operations a bit so that we still strictly honor our precedence of settings:
cmdline > env > config now that outputs and filters are not appended anymore.

Signed-off-by: Erik Skultety <eskultet@redhat.com>
daemon/libvirtd.c
src/locking/lock_daemon.c
src/logging/log_daemon.c

index 3902a8bd18c17c5b0bcad29272bd590dcbcdb37a..b6d76ed84ba4e13169f7089c2a4133be7513260c 100644 (file)
@@ -675,26 +675,25 @@ daemonSetupLogging(struct daemonConfig *config,
      * Libvirtd's order of precedence is:
      * cmdline > environment > config
      *
-     * In order to achieve this, we must process configuration in
-     * different order for the log level versus the filters and
-     * outputs. Because filters and outputs append, we have to look at
-     * the environment first and then only check the config file if
-     * there was no result from the environment. The default output is
-     * then applied only if there was no setting from either of the
-     * first two. Because we don't have a way to determine if the log
-     * level has been set, we must process variables in the opposite
+     * The default output is applied only if there was no setting from either
+     * the config or the environment. Because we don't have a way to determine
+     * if the log level has been set, we must process variables in the opposite
      * order, each one overriding the previous.
      */
     if (config->log_level != 0)
         virLogSetDefaultPriority(config->log_level);
 
-    virLogSetFromEnv();
+    if (virLogSetDefaultOutput("libvirtd.log", godaemon, privileged) < 0)
+        return -1;
 
-    if (virLogGetNbFilters() == 0)
-        virLogSetFilters(config->log_filters);
+    /* In case the config is empty, the filters become empty and outputs will
+     * be set to default
+     */
+    ignore_value(virLogSetFilters(config->log_filters));
+    ignore_value(virLogSetOutputs(config->log_outputs));
 
-    if (virLogGetNbOutputs() == 0)
-        virLogSetOutputs(config->log_outputs);
+    /* If there are some environment variables defined, use those instead */
+    virLogSetFromEnv();
 
     /*
      * Command line override for --verbose
@@ -702,76 +701,7 @@ daemonSetupLogging(struct daemonConfig *config,
     if ((verbose) && (virLogGetDefaultPriority() > VIR_LOG_INFO))
         virLogSetDefaultPriority(VIR_LOG_INFO);
 
-    /*
-     * 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 (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);
-    }
-
     return 0;
-
- error:
-    return -1;
 }
 
 
index 9ee818e93e25b9976a221fa08d10673a60906020..1c94ddd054c257188f03be5e8767e1661caac901 100644 (file)
@@ -460,26 +460,25 @@ virLockDaemonSetupLogging(virLockDaemonConfigPtr config,
      * Libvirtd's order of precedence is:
      * cmdline > environment > config
      *
-     * In order to achieve this, we must process configuration in
-     * different order for the log level versus the filters and
-     * outputs. Because filters and outputs append, we have to look at
-     * the environment first and then only check the config file if
-     * there was no result from the environment. The default output is
-     * then applied only if there was no setting from either of the
-     * first two. Because we don't have a way to determine if the log
-     * level has been set, we must process variables in the opposite
+     * The default output is applied only if there was no setting from either
+     * the config or the environment. Because we don't have a way to determine
+     * if the log level has been set, we must process variables in the opposite
      * order, each one overriding the previous.
      */
     if (config->log_level != 0)
         virLogSetDefaultPriority(config->log_level);
 
-    virLogSetFromEnv();
+    if (virLogSetDefaultOutput("virtlockd.log", godaemon, privileged) < 0)
+        return -1;
 
-    if (virLogGetNbFilters() == 0)
-        virLogSetFilters(config->log_filters);
+    /* In case the config is empty, the filters become empty and outputs will
+     * be set to default
+     */
+    ignore_value(virLogSetFilters(config->log_filters));
+    ignore_value(virLogSetOutputs(config->log_outputs));
 
-    if (virLogGetNbOutputs() == 0)
-        virLogSetOutputs(config->log_outputs);
+    /* If there are some environment variables defined, use those instead */
+    virLogSetFromEnv();
 
     /*
      * Command line override for --verbose
@@ -487,70 +486,7 @@ virLockDaemonSetupLogging(virLockDaemonConfigPtr config,
     if ((verbose) && (virLogGetDefaultPriority() > VIR_LOG_INFO))
         virLogSetDefaultPriority(VIR_LOG_INFO);
 
-    /*
-     * 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 (virLogGetNbOutputs() == 0 &&
-        (godaemon || !isatty(STDIN_FILENO))) {
-        char *tmp;
-        if (access("/run/systemd/journal/socket", W_OK) >= 0) {
-            if (virAsprintf(&tmp, "%d:journald", virLogGetDefaultPriority()) < 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/virtlockd.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) {
-                    VIR_FREE(logdir);
-                    umask(old_umask);
-                    goto error;
-                }
-                umask(old_umask);
-
-                if (virAsprintf(&tmp, "%d:file:%s/virtlockd.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;
 }
 
 
index a9aebdb6d7dd5c531b5bfda34af654e4964e2a87..5997cce9d3415637fd252a82e25d6283e2d4ff14 100644 (file)
@@ -385,29 +385,28 @@ virLogDaemonSetupLogging(virLogDaemonConfigPtr config,
     virLogReset();
 
     /*
-     * virtlogd's order of precedence is:
+     * Libvirtd's order of precedence is:
      * cmdline > environment > config
      *
-     * In order to achieve this, we must process configuration in
-     * different order for the log level versus the filters and
-     * outputs. Because filters and outputs append, we have to look at
-     * the environment first and then only check the config file if
-     * there was no result from the environment. The default output is
-     * then applied only if there was no setting from either of the
-     * first two. Because we don't have a way to determine if the log
-     * level has been set, we must process variables in the opposite
+     * The default output is applied only if there was no setting from either
+     * the config or the environment. Because we don't have a way to determine
+     * if the log level has been set, we must process variables in the opposite
      * order, each one overriding the previous.
      */
     if (config->log_level != 0)
         virLogSetDefaultPriority(config->log_level);
 
-    virLogSetFromEnv();
+    if (virLogSetDefaultOutput("virtlogd.log", godaemon, privileged) < 0)
+        return -1;
 
-    if (virLogGetNbFilters() == 0)
-        virLogSetFilters(config->log_filters);
+    /* In case the config is empty, the filters become empty and outputs will
+     * be set to default
+     */
+    ignore_value(virLogSetFilters(config->log_filters));
+    ignore_value(virLogSetOutputs(config->log_outputs));
 
-    if (virLogGetNbOutputs() == 0)
-        virLogSetOutputs(config->log_outputs);
+    /* If there are some environment variables defined, use those instead */
+    virLogSetFromEnv();
 
     /*
      * Command line override for --verbose
@@ -415,70 +414,7 @@ virLogDaemonSetupLogging(virLogDaemonConfigPtr config,
     if ((verbose) && (virLogGetDefaultPriority() > VIR_LOG_INFO))
         virLogSetDefaultPriority(VIR_LOG_INFO);
 
-    /*
-     * 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 (virLogGetNbOutputs() == 0 &&
-        (godaemon || !isatty(STDIN_FILENO))) {
-        char *tmp;
-        if (access("/run/systemd/journal/socket", W_OK) >= 0) {
-            if (virAsprintf(&tmp, "%d:journald", virLogGetDefaultPriority()) < 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/virtlogd.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);
-                    VIR_FREE(logdir);
-                    goto error;
-                }
-                umask(old_umask);
-
-                if (virAsprintf(&tmp, "%d:file:%s/virtlogd.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;
 }