[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]

[libvirt] [PATCH 6/9] Change logging category parameter into an enum



From: "Daniel P. Berrange" <berrange redhat com>

The 'const char *category' parameter only has a few possible
values now that the filename has been separated. Turn this
parameter into an enum instead.

Signed-off-by: Daniel P. Berrange <berrange redhat com>
---
 src/internal.h                     |  6 +++--
 src/node_device/node_device_udev.c |  2 +-
 src/uml/uml_conf.c                 |  3 ---
 src/util/logging.c                 | 49 ++++++++++++++++++---------------
 src/util/logging.h                 | 55 ++++++++++++++++++++++----------------
 src/util/viraudit.c                |  9 ++++---
 src/util/viraudit.h                |  6 ++---
 src/util/virterror.c               |  5 ++--
 tests/testutils.c                  |  2 +-
 9 files changed, 76 insertions(+), 61 deletions(-)

diff --git a/src/internal.h b/src/internal.h
index a1d46b9..2d378fd 100644
--- a/src/internal.h
+++ b/src/internal.h
@@ -349,7 +349,8 @@
 
 #  define PROBE_EXPAND(NAME, ARGS) NAME(ARGS)
 #  define PROBE(NAME, FMT, ...)                              \
-    VIR_DEBUG_INT("trace", __FILE__ , __LINE__, __func__,    \
+    VIR_DEBUG_INT(VIR_LOG_FROM_TRACE,                       \
+                  __FILE__ , __LINE__, __func__,             \
                   #NAME ": " FMT, __VA_ARGS__);              \
     if (LIBVIRT_ ## NAME ## _ENABLED()) {                    \
         PROBE_EXPAND(LIBVIRT_ ## NAME,                       \
@@ -357,7 +358,8 @@
     }
 # else
 #  define PROBE(NAME, FMT, ...)                              \
-    VIR_DEBUG_INT("trace", __FILE__, __LINE__, __func__,     \
+    VIR_DEBUG_INT(VIR_LOG_FROM_TRACE,                       \
+                  __FILE__ , __LINE__, __func__,             \
                   #NAME ": " FMT, __VA_ARGS__);
 # endif
 
diff --git a/src/node_device/node_device_udev.c b/src/node_device/node_device_udev.c
index 2104bd3..943e020 100644
--- a/src/node_device/node_device_udev.c
+++ b/src/node_device/node_device_udev.c
@@ -358,7 +358,7 @@ static void udevLogFunction(struct udev *udev ATTRIBUTE_UNUSED,
                             const char *fmt,
                             va_list args)
 {
-    VIR_ERROR_INT("library", file, line, fn, fmt, args);
+    VIR_ERROR_INT(VIR_LOG_FROM_LIBRARY, file, line, fn, fmt, args);
 }
 
 
diff --git a/src/uml/uml_conf.c b/src/uml/uml_conf.c
index a317bcc..d0a2d97 100644
--- a/src/uml/uml_conf.c
+++ b/src/uml/uml_conf.c
@@ -52,9 +52,6 @@
 
 #define VIR_FROM_THIS VIR_FROM_UML
 
-#define umlLog(level, msg, ...)                                     \
-        virLogMessage(__FILE__, level, 0, msg, __VA_ARGS__)
-
 
 static int umlDefaultConsoleType(const char *ostype ATTRIBUTE_UNUSED)
 {
diff --git a/src/util/logging.c b/src/util/logging.c
index ceab64d..fef4577 100644
--- a/src/util/logging.c
+++ b/src/util/logging.c
@@ -47,6 +47,14 @@
 
 #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
  */
@@ -98,7 +106,7 @@ static virLogPriority virLogDefaultPriority = VIR_LOG_DEFAULT;
 
 static int virLogResetFilters(void);
 static int virLogResetOutputs(void);
-static void virLogOutputToFd(const char *category,
+static void virLogOutputToFd(virLogSource src,
                              virLogPriority priority,
                              const char *filename,
                              size_t linenr,
@@ -703,11 +711,11 @@ virLogVersionString(const char **rawmsg,
 
 /**
  * virLogMessage:
- * @category: where is that message coming from
+ * @source: where is that message coming from
  * @priority: the priority level
- * @funcname: the function emitting the (debug) message
+ * @filename: file where the message was emitted
  * @linenr: line where the message was emitted
- * @flags: extra flags, 1 if coming from the error handler
+ * @funcname: the function emitting the (debug) message
  * @fmt: the string format
  * @...: the arguments
  *
@@ -715,30 +723,29 @@ virLogVersionString(const char **rawmsg,
  * the message may be stored, sent to output or just discarded
  */
 void
-virLogMessage(const char *category,
+virLogMessage(virLogSource source,
               virLogPriority priority,
               const char *filename,
               size_t linenr,
               const char *funcname,
-              unsigned int flags,
               const char *fmt, ...)
 {
     va_list ap;
     va_start(ap, fmt);
-    virLogVMessage(category, priority,
+    virLogVMessage(source, priority,
                    filename, linenr, funcname,
-                   flags, fmt, ap);
+                   fmt, ap);
     va_end(ap);
 }
 
 
 /**
  * virLogVMessage:
- * @category: where is that message coming from
+ * @source: where is that message coming from
  * @priority: the priority level
- * @funcname: the function emitting the (debug) message
+ * @filename: file where the message was emitted
  * @linenr: line where the message was emitted
- * @flags: extra flags, 1 if coming from the error handler
+ * @funcname: the function emitting the (debug) message
  * @fmt: the string format
  * @vargs: format args
  *
@@ -746,12 +753,11 @@ virLogMessage(const char *category,
  * the message may be stored, sent to output or just discarded
  */
 void
-virLogVMessage(const char *category,
+virLogVMessage(virLogSource source,
                virLogPriority priority,
                const char *filename,
                size_t linenr,
                const char *funcname,
-               unsigned int flags,
                const char *fmt,
                va_list vargs)
 {
@@ -773,7 +779,7 @@ virLogVMessage(const char *category,
     /*
      * check against list of specific logging patterns
      */
-    fprio = virLogFiltersCheck(category, &filterflags);
+    fprio = virLogFiltersCheck(filename, &filterflags);
     if (fprio == 0) {
         if (priority < virLogDefaultPriority)
             emit = 0;
@@ -820,32 +826,32 @@ virLogVMessage(const char *category,
                 const char *rawver;
                 char *ver = NULL;
                 if (virLogVersionString(&rawver, &ver) >= 0)
-                    virLogOutputs[i].f(category, VIR_LOG_INFO,
+                    virLogOutputs[i].f(VIR_LOG_FROM_FILE, VIR_LOG_INFO,
                                        __FILE__, __LINE__, __func__,
                                        timestamp, 0, rawver, ver,
                                        virLogOutputs[i].data);
                 VIR_FREE(ver);
                 virLogOutputs[i].logVersion = false;
             }
-            virLogOutputs[i].f(category, priority,
+            virLogOutputs[i].f(source, priority,
                                filename, linenr, funcname,
                                timestamp, filterflags,
                                str, msg, virLogOutputs[i].data);
         }
     }
-    if ((virLogNbOutputs == 0) && (flags != 1)) {
+    if ((virLogNbOutputs == 0) && (source != VIR_LOG_FROM_ERROR)) {
         if (logVersionStderr) {
             const char *rawver;
             char *ver = NULL;
             if (virLogVersionString(&rawver, &ver) >= 0)
-                virLogOutputToFd(category, VIR_LOG_INFO,
+                virLogOutputToFd(VIR_LOG_FROM_FILE, VIR_LOG_INFO,
                                  __FILE__, __LINE__, __func__,
                                  timestamp, 0, rawver, ver,
                                  (void *) STDERR_FILENO);
             VIR_FREE(ver);
             logVersionStderr = false;
         }
-        virLogOutputToFd(category, priority,
+        virLogOutputToFd(source, priority,
                          filename, linenr, funcname,
                          timestamp, filterflags,
                          str, msg, (void *) STDERR_FILENO);
@@ -879,9 +885,8 @@ virLogStackTraceToFd(int fd)
 #undef STRIP_DEPTH
 }
 
-
 static void
-virLogOutputToFd(const char *category ATTRIBUTE_UNUSED,
+virLogOutputToFd(virLogSource source ATTRIBUTE_UNUSED,
                  virLogPriority priority ATTRIBUTE_UNUSED,
                  const char *filename ATTRIBUTE_UNUSED,
                  size_t linenr ATTRIBUTE_UNUSED,
@@ -967,7 +972,7 @@ virLogPrioritySyslog(virLogPriority priority)
 
 
 static void
-virLogOutputToSyslog(const char *category ATTRIBUTE_UNUSED,
+virLogOutputToSyslog(virLogSource source ATTRIBUTE_UNUSED,
                      virLogPriority priority,
                      const char *filename ATTRIBUTE_UNUSED,
                      size_t linenr ATTRIBUTE_UNUSED,
diff --git a/src/util/logging.h b/src/util/logging.h
index 25e6a1c..e5918db 100644
--- a/src/util/logging.h
+++ b/src/util/logging.h
@@ -31,8 +31,8 @@
  * defined at runtime from the libvirt daemon configuration file
  */
 # ifdef ENABLE_DEBUG
-#  define VIR_DEBUG_INT(category, filename, linenr, funcname, ...)                            \
-    virLogMessage(category, VIR_LOG_DEBUG, filename, linenr, funcname, 0, __VA_ARGS__)
+#  define VIR_DEBUG_INT(src, filename, linenr, funcname, ...)           \
+    virLogMessage(src, VIR_LOG_DEBUG, filename, linenr, funcname, __VA_ARGS__)
 # else
 /**
  * virLogEatParams:
@@ -44,25 +44,25 @@ static inline void virLogEatParams(const char *unused, ...)
     /* Silence gcc */
     unused = unused;
 }
-#  define VIR_DEBUG_INT(category, filename, linenr, funcname, ...)    \
-    virLogEatParams(category, filename, linenr, funcname, __VA_ARGS__)
+#  define VIR_DEBUG_INT(src, filename, linenr, funcname, ...)           \
+    virLogEatParams(src, filename, linenr, funcname, __VA_ARGS__)
 # endif /* !ENABLE_DEBUG */
 
-# define VIR_INFO_INT(category, filename, linenr, funcname, ...)                              \
-    virLogMessage(category, VIR_LOG_INFO, filename, linenr, funcname, 0, __VA_ARGS__)
-# define VIR_WARN_INT(category, filename, linenr, funcname, ...)                              \
-    virLogMessage(category, VIR_LOG_WARN, filename, linenr, funcname, 0, __VA_ARGS__)
-# define VIR_ERROR_INT(category, filename, linenr, funcname, ...)                             \
-    virLogMessage(category, VIR_LOG_ERROR, filename, linenr, funcname, 0, __VA_ARGS__)
+# define VIR_INFO_INT(src, filename, linenr, funcname, ...)             \
+    virLogMessage(src, VIR_LOG_INFO, filename, linenr, funcname, __VA_ARGS__)
+# define VIR_WARN_INT(src, filename, linenr, funcname, ...)             \
+    virLogMessage(src, VIR_LOG_WARN, filename, linenr, funcname, __VA_ARGS__)
+# define VIR_ERROR_INT(src, filename, linenr, funcname, ...)            \
+    virLogMessage(src, VIR_LOG_ERROR, filename, linenr, funcname, __VA_ARGS__)
 
 # define VIR_DEBUG(...)                                                 \
-        VIR_DEBUG_INT("file", __FILE__, __LINE__, __func__, __VA_ARGS__)
+    VIR_DEBUG_INT(VIR_LOG_FROM_FILE, __FILE__, __LINE__, __func__, __VA_ARGS__)
 # define VIR_INFO(...)                                                  \
-        VIR_INFO_INT("file", __FILE__, __LINE__, __func__, __VA_ARGS__)
+    VIR_INFO_INT(VIR_LOG_FROM_FILE, __FILE__, __LINE__, __func__, __VA_ARGS__)
 # define VIR_WARN(...)                                                  \
-        VIR_WARN_INT("file", __FILE__, __LINE__, __func__, __VA_ARGS__)
+    VIR_WARN_INT(VIR_LOG_FROM_FILE, __FILE__, __LINE__, __func__, __VA_ARGS__)
 # define VIR_ERROR(...)                                                 \
-        VIR_ERROR_INT("file", __FILE__, __LINE__, __func__, __VA_ARGS__)
+    VIR_ERROR_INT(VIR_LOG_FROM_FILE, __FILE__, __LINE__, __func__, __VA_ARGS__)
 
 /*
  * To be made public
@@ -82,12 +82,23 @@ typedef enum {
     VIR_LOG_TO_FILE,
 } virLogDestination;
 
+typedef enum {
+    VIR_LOG_FROM_FILE,
+    VIR_LOG_FROM_ERROR,
+    VIR_LOG_FROM_AUDIT,
+    VIR_LOG_FROM_TRACE,
+    VIR_LOG_FROM_LIBRARY,
+
+    VIR_LOG_FROM_LAST,
+} virLogSource;
+
 /**
  * virLogOutputFunc:
- * @category: the category for the message
+ * @src: the src for the message
  * @priority: the priority for the message
- * @funcname: the function emitting the message
+ * @filename: file where the message was emitted
  * @linenr: line where the message was emitted
+ * @funcname: the function emitting the message
  * @timestamp: zero terminated string with timestamp of the message
  * @flags: flags associated with the message
  * @rawstr: the unformatted message to log, zero terminated
@@ -96,7 +107,7 @@ typedef enum {
  *
  * Callback function used to output messages
  */
-typedef void (*virLogOutputFunc) (const char *category,
+typedef void (*virLogOutputFunc) (virLogSource src,
                                   virLogPriority priority,
                                   const char *filename,
                                   size_t linenr,
@@ -147,21 +158,19 @@ extern int virLogReset(void);
 extern int virLogParseDefaultPriority(const char *priority);
 extern int virLogParseFilters(const char *filters);
 extern int virLogParseOutputs(const char *output);
-extern void virLogMessage(const char *category,
+extern void virLogMessage(virLogSource src,
                           virLogPriority priority,
                           const char *filename,
                           size_t linenr,
                           const char *funcname,
-                          unsigned int flags,
-                          const char *fmt, ...) ATTRIBUTE_FMT_PRINTF(7, 8);
-extern void virLogVMessage(const char *category,
+                          const char *fmt, ...) ATTRIBUTE_FMT_PRINTF(6, 7);
+extern void virLogVMessage(virLogSource src,
                            virLogPriority priority,
                            const char *filename,
                            size_t linenr,
                            const char *funcname,
-                           unsigned int flags,
                            const char *fmt,
-                           va_list vargs) ATTRIBUTE_FMT_PRINTF(7, 0);
+                           va_list vargs) ATTRIBUTE_FMT_PRINTF(6, 0);
 extern int virLogSetBufferSize(int size);
 extern void virLogEmergencyDumpAll(int signum);
 #endif
diff --git a/src/util/viraudit.c b/src/util/viraudit.c
index ea1a79b..61f09eb 100644
--- a/src/util/viraudit.c
+++ b/src/util/viraudit.c
@@ -74,8 +74,9 @@ void virAuditLog(int logging)
 }
 
 
-void virAuditSend(const char *file ATTRIBUTE_UNUSED, const char *func,
+void virAuditSend(const char *filename,
                   size_t linenr,
+                  const char *funcname,
                   const char *clienttty ATTRIBUTE_UNUSED,
                   const char *clientaddr ATTRIBUTE_UNUSED,
                   enum virAuditRecordType type ATTRIBUTE_UNUSED, bool success,
@@ -103,10 +104,12 @@ void virAuditSend(const char *file ATTRIBUTE_UNUSED, const char *func,
 
     if (auditlog && str) {
         if (success)
-            virLogMessage("audit", VIR_LOG_INFO, file, linenr, func, 0,
+            virLogMessage(VIR_LOG_FROM_AUDIT, VIR_LOG_INFO,
+                          filename, linenr, funcname,
                           "success=yes %s", str);
         else
-            virLogMessage("audit", VIR_LOG_WARN, file, linenr, func, 0,
+            virLogMessage(VIR_LOG_FROM_AUDIT, VIR_LOG_WARN,
+                          filename, linenr, funcname,
                           "success=no %s", str);
     }
 
diff --git a/src/util/viraudit.h b/src/util/viraudit.h
index 36fdd7f..b0356da 100644
--- a/src/util/viraudit.h
+++ b/src/util/viraudit.h
@@ -35,7 +35,7 @@ int virAuditOpen(void);
 
 void virAuditLog(int enabled);
 
-void virAuditSend(const char *file, const char *func, size_t linenr,
+void virAuditSend(const char *filename, size_t linenr, const char *funcname,
                   const char *clienttty, const char *clientaddr,
                   enum virAuditRecordType type, bool success,
                   const char *fmt, ...)
@@ -46,11 +46,11 @@ char *virAuditEncode(const char *key, const char *value);
 void virAuditClose(void);
 
 # define VIR_AUDIT(type, success, ...)				\
-    virAuditSend(__FILE__, __func__, __LINE__,			\
+    virAuditSend(__FILE__, __LINE__, __func__,                  \
                  NULL, NULL, type, success, __VA_ARGS__);
 
 # define VIR_AUDIT_USER(type, success, clienttty, clientaddr, ...)	\
-    virAuditSend(__FILE__, __func__, __LINE__,				\
+    virAuditSend(__FILE__, __LINE__, __func__,                          \
                  clienttty, clientaddr, type, success, __VA_ARGS__);
 
 # define VIR_AUDIT_STR(str) \
diff --git a/src/util/virterror.c b/src/util/virterror.c
index 53f80af..ed1ff87 100644
--- a/src/util/virterror.c
+++ b/src/util/virterror.c
@@ -671,14 +671,13 @@ virRaiseErrorFull(const char *filename ATTRIBUTE_UNUSED,
 
     /*
      * Hook up the error or warning to the logging facility
-     * XXXX should we include filename as 'category' instead of domain name ?
      */
     priority = virErrorLevelPriority(level);
     if (virErrorLogPriorityFilter)
         priority = virErrorLogPriorityFilter(to, priority);
-    virLogMessage("error", priority,
+    virLogMessage(virErrorLogPriorityFilter ? VIR_LOG_FROM_FILE : VIR_LOG_FROM_ERROR,
+                  priority,
                   filename, linenr, funcname,
-                  virErrorLogPriorityFilter ? 0 : 1,
                   "%s", str);
 
     errno = save_errno;
diff --git a/tests/testutils.c b/tests/testutils.c
index 244db43..0c52e12 100644
--- a/tests/testutils.c
+++ b/tests/testutils.c
@@ -480,7 +480,7 @@ struct virtTestLogData {
 static struct virtTestLogData testLog = { VIR_BUFFER_INITIALIZER };
 
 static void
-virtTestLogOutput(const char *category ATTRIBUTE_UNUSED,
+virtTestLogOutput(virLogSource source ATTRIBUTE_UNUSED,
                   virLogPriority priority ATTRIBUTE_UNUSED,
                   const char *filename ATTRIBUTE_UNUSED,
                   size_t lineno ATTRIBUTE_UNUSED,
-- 
1.7.11.4


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]