Browse Source

[trac558] Remove macros from logger formatting

Argumennt information is now passed to the implementation class, which
looks up the format string in the global dictionary and performs the
argument substitution.
Stephen Morris 14 years ago
parent
commit
13a3b9f237
3 changed files with 46 additions and 43 deletions
  1. 20 29
      src/lib/log/logger.cc
  2. 8 2
      src/lib/log/logger_impl.cc
  3. 18 12
      src/lib/log/logger_impl.h

+ 20 - 29
src/lib/log/logger.cc

@@ -110,20 +110,6 @@ Logger::isFatalEnabled() {
 // definition of the macro).  Also note that it expects that the message buffer
 // "message" is declared in the compilation unit.
 
-namespace {
-const size_t MESSAGE_SIZE = 512;
-}
-
-#define FORMAT_MESSAGE(message) \
-    { \
-    MessageDictionary& global = MessageDictionary::globalDictionary(); \
-    string format = global.getText(ident); \
-    va_list ap; \
-    va_start(ap, ident); \
-    vsnprintf(message, sizeof(message), format.c_str(), ap); \
-    message[sizeof(message) - 1] = '\0'; \
-    va_end(ap); \
-    }
     
 
 // Output methods
@@ -131,45 +117,50 @@ const size_t MESSAGE_SIZE = 512;
 void
 Logger::debug(int dbglevel, const isc::log::MessageID& ident, ...) {
     if (isDebugEnabled(dbglevel)) {
-        char message[MESSAGE_SIZE];
-        FORMAT_MESSAGE(message);
-        getLoggerPtr()->debug(ident, message);
+        va_list ap;
+        va_start(ap, ident);
+        getLoggerPtr()->debug(ident, ap);
+        va_end(ap);
     }
 }
 
 void
 Logger::info(const isc::log::MessageID& ident, ...) {
     if (isInfoEnabled()) {
-        char message[MESSAGE_SIZE];
-        FORMAT_MESSAGE(message);
-        getLoggerPtr()->info(ident, message);
+        va_list ap;
+        va_start(ap, ident);
+        getLoggerPtr()->info(ident, ap);
+        va_end(ap);
     }
 }
 
 void
 Logger::warn(const isc::log::MessageID& ident, ...) {
     if (isWarnEnabled()) {
-        char message[MESSAGE_SIZE];
-        FORMAT_MESSAGE(message);
-        getLoggerPtr()->warn(ident, message);
+        va_list ap;
+        va_start(ap, ident);
+        getLoggerPtr()->warn(ident, ap);
+        va_end(ap);
     }
 }
 
 void
 Logger::error(const isc::log::MessageID& ident, ...) {
     if (isErrorEnabled()) {
-        char message[MESSAGE_SIZE];
-        FORMAT_MESSAGE(message);
-        getLoggerPtr()->error(ident, message);
+        va_list ap;
+        va_start(ap, ident);
+        getLoggerPtr()->error(ident, ap);
+        va_end(ap);
     }
 }
 
 void
 Logger::fatal(const isc::log::MessageID& ident, ...) {
     if (isFatalEnabled()) {
-        char message[MESSAGE_SIZE];
-        FORMAT_MESSAGE(message);
-        getLoggerPtr()->fatal(ident, message);
+        va_list ap;
+        va_start(ap, ident);
+        getLoggerPtr()->fatal(ident, ap);
+        va_end(ap);
     }
 }
 

+ 8 - 2
src/lib/log/logger_impl.cc

@@ -196,8 +196,14 @@ LoggerImpl::isDebugEnabled(int dbglevel) {
 
 void
 LoggerImpl::output(const char* sev_text, const MessageID& ident,
-    const char* text)
+    va_list ap)
 {
+    char message[512];      // Should be large enough for any message
+
+    // Obtain text of the message and substitute arguments.
+    const string format = MessageDictionary::globalDictionary().getText(ident);
+    vsnprintf(message, sizeof(message), format.c_str(), ap);
+    message[sizeof(message) - 1] = '\0';    // Guarantee trailing NULL
 
     // Get the time in a struct tm format, and convert to text
     time_t t_time;
@@ -210,7 +216,7 @@ LoggerImpl::output(const char* sev_text, const MessageID& ident,
 
     // Now output.
     std::cout << chr_time << " " << sev_text << " [" << getName() << "] " <<
-        ident << ", " << text << "\n";
+        ident << ", " << message << "\n";
 }
 
 } // namespace log

+ 18 - 12
src/lib/log/logger_impl.h

@@ -15,6 +15,7 @@
 #ifndef __LOGGER_IMPL_H
 #define __LOGGER_IMPL_H
 
+#include <stdarg.h>
 #include <time.h>
 
 #include <cstdlib>
@@ -175,17 +176,18 @@ public:
     ///
     /// \param sev_text Severity level as a text string
     /// \param ident Message identification
-    /// \param text Text to log
+    /// \param ap Variable argument list holding message arguments
     void output(const char* sev_text, const MessageID& ident,
-        const char* text);
+        va_list ap);
 
 
     /// \brief Output Debug Message
     ///
     /// \param ident Message identification.
     /// \param text Text to log
-    void debug(const MessageID& ident, const char* text) {
-        output("DEBUG", ident, text);
+    /// \param ap Variable argument list holding message arguments
+    void debug(const MessageID& ident, va_list ap) {
+        output("DEBUG", ident, ap);
     }
 
 
@@ -193,32 +195,36 @@ public:
     ///
     /// \param ident Message identification.
     /// \param text Text to log
-    void info(const MessageID& ident, const char* text) {
-        output("INFO ", ident, text);
+    /// \param ap Variable argument list holding message arguments
+    void info(const MessageID& ident, va_list ap) {
+        output("INFO ", ident, ap);
     }
 
     /// \brief Output Warning Message
     ///
     /// \param ident Message identification.
     /// \param text Text to log
-    void warn(const MessageID& ident, const char* text) {
-        output("WARN ", ident, text);
+    /// \param ap Variable argument list holding message arguments
+    void warn(const MessageID& ident, va_list ap) {
+        output("WARN ", ident, ap);
     }
 
     /// \brief Output Error Message
     ///
     /// \param ident Message identification.
     /// \param text Text to log
-    void error(const MessageID& ident, const char* text) {
-        output("ERROR", ident, text);
+    /// \param ap Variable argument list holding message arguments
+    void error(const MessageID& ident, va_list ap) {
+        output("ERROR", ident, ap);
     }
 
     /// \brief Output Fatal Message
     ///
     /// \param ident Message identification.
     /// \param text Text to log
-    void fatal(const MessageID& ident, const char* text) {
-        output("FATAL", ident, text);
+    /// \param ap Variable argument list holding message arguments
+    void fatal(const MessageID& ident, va_list ap) {
+        output("FATAL", ident, ap);
     }
 
     /// \brief Equality