Include error domain and code in log messages from errors

When a virError is raised, pass the error domain and code
onto the systemd journald using metadata fields.

This allows error messages to be queried by code eg

  $ journalctl LIBVIRT_CODE=43

Signed-off-by: Daniel P. Berrange <berrange@redhat.com>
This commit is contained in:
Daniel P. Berrange 2014-02-21 17:23:10 +00:00
parent c6cae57098
commit 0915053e97
2 changed files with 11 additions and 1 deletions

View File

@ -173,6 +173,10 @@
<dd>The line number of the file emitting the log record</dd>
<dt><code>CODE_FUNC</code></dt>
<dd>The name of the function emitting the log record</dd>
<dt><code>LIBVIRT_DOMAIN</code></dt>
<dd>The libvirt error domain (values from virErrorDomain enum), if LIBVIRT_SOURCE="error"</dd>
<dt><code>LIBVIRT_CODE</code></dt>
<dd>The libvirt error code (values from virErrorCode enum), if LIBVIRT_SOURCE="error"</dd>
</dl>
<h2>

View File

@ -649,6 +649,11 @@ virRaiseErrorFull(const char *filename ATTRIBUTE_UNUSED,
virErrorPtr to;
char *str;
int priority;
virLogMetadata meta[] = {
{ .key = "LIBVIRT_DOMAIN", .s = NULL, .iv = domain },
{ .key = "LIBVIRT_CODE", .s = NULL, .iv = code },
{ .key = NULL },
};
/*
* All errors are recorded in thread local storage
@ -703,10 +708,11 @@ virRaiseErrorFull(const char *filename ATTRIBUTE_UNUSED,
priority = virErrorLevelPriority(level);
if (virErrorLogPriorityFilter)
priority = virErrorLogPriorityFilter(to, priority);
virLogMessage(virErrorLogPriorityFilter ? VIR_LOG_FROM_FILE : VIR_LOG_FROM_ERROR,
priority,
filename, linenr, funcname,
NULL, "%s", str);
meta, "%s", str);
errno = save_errno;
}