From 698d80a824e0a82ba39194a1e77b807ae83f1f94 Mon Sep 17 00:00:00 2001 From: Daniel Veillard Date: Mon, 22 Dec 2008 10:44:10 +0000 Subject: [PATCH] * src/logging.h src/logging.c: commited the more recent version with function name and line number daniel --- ChangeLog | 5 +++++ src/logging.c | 49 +++++++++++++++++++++++++--------------- src/logging.h | 62 +++++++++++++++++++++++++++++++++------------------ 3 files changed, 76 insertions(+), 40 deletions(-) diff --git a/ChangeLog b/ChangeLog index 304a687d5f..c3dfcda43a 100644 --- a/ChangeLog +++ b/ChangeLog @@ -1,3 +1,8 @@ +Mon Dec 22 11:43:04 CET 2008 Daniel Veillard + + * src/logging.h src/logging.c: commited the more recent version + with function name and line number + Mon Dec 22 11:33:07 CET 2008 Daniel Veillard * src/logging.h src/logging.c: add the infrastructure and internal diff --git a/src/logging.c b/src/logging.c index 27cd236e18..b1ff6dfb08 100644 --- a/src/logging.c +++ b/src/logging.c @@ -469,6 +469,8 @@ cleanup: * virLogMessage: * @category: where is that message coming from * @priority: the priority level + * @funcname: the function emitting the (debug) message + * @linenr: line where the message was emitted * @flags: extra flags, 1 if coming from the error handler * @fmt: the string format * @...: the arguments @@ -476,13 +478,13 @@ cleanup: * Call the libvirt logger with some informations. Based on the configuration * the message may be stored, sent to output or just discarded */ -void virLogMessage(const char *category, int priority, int flags, - const char *fmt, ...) { +void virLogMessage(const char *category, int priority, const char *funcname, + long long linenr, int flags, const char *fmt, ...) { char *str = NULL; char *msg; struct timeval cur_time; struct tm time_info; - int len, fprio, i; + int len, fprio, i, ret; if (!virLogInitialized) virLogStartup(); @@ -509,15 +511,22 @@ void virLogMessage(const char *category, int priority, int flags, gettimeofday(&cur_time, NULL); localtime_r(&cur_time.tv_sec, &time_info); - if (asprintf(&msg, "%02d:%02d:%02d.%03d: %s : %s\n", - time_info.tm_hour, time_info.tm_min, - time_info.tm_sec, (int) cur_time.tv_usec / 1000, - virLogPriorityString(priority), str) < 0) { - /* apparently we're running out of memory */ - VIR_FREE(str); - return; + if ((funcname != NULL) && (priority == VIR_LOG_DEBUG)) { + ret = asprintf(&msg, "%02d:%02d:%02d.%03d: %s : %s:%lld : %s\n", + time_info.tm_hour, time_info.tm_min, + time_info.tm_sec, (int) cur_time.tv_usec / 1000, + virLogPriorityString(priority), funcname, linenr, str); + } else { + ret = asprintf(&msg, "%02d:%02d:%02d.%03d: %s : %s\n", + time_info.tm_hour, time_info.tm_min, + time_info.tm_sec, (int) cur_time.tv_usec / 1000, + virLogPriorityString(priority), str); } VIR_FREE(str); + if (ret < 0) { + /* apparently we're running out of memory */ + return; + } /* * Log based on defaults, first store in the history buffer @@ -532,8 +541,8 @@ void virLogMessage(const char *category, int priority, int flags, virLogLock(); for (i = 0; i < virLogNbOutputs;i++) { if (priority >= virLogOutputs[i].priority) - virLogOutputs[i].f(virLogOutputs[i].data, category, priority, - msg, len); + virLogOutputs[i].f(category, priority, funcname, linenr, + msg, len, virLogOutputs[i].data); } if ((virLogNbOutputs == 0) && (flags != 1)) safewrite(2, msg, len); @@ -542,9 +551,11 @@ void virLogMessage(const char *category, int priority, int flags, VIR_FREE(msg); } -static int virLogOutputToFd(void *data, const char *category ATTRIBUTE_UNUSED, +static int virLogOutputToFd(const char *category ATTRIBUTE_UNUSED, int priority ATTRIBUTE_UNUSED, - const char *str, int len) { + const char *funcname ATTRIBUTE_UNUSED, + long long linenr ATTRIBUTE_UNUSED, + const char *str, int len, void *data) { int fd = (long) data; int ret; @@ -582,10 +593,12 @@ static int virLogAddOutputToFile(int priority, const char *file) { } #if HAVE_SYSLOG_H -static int virLogOutputToSyslog(void *data ATTRIBUTE_UNUSED, - const char *category ATTRIBUTE_UNUSED, - int priority, const char *str, - int len ATTRIBUTE_UNUSED) { +static int virLogOutputToSyslog(const char *category ATTRIBUTE_UNUSED, + int priority, + const char *funcname ATTRIBUTE_UNUSED, + long long linenr ATTRIBUTE_UNUSED, + const char *str, int len ATTRIBUTE_UNUSED, + void *data ATTRIBUTE_UNUSED) { int prio; switch (priority) { diff --git a/src/logging.h b/src/logging.h index f6aab41eac..84263702a8 100644 --- a/src/logging.h +++ b/src/logging.h @@ -30,16 +30,22 @@ * defined at runtime of from the libvirt daemon configuration file */ #ifdef ENABLE_DEBUG -#define VIR_DEBUG(category, fmt,...) \ - virLogMessage(category, VIR_LOG_DEBUG, 0, fmt, __VA_ARGS__) -#define VIR_INFO(category, fmt,...) \ - virLogMessage(category, VIR_LOG_INFO, 0, fmt, __VA_ARGS__) -#define VIR_WARN(category, fmt,...) \ - virLogMessage(category, VIR_LOG_WARN, 0, fmt, __VA_ARGS__) -#define VIR_ERROR(category, fmt,...) \ - virLogMessage(category, VIR_LOG_ERROR, 0, fmt, __VA_ARGS__) +#define VIR_DEBUG(category, f, l, fmt,...) \ + virLogMessage(category, VIR_LOG_DEBUG, f, l, 0, fmt, __VA_ARGS__) +#define VIR_INFO(category, f, l, fmt,...) \ + virLogMessage(category, VIR_LOG_INFO, f, l, 0, fmt, __VA_ARGS__) +#define VIR_WARN(category, f, l, fmt,...) \ + virLogMessage(category, VIR_LOG_WARN, f, l, 0, fmt, __VA_ARGS__) +#define VIR_ERROR(category, f, l, fmt,...) \ + virLogMessage(category, VIR_LOG_ERROR, f, l, 0, fmt, __VA_ARGS__) #else -#define VIR_DEBUG(category, fmt,...) \ +#define VIR_DEBUG(category, f, l, fmt,...) \ + do { } while (0) +#define VIR_INFO(category, f, l, fmt,...) \ + do { } while (0) +#define VIR_WARN(category, f, l, fmt,...) \ + do { } while (0) +#define VIR_ERROR(category, f, l, fmt,...) \ do { } while (0) #define VIR_INFO(category, fmt,...) \ do { } while (0) @@ -49,14 +55,22 @@ do { } while (0) #endif /* !ENABLE_DEBUG */ -#define DEBUG(fmt,...) VIR_DEBUG(__FILE__, fmt, __VA_ARGS__) -#define DEBUG0(msg) VIR_DEBUG(__FILE__, "%s", msg) -#define INFO(fmt,...) VIR_INFO(__FILE__, fmt, __VA_ARGS__) -#define INFO0(msg) VIR_INFO(__FILE__, "%s", msg) -#define WARN(fmt,...) VIR_WARN(__FILE__, fmt, __VA_ARGS__) -#define WARN0(msg) VIR_WARN(__FILE__, "%s", msg) -#define ERROR(fmt,...) VIR_ERROR(__FILE__, fmt, __VA_ARGS__) -#define ERROR0(msg) VIR_ERROR(__FILE__, "%s", msg) +#define DEBUG(fmt,...) \ + VIR_DEBUG("file." __FILE__, __func__, __LINE__, fmt, __VA_ARGS__) +#define DEBUG0(msg) \ + VIR_DEBUG("file." __FILE__, __func__, __LINE__, "%s", msg) +#define INFO(fmt,...) \ + VIR_INFO("file." __FILE__, __func__, __LINE__, fmt, __VA_ARGS__) +#define INFO0(msg) \ + VIR_INFO("file." __FILE__, __func__, __LINE__, "%s", msg) +#define WARN(fmt,...) \ + VIR_WARN("file." __FILE__, __func__, __LINE__, fmt, __VA_ARGS__) +#define WARN0(msg) \ + VIR_WARN("file." __FILE__, __func__, __LINE__, "%s", msg) +#define ERROR(fmt,...) \ + VIR_ERROR("file." __FILE__, __func__, __LINE__, fmt, __VA_ARGS__) +#define ERROR0(msg) \ + VIR_ERROR("file." __FILE__, __func__, __LINE__, "%s", msg) /* @@ -71,18 +85,21 @@ typedef enum { /** * virLogOutputFunc: - * @data: extra output logging data * @category: the category for the message * @priority: the priority for the message + * @funcname: the function emitting the message + * @linenr: line where the message was emitted * @msg: the message to log, preformatted and zero terminated * @len: the lenght of the message in bytes without the terminating zero + * @data: extra output logging data * * Callback function used to output messages * * Returns the number of bytes written or -1 in case of error */ -typedef int (*virLogOutputFunc) (void *data, const char *category, - int priority, const char *str, int len); +typedef int (*virLogOutputFunc) (const char *category, int priority, + const char *funcname, long long lineno, + const char *str, int len, void *data); /** * virLogCloseFunc: @@ -110,7 +127,8 @@ extern int virLogReset(void); extern void virLogShutdown(void); extern int virLogParseFilters(const char *filters); extern int virLogParseOutputs(const char *output); -extern void virLogMessage(const char *category, int priority, int flags, - const char *fmt, ...) ATTRIBUTE_FORMAT(printf, 4, 5); +extern void virLogMessage(const char *category, int priority, + const char *funcname, long long linenr, int flags, + const char *fmt, ...) ATTRIBUTE_FORMAT(printf, 6, 7); #endif