As part of the goal to get away from doing string matching on filenames when deciding whether to emit a log message, turn the virLogSource enum into a struct which contains a log "name". There will eventually be one virLogSource instance statically declared per source file. To minimise churn in this commit though, a single global instance is used. Signed-off-by: Daniel P. Berrange <berrange@xxxxxxxxxx> --- src/libvirt_private.syms | 1 + src/node_device/node_device_udev.c | 2 +- src/qemu/qemu_capabilities.c | 2 +- src/util/viraudit.c | 7 ++++--- src/util/viraudit.h | 10 ++++++---- src/util/virerror.c | 2 +- src/util/virlog.c | 30 ++++++++++++------------------ src/util/virlog.h | 33 ++++++++++++++++----------------- src/util/virprobe.h | 4 ++-- tests/testutils.c | 2 +- 10 files changed, 45 insertions(+), 48 deletions(-) diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 3d8f2e3..4bd3ec3 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1437,6 +1437,7 @@ virLogParseOutputs; virLogPriorityFromSyslog; virLogProbablyLogMessage; virLogReset; +virLogSelf; virLogSetBufferSize; virLogSetDefaultPriority; virLogSetFromEnv; diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c index 5d49968..5c48fe8 100644 --- a/src/node_device/node_device_udev.c +++ b/src/node_device/node_device_udev.c @@ -374,7 +374,7 @@ udevLogFunction(struct udev *udev ATTRIBUTE_UNUSED, format = virBufferContentAndReset(&buf); - virLogVMessage(VIR_LOG_FROM_LIBRARY, + virLogVMessage(&virLogSelf, virLogPriorityFromSyslog(priority), file, line, fn, NULL, format ? format : fmt, args); diff --git a/src/qemu/qemu_capabilities.c b/src/qemu/qemu_capabilities.c index 76a8c73..70b5f15 100644 --- a/src/qemu/qemu_capabilities.c +++ b/src/qemu/qemu_capabilities.c @@ -2766,7 +2766,7 @@ virQEMUCapsLogProbeFailure(const char *binary) }; virErrorPtr err = virGetLastError(); - virLogMessage(VIR_LOG_FROM_FILE, + virLogMessage(&virLogSelf, VIR_LOG_WARN, __FILE__, __LINE__, __func__, meta, diff --git a/src/util/viraudit.c b/src/util/viraudit.c index 5dd4e1f..1d9442e 100644 --- a/src/util/viraudit.c +++ b/src/util/viraudit.c @@ -74,7 +74,8 @@ void virAuditLog(int logging) } -void virAuditSend(const char *filename, +void virAuditSend(virLogSourcePtr source, + const char *filename, size_t linenr, const char *funcname, const char *clienttty ATTRIBUTE_UNUSED, @@ -104,11 +105,11 @@ void virAuditSend(const char *filename, if (auditlog && str) { if (success) - virLogMessage(VIR_LOG_FROM_AUDIT, VIR_LOG_INFO, + virLogMessage(source, VIR_LOG_INFO, /* XXX */ filename, linenr, funcname, NULL, "success=yes %s", str); else - virLogMessage(VIR_LOG_FROM_AUDIT, VIR_LOG_WARN, + virLogMessage(source, VIR_LOG_WARN, /* XXX */ filename, linenr, funcname, NULL, "success=no %s", str); } diff --git a/src/util/viraudit.h b/src/util/viraudit.h index b0356da..cf3da6c 100644 --- a/src/util/viraudit.h +++ b/src/util/viraudit.h @@ -24,6 +24,7 @@ # define __LIBVIRT_AUDIT_H__ # include "internal.h" +# include "virlog.h" enum virAuditRecordType { VIR_AUDIT_RECORD_MACHINE_CONTROL, @@ -35,22 +36,23 @@ int virAuditOpen(void); void virAuditLog(int enabled); -void virAuditSend(const char *filename, size_t linenr, const char *funcname, +void virAuditSend(virLogSourcePtr source, + const char *filename, size_t linenr, const char *funcname, const char *clienttty, const char *clientaddr, enum virAuditRecordType type, bool success, const char *fmt, ...) - ATTRIBUTE_FMT_PRINTF(8, 9); + ATTRIBUTE_FMT_PRINTF(9, 10); char *virAuditEncode(const char *key, const char *value); void virAuditClose(void); # define VIR_AUDIT(type, success, ...) \ - virAuditSend(__FILE__, __LINE__, __func__, \ + virAuditSend(&virLogSelf, __FILE__, __LINE__, __func__, \ NULL, NULL, type, success, __VA_ARGS__); # define VIR_AUDIT_USER(type, success, clienttty, clientaddr, ...) \ - virAuditSend(__FILE__, __LINE__, __func__, \ + virAuditSend(&virLogSelf, __FILE__, __LINE__, __func__, \ clienttty, clientaddr, type, success, __VA_ARGS__); # define VIR_AUDIT_STR(str) \ diff --git a/src/util/virerror.c b/src/util/virerror.c index 562c871..123e51f 100644 --- a/src/util/virerror.c +++ b/src/util/virerror.c @@ -715,7 +715,7 @@ virRaiseErrorFull(const char *filename ATTRIBUTE_UNUSED, * hate & thus disable that too. */ if (virLogGetNbOutputs() > 0) - virLogMessage(virErrorLogPriorityFilter ? VIR_LOG_FROM_FILE : VIR_LOG_FROM_ERROR, + virLogMessage(&virLogSelf, priority, filename, linenr, funcname, meta, "%s", str); diff --git a/src/util/virlog.c b/src/util/virlog.c index 801f259..b535623 100644 --- a/src/util/virlog.c +++ b/src/util/virlog.c @@ -60,14 +60,6 @@ #define VIR_FROM_THIS VIR_FROM_NONE -VIR_ENUM_DECL(virLogSource) -VIR_ENUM_IMPL(virLogSource, VIR_LOG_FROM_LAST, - "file", - "error", - "audit", - "trace", - "library"); - /* * A logging buffer to keep some history over logs */ @@ -104,6 +96,8 @@ typedef virLogFilter *virLogFilterPtr; static virLogFilterPtr virLogFilters = NULL; static int virLogNbFilters = 0; +virLogSource virLogSelf = { .name = "util.log" }; + /* * Outputs are used to emit the messages retained * after filtering, multiple output can be used simultaneously @@ -130,7 +124,7 @@ static virLogPriority virLogDefaultPriority = VIR_LOG_DEFAULT; static int virLogResetFilters(void); static int virLogResetOutputs(void); -static void virLogOutputToFd(virLogSource src, +static void virLogOutputToFd(virLogSourcePtr src, virLogPriority priority, const char *filename, int linenr, @@ -142,6 +136,7 @@ static void virLogOutputToFd(virLogSource src, const char *str, void *data); + /* * Logs accesses must be serialized though a mutex */ @@ -765,7 +760,7 @@ virLogVersionString(const char **rawmsg, * the message may be stored, sent to output or just discarded */ void -virLogMessage(virLogSource source, +virLogMessage(virLogSourcePtr source, virLogPriority priority, const char *filename, int linenr, @@ -797,7 +792,7 @@ virLogMessage(virLogSource source, * the message may be stored, sent to output or just discarded */ void -virLogVMessage(virLogSource source, +virLogVMessage(virLogSourcePtr source, virLogPriority priority, const char *filename, int linenr, @@ -869,7 +864,7 @@ virLogVMessage(virLogSource source, const char *rawver; char *ver = NULL; if (virLogVersionString(&rawver, &ver) >= 0) - virLogOutputs[i].f(VIR_LOG_FROM_FILE, VIR_LOG_INFO, + virLogOutputs[i].f(&virLogSelf, VIR_LOG_INFO, __FILE__, __LINE__, __func__, timestamp, NULL, 0, rawver, ver, virLogOutputs[i].data); @@ -887,7 +882,7 @@ virLogVMessage(virLogSource source, const char *rawver; char *ver = NULL; if (virLogVersionString(&rawver, &ver) >= 0) - virLogOutputToFd(VIR_LOG_FROM_FILE, VIR_LOG_INFO, + virLogOutputToFd(&virLogSelf, VIR_LOG_INFO, __FILE__, __LINE__, __func__, timestamp, NULL, 0, rawver, ver, (void *) STDERR_FILENO); @@ -929,7 +924,7 @@ virLogStackTraceToFd(int fd) } static void -virLogOutputToFd(virLogSource source ATTRIBUTE_UNUSED, +virLogOutputToFd(virLogSourcePtr source ATTRIBUTE_UNUSED, virLogPriority priority ATTRIBUTE_UNUSED, const char *filename ATTRIBUTE_UNUSED, int linenr ATTRIBUTE_UNUSED, @@ -1033,7 +1028,7 @@ virLogPrioritySyslog(virLogPriority priority) #if HAVE_SYSLOG_H static void -virLogOutputToSyslog(virLogSource source ATTRIBUTE_UNUSED, +virLogOutputToSyslog(virLogSourcePtr source ATTRIBUTE_UNUSED, virLogPriority priority, const char *filename ATTRIBUTE_UNUSED, int linenr ATTRIBUTE_UNUSED, @@ -1160,7 +1155,7 @@ journalAddInt(struct journalState *state, const char *field, int value) static int journalfd = -1; static void -virLogOutputToJournald(virLogSource source, +virLogOutputToJournald(virLogSourcePtr source, virLogPriority priority, const char *filename, int linenr, @@ -1202,8 +1197,7 @@ virLogOutputToJournald(virLogSource source, journalAddString(&state, "MESSAGE", rawstr); journalAddInt(&state, "PRIORITY", virLogPrioritySyslog(priority)); - journalAddString(&state, "LIBVIRT_SOURCE", - virLogSourceTypeToString(source)); + journalAddString(&state, "LIBVIRT_SOURCE", source->name); if (filename) journalAddString(&state, "CODE_FILE", filename); journalAddInt(&state, "CODE_LINE", linenr); diff --git a/src/util/virlog.h b/src/util/virlog.h index 6ba2daa..cb27725 100644 --- a/src/util/virlog.h +++ b/src/util/virlog.h @@ -44,15 +44,14 @@ typedef enum { VIR_LOG_TO_JOURNALD, } virLogDestination; -typedef enum { - VIR_LOG_FROM_FILE, /* General debugging */ - VIR_LOG_FROM_ERROR, /* Errors reported */ - VIR_LOG_FROM_AUDIT, /* Audit operations */ - VIR_LOG_FROM_TRACE, /* DTrace probe pointers */ - VIR_LOG_FROM_LIBRARY, /* 3rd party libraries */ +typedef struct _virLogSource virLogSource; +typedef virLogSource *virLogSourcePtr; + +struct _virLogSource { + const char *name; +}; - VIR_LOG_FROM_LAST, -} virLogSource; +extern virLogSource virLogSelf; /* * If configured with --enable-debug=yes then library calls @@ -68,7 +67,7 @@ typedef enum { * * Do nothing but eat parameters. */ -static inline void virLogEatParams(virLogSource unused, ...) +static inline void virLogEatParams(virLog unused, ...) { /* Silence gcc */ unused = unused; @@ -85,13 +84,13 @@ static inline void virLogEatParams(virLogSource unused, ...) virLogMessage(src, VIR_LOG_ERROR, filename, linenr, funcname, NULL, __VA_ARGS__) # define VIR_DEBUG(...) \ - VIR_DEBUG_INT(VIR_LOG_FROM_FILE, __FILE__, __LINE__, __func__, __VA_ARGS__) + VIR_DEBUG_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__) # define VIR_INFO(...) \ - VIR_INFO_INT(VIR_LOG_FROM_FILE, __FILE__, __LINE__, __func__, __VA_ARGS__) + VIR_INFO_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__) # define VIR_WARN(...) \ - VIR_WARN_INT(VIR_LOG_FROM_FILE, __FILE__, __LINE__, __func__, __VA_ARGS__) + VIR_WARN_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__) # define VIR_ERROR(...) \ - VIR_ERROR_INT(VIR_LOG_FROM_FILE, __FILE__, __LINE__, __func__, __VA_ARGS__) + VIR_ERROR_INT(&virLogSelf, __FILE__, __LINE__, __func__, __VA_ARGS__) struct _virLogMetadata { @@ -105,7 +104,7 @@ typedef struct _virLogMetadata *virLogMetadataPtr; /** * virLogOutputFunc: - * @src: the src for the message + * @src: the source of the log message * @priority: the priority for the message * @filename: file where the message was emitted * @linenr: line where the message was emitted @@ -119,7 +118,7 @@ typedef struct _virLogMetadata *virLogMetadataPtr; * * Callback function used to output messages */ -typedef void (*virLogOutputFunc) (virLogSource src, +typedef void (*virLogOutputFunc) (virLogSourcePtr src, virLogPriority priority, const char *filename, int linenr, @@ -172,14 +171,14 @@ extern int virLogParseDefaultPriority(const char *priority); extern int virLogParseFilters(const char *filters); extern int virLogParseOutputs(const char *output); extern int virLogPriorityFromSyslog(int priority); -extern void virLogMessage(virLogSource src, +extern void virLogMessage(virLogSourcePtr source, virLogPriority priority, const char *filename, int linenr, const char *funcname, virLogMetadataPtr metadata, const char *fmt, ...) ATTRIBUTE_FMT_PRINTF(7, 8); -extern void virLogVMessage(virLogSource src, +extern void virLogVMessage(virLogSourcePtr source, virLogPriority priority, const char *filename, int linenr, diff --git a/src/util/virprobe.h b/src/util/virprobe.h index 1891135..c6e07af 100644 --- a/src/util/virprobe.h +++ b/src/util/virprobe.h @@ -83,7 +83,7 @@ # define PROBE_EXPAND(NAME, ARGS) NAME(ARGS) # define PROBE(NAME, FMT, ...) \ - VIR_DEBUG_INT(VIR_LOG_FROM_TRACE, \ + VIR_DEBUG_INT(&virLogSelf, \ NULL, __LINE__, __func__, \ #NAME ": " FMT, __VA_ARGS__); \ if (LIBVIRT_ ## NAME ## _ENABLED()) { \ @@ -92,7 +92,7 @@ } # else # define PROBE(NAME, FMT, ...) \ - VIR_DEBUG_INT(VIR_LOG_FROM_TRACE, \ + VIR_DEBUG_INT(&virLogSelf, \ NULL, __LINE__, __func__, \ #NAME ": " FMT, __VA_ARGS__); # endif diff --git a/tests/testutils.c b/tests/testutils.c index 97d5b50..f88c18d 100644 --- a/tests/testutils.c +++ b/tests/testutils.c @@ -584,7 +584,7 @@ struct virtTestLogData { static struct virtTestLogData testLog = { VIR_BUFFER_INITIALIZER }; static void -virtTestLogOutput(virLogSource source ATTRIBUTE_UNUSED, +virtTestLogOutput(virLogSourcePtr source ATTRIBUTE_UNUSED, virLogPriority priority ATTRIBUTE_UNUSED, const char *filename ATTRIBUTE_UNUSED, int lineno ATTRIBUTE_UNUSED, -- 1.8.5.3 -- libvir-list mailing list libvir-list@xxxxxxxxxx https://www.redhat.com/mailman/listinfo/libvir-list