The error reporting code will invoke a callback when any error
is raised and the default callback will print to stderr. The
virRaiseErrorFull method also sends all error messages on to the
logging code, which also prints to stderr by default. To avoid
duplicated data on stderr, the logging code has some logic to
skip emission when no log outputs are configured, which checks
whether the virLogSource == VIR_LOG_FROM_ERROR.
Meanwhile the libvirtd daemon can register another callback which
is used to reduce log message priority from error to a lower level.
When this is used we do want messages to end up on stderr, so the
error code will conditionally use either VIR_LOG_FROM_FILE or
VIR_LOG_FROM_ERROR depending on whether such a callback is provided.
This will all complicate later refactoring. By pushing the checks
for whether a log output is present up a level into the error code,
the special cases can be isolated in one place.
Signed-off-by: Daniel P. Berrange <berrange(a)redhat.com>
---
src/util/virerror.c | 17 +++++++++++++----
src/util/virlog.c | 2 +-
2 files changed, 14 insertions(+), 5 deletions(-)
diff --git a/src/util/virerror.c b/src/util/virerror.c
index 820e1ad..f85f6b3 100644
--- a/src/util/virerror.c
+++ b/src/util/virerror.c
@@ -709,10 +709,19 @@ virRaiseErrorFull(const char *filename ATTRIBUTE_UNUSED,
if (virErrorLogPriorityFilter)
priority = virErrorLogPriorityFilter(to, priority);
- virLogMessage(virErrorLogPriorityFilter ? VIR_LOG_FROM_FILE : VIR_LOG_FROM_ERROR,
- priority,
- filename, linenr, funcname,
- meta, "%s", str);
+ /* We don't want to pollute stderr if no logging outputs
+ * are explicitly requested by the user, since the default
+ * error function already pollutes stderr and most apps
+ * hate & thus disable that too. If the daemon has set
+ * a priority filter though, we should always forward
+ * all errors to the logging code.
+ */
+ if (virLogGetNbOutputs() > 0 ||
+ virErrorLogPriorityFilter)
+ virLogMessage(VIR_LOG_FROM_ERROR,
+ priority,
+ filename, linenr, funcname,
+ meta, "%s", str);
errno = save_errno;
}
diff --git a/src/util/virlog.c b/src/util/virlog.c
index e9bd61b..801f259 100644
--- a/src/util/virlog.c
+++ b/src/util/virlog.c
@@ -882,7 +882,7 @@ virLogVMessage(virLogSource source,
str, msg, virLogOutputs[i].data);
}
}
- if ((virLogNbOutputs == 0) && (source != VIR_LOG_FROM_ERROR)) {
+ if (virLogNbOutputs == 0) {
if (logVersionStderr) {
const char *rawver;
char *ver = NULL;
--
1.8.5.3