Turn virLogSource into a struct instead of an enum

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@redhat.com>
This commit is contained in:
Daniel P. Berrange 2014-02-27 17:44:53 +00:00
parent b29275d928
commit 098dd79ee2
10 changed files with 45 additions and 48 deletions

View File

@ -1462,6 +1462,7 @@ virLogParseOutputs;
virLogPriorityFromSyslog;
virLogProbablyLogMessage;
virLogReset;
virLogSelf;
virLogSetBufferSize;
virLogSetDefaultPriority;
virLogSetFromEnv;

View File

@ -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);

View File

@ -3178,7 +3178,7 @@ virQEMUCapsLogProbeFailure(const char *binary)
};
virErrorPtr err = virGetLastError();
virLogMessage(VIR_LOG_FROM_FILE,
virLogMessage(&virLogSelf,
VIR_LOG_WARN,
__FILE__, __LINE__, __func__,
meta,

View File

@ -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,
filename, linenr, funcname,
NULL, "success=yes %s", str);
else
virLogMessage(VIR_LOG_FROM_AUDIT, VIR_LOG_WARN,
virLogMessage(source, VIR_LOG_WARN,
filename, linenr, funcname,
NULL, "success=no %s", str);
}

View File

@ -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) \

View File

@ -719,7 +719,7 @@ virRaiseErrorFull(const char *filename ATTRIBUTE_UNUSED,
*/
if (virLogGetNbOutputs() > 0 ||
virErrorLogPriorityFilter)
virLogMessage(VIR_LOG_FROM_ERROR,
virLogMessage(&virLogSelf,
priority,
filename, linenr, funcname,
meta, "%s", str);

View File

@ -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);

View File

@ -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;
VIR_LOG_FROM_LAST,
} virLogSource;
struct _virLogSource {
const char *name;
};
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(virLogSourcePtr 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,

View File

@ -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

View File

@ -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,