Browse Source

[trac901] Switch to new logging interface

Michal 'vorner' Vaner 14 years ago
parent
commit
eab5008d28

+ 9 - 2
src/lib/log/log_formatter.h

@@ -73,9 +73,8 @@ private:
     /// \brief Which will be the next placeholder to replace
     const unsigned nextPlaceholder_;
     /// \brief Should we do output?
-    bool active_;
+    mutable bool active_;
     Formatter& operator =(const Formatter& other);
-    Formatter(const Formatter& other);
 public:
     /// \brief Constructor of "active" formatter
     ///
@@ -109,6 +108,14 @@ public:
         active_(false)
     {
     }
+
+    Formatter(const Formatter& other) :
+        logger_(other.logger_), prefix_(other.prefix_),
+        message_(other.message_), nextPlaceholder_(other.nextPlaceholder_),
+        active_(other.active_)
+    {
+        other.active_ = false;
+    }
     /// \brief Destructor.
     //
     /// This is the place where output happens if the formatter is active.

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

@@ -112,52 +112,57 @@ Logger::isFatalEnabled() {
 // Output methods
 
 void
-Logger::debug(int dbglevel, const isc::log::MessageID& ident, ...) {
+Logger::output(const char* sevText, const string& message) {
+    getLoggerPtr()->outputRaw(sevText, message);
+}
+
+Logger::Formatter
+Logger::debug(int dbglevel, const isc::log::MessageID& ident) {
     if (isDebugEnabled(dbglevel)) {
-        va_list ap;
-        va_start(ap, ident);
-        getLoggerPtr()->debug(ident, ap);
-        va_end(ap);
+        return (Formatter("DEBUG", getLoggerPtr()->lookupMessage(ident), 1,
+                          *this));
+    } else {
+        return (Formatter());
     }
 }
 
-void
-Logger::info(const isc::log::MessageID& ident, ...) {
+Logger::Formatter
+Logger::info(const isc::log::MessageID& ident) {
     if (isInfoEnabled()) {
-        va_list ap;
-        va_start(ap, ident);
-        getLoggerPtr()->info(ident, ap);
-        va_end(ap);
+        return (Formatter("INFO ", getLoggerPtr()->lookupMessage(ident), 1,
+                          *this));
+    } else {
+        return (Formatter());
     }
 }
 
-void
-Logger::warn(const isc::log::MessageID& ident, ...) {
+Logger::Formatter
+Logger::warn(const isc::log::MessageID& ident) {
     if (isWarnEnabled()) {
-        va_list ap;
-        va_start(ap, ident);
-        getLoggerPtr()->warn(ident, ap);
-        va_end(ap);
+        return (Formatter("WARN ", getLoggerPtr()->lookupMessage(ident), 1,
+                          *this));
+    } else {
+        return (Formatter());
     }
 }
 
-void
-Logger::error(const isc::log::MessageID& ident, ...) {
+Logger::Formatter
+Logger::error(const isc::log::MessageID& ident) {
     if (isErrorEnabled()) {
-        va_list ap;
-        va_start(ap, ident);
-        getLoggerPtr()->error(ident, ap);
-        va_end(ap);
+        return (Formatter("ERROR", getLoggerPtr()->lookupMessage(ident), 1,
+                          *this));
+    } else {
+        return (Formatter());
     }
 }
 
-void
-Logger::fatal(const isc::log::MessageID& ident, ...) {
+Logger::Formatter
+Logger::fatal(const isc::log::MessageID& ident) {
     if (isFatalEnabled()) {
-        va_list ap;
-        va_start(ap, ident);
-        getLoggerPtr()->fatal(ident, ap);
-        va_end(ap);
+        return (Formatter("FATAL", getLoggerPtr()->lookupMessage(ident), 1,
+                          *this));
+    } else {
+        return (Formatter());
     }
 }
 

+ 9 - 10
src/lib/log/logger.h

@@ -21,6 +21,7 @@
 #include <log/debug_levels.h>
 #include <log/logger_levels.h>
 #include <log/message_types.h>
+#include <log/log_formatter.h>
 
 namespace isc {
 namespace log {
@@ -48,6 +49,9 @@ class LoggerImpl;   // Forward declaration of the implementation class
 class Logger {
 public:
 
+    typedef isc::log::Formatter<Logger> Formatter;
+    void output(const char* sevText, const std::string& message);
+
     /// \brief Constructor
     ///
     /// Creates/attaches to a logger of a specific name.
@@ -157,36 +161,31 @@ public:
     /// \param dbglevel Debug level, ranging between 0 and 99.  Higher numbers
     /// are used for more verbose output.
     /// \param ident Message identification.
-    /// \param ... Optional arguments for the message.
-    void debug(int dbglevel, const MessageID& ident, ...);
+    Formatter debug(int dbglevel, const MessageID& ident);
 
 
     /// \brief Output Informational Message
     ///
     /// \param ident Message identification.
-    /// \param ... Optional arguments for the message.
-    void info(const MessageID& ident, ...);
+    Formatter info(const MessageID& ident);
 
 
     /// \brief Output Warning Message
     ///
     /// \param ident Message identification.
-    /// \param ... Optional arguments for the message.
-    void warn(const MessageID& ident, ...);
+    Formatter warn(const MessageID& ident);
 
 
     /// \brief Output Error Message
     ///
     /// \param ident Message identification.
-    /// \param ... Optional arguments for the message.
-    void error(const MessageID& ident, ...);
+    Formatter error(const MessageID& ident);
 
 
     /// \brief Output Fatal Message
     ///
     /// \param ident Message identification.
-    /// \param ... Optional arguments for the message.
-    void fatal(const MessageID& ident, ...);
+    Formatter fatal(const MessageID& ident);
 
     /// \brief Equality
     ///

+ 5 - 5
src/lib/log/logger_support.cc

@@ -62,7 +62,7 @@ readLocalMessageFile(const char* file) {
     MessageDictionary& dictionary = MessageDictionary::globalDictionary();
     MessageReader reader(&dictionary);
     try {
-        logger.info(MSG_RDLOCMES, file);
+        logger.info(MSG_RDLOCMES).arg(file);
         reader.readFile(file, MessageReader::REPLACE);
 
         // File successfully read, list the duplicates
@@ -70,7 +70,7 @@ readLocalMessageFile(const char* file) {
         for (MessageReader::MessageIDCollection::const_iterator
             i = unknown.begin(); i != unknown.end(); ++i) {
             string message_id = boost::lexical_cast<string>(*i);
-                logger.warn(MSG_IDNOTFND, message_id.c_str());
+                logger.warn(MSG_IDNOTFND).arg(message_id);
         }
     }
     catch (MessageException& e) {
@@ -82,11 +82,11 @@ readLocalMessageFile(const char* file) {
             break;
 
         case 1:
-            logger.error(ident, args[0].c_str());
+            logger.error(ident).arg(args[0]);
             break;
 
         default:    // 2 or more (2 should be the maximum)
-            logger.error(ident, args[0].c_str(), args[1].c_str());
+            logger.error(ident).arg(args[0]).arg(args[1]);
         }
     }
 }
@@ -117,7 +117,7 @@ initLogger(const string& root, isc::log::Severity severity, int dbglevel,
         vector<string>::iterator new_end =
             unique(duplicates.begin(), duplicates.end());
         for (vector<string>::iterator i = duplicates.begin(); i != new_end; ++i) {
-            logger.warn(MSG_DUPMSGID, i->c_str());
+            logger.warn(MSG_DUPMSGID).arg(*i);
         }
 
     }

+ 12 - 12
src/lib/log/messagedef.cc

@@ -1,4 +1,4 @@
-// File created from messagedef.mes on Mon Feb 14 11:07:45 2011
+// File created from messagedef.mes on Thu May  5 16:57:11 2011
 
 #include <cstddef>
 #include <log/message_types.h>
@@ -33,21 +33,21 @@ namespace {
 const char* values[] = {
     "DUPLNS", "duplicate $NAMESPACE directive found",
     "DUPLPRFX", "duplicate $PREFIX directive found",
-    "DUPMSGID", "duplicate message ID (%s) in compiled code",
-    "IDNOTFND", "could not replace message for '%s': no such message identification",
-    "MSGRDERR", "error reading from message file %s: %s",
-    "MSGWRTERR", "error writing to %s: %s",
-    "NOMSGTXT", "a line containing a message ID ('%s') and nothing else was found",
+    "DUPMSGID", "duplicate message ID (%1) in compiled code",
+    "IDNOTFND", "could not replace message for '%1': no such message identification",
+    "MSGRDERR", "error reading from message file %1: %2",
+    "MSGWRTERR", "error writing to %1: %2",
+    "NOMSGTXT", "a line containing a message ID ('%1') and nothing else was found",
     "NSEXTRARG", "$NAMESPACE directive has too many arguments",
-    "NSINVARG", "$NAMESPACE directive has an invalid argument ('%s')",
+    "NSINVARG", "$NAMESPACE directive has an invalid argument ('%1')",
     "NSNOARG", "no arguments were given to the $NAMESPACE directive",
-    "OPNMSGIN", "unable to open message file %s for input: %s",
-    "OPNMSGOUT", "unable to open %s for output: %s",
+    "OPNMSGIN", "unable to open message file %1 for input: %2",
+    "OPNMSGOUT", "unable to open %1 for output: %2",
     "PRFEXTRARG", "$PREFIX directive has too many arguments",
-    "PRFINVARG", "$PREFIX directive has an invalid argument ('%s')",
+    "PRFINVARG", "$PREFIX directive has an invalid argument ('%1')",
     "PRFNOARG", "no arguments were given to the $PREFIX directive",
-    "RDLOCMES", "reading local message file %s",
-    "UNRECDIR", "unrecognised directive '%s'",
+    "RDLOCMES", "reading local message file %1",
+    "UNRECDIR", "unrecognised directive '%1'",
     NULL
 };
 

+ 1 - 1
src/lib/log/messagedef.h

@@ -1,4 +1,4 @@
-// File created from messagedef.mes on Mon Feb 14 11:07:45 2011
+// File created from messagedef.mes on Thu May  5 16:57:11 2011
 
 #ifndef __MESSAGEDEF_H
 #define __MESSAGEDEF_H

+ 11 - 11
src/lib/log/messagedef.mes

@@ -23,7 +23,7 @@ $NAMESPACE isc::log
 # chicken-and-egg situation where we need the files to build the message
 # compiler, yet we need the compiler to build the files.
 
-DUPMSGID  duplicate message ID (%s) in compiled code
+DUPMSGID  duplicate message ID (%1) in compiled code
 + Indicative of a programming error, when it started up, BIND10 detected that
 + the given message ID had been registered by one or more modules.  (All message
 + IDs should be unique throughout BIND10.)  This has no impact on the operation
@@ -44,7 +44,7 @@ DUPLPRFX    duplicate $PREFIX directive found
 + this version of the code, such a condition is regarded as an error and the
 + read will be abandoned.
 
-IDNOTFND    could not replace message for '%s': no such message identification
+IDNOTFND    could not replace message for '%1': no such message identification
 + During start-up a local message file was read.  A line with the listed
 + message identification was found in the file, but the identification is not
 + one contained in the compiled-in message dictionary.  Either the message
@@ -55,10 +55,10 @@ IDNOTFND    could not replace message for '%s': no such message identification
 + This message may appear a number of times in the file, once for every such
 + unknown message identification.
 
-MSGRDERR    error reading from message file %s: %s
+MSGRDERR    error reading from message file %1: %2
 + The specified error was encountered reading from the named message file.
 
-MSGWRTERR   error writing to %s: %s
+MSGWRTERR   error writing to %1: %2
 + The specified error was encountered by the message compiler when writing to
 + the named output file.
 
@@ -67,13 +67,13 @@ NSEXTRARG  $NAMESPACE directive has too many arguments
 + generated symbol names are placed.  This error is generated when the
 + compiler finds a $NAMESPACE directive with more than one argument.
 
-NSINVARG    $NAMESPACE directive has an invalid argument ('%s')
+NSINVARG    $NAMESPACE directive has an invalid argument ('%1')
 + The $NAMESPACE argument should be a valid C++ namespace.  The reader does a
 + cursory check on its validity, checking that the characters in the namespace
 + are correct.  The error is generated when the reader finds an invalid
 + character. (Valid are alphanumeric characters, underscores and colons.)
 
-NOMSGTXT    a line containing a message ID ('%s') and nothing else was found
+NOMSGTXT    a line containing a message ID ('%1') and nothing else was found
 + Message definitions comprise lines starting with a message identification (a
 + symbolic name for the message) and followed by the text of the message.  This
 + error is generated when a line is found in the message file that contains just
@@ -84,11 +84,11 @@ NSNOARG     no arguments were given to the $NAMESPACE directive
 + generated symbol names are placed.  This error is generated when the
 + compiler finds a $NAMESPACE directive with no arguments.
 
-OPNMSGIN     unable to open message file %s for input: %s
+OPNMSGIN     unable to open message file %1 for input: %2
 + The program was not able to open the specified input message file for the
 + reason given.
 
-OPNMSGOUT   unable to open %s for output: %s
+OPNMSGOUT   unable to open %1 for output: %2
 + The program was not able to open the specified output file for the reason
 + given.
 
@@ -97,7 +97,7 @@ PRFEXTRARG  $PREFIX directive has too many arguments
 + symbol names when a C++ .h file is created.  This error is generated when the
 + compiler finds a $PREFIX directive with more than one argument.
 
-PRFINVARG   $PREFIX directive has an invalid argument ('%s')
+PRFINVARG   $PREFIX directive has an invalid argument ('%1')
 + The $PREFIX argument is used in a symbol name in a C++ header file.  As such,
 + it must adhere to restrictions on C++ symbol names (e.g. may only contain
 + alphanumeric characters or underscores, and may nor start with a digit).  A
@@ -109,11 +109,11 @@ PRFNOARG    no arguments were given to the $PREFIX directive
 + symbol names when a C++ .h file is created.  This error is generated when the
 + compiler finds a $PREFIX directive with no arguments.
 
-RDLOCMES    reading local message file %s
+RDLOCMES    reading local message file %1
 + This is an informational message output by BIND10 when it starts to read a
 + local message file.  (A local message file may replace the text of one of more
 + messages; the ID of the message will not be changed though.)
 
-UNRECDIR    unrecognised directive '%s'
+UNRECDIR    unrecognised directive '%1'
 + A line starting with a dollar symbol was found, but the first word on the line
 + (shown in the message) was not a recognised message compiler directive.

+ 8 - 8
src/lib/log/tests/logger_support_test.cc

@@ -92,13 +92,13 @@ int main(int argc, char** argv) {
     initLogger("alpha", severity, dbglevel, localfile);
 
     // Log a few messages
-    logger_ex.fatal(MSG_MSGWRTERR, "test1", "42");
-    logger_ex.error(MSG_UNRECDIR, "false");
-    logger_dlm.warn(MSG_MSGRDERR, "a.txt", "dummy test");
-    logger_dlm.info(MSG_OPNMSGIN, "example.msg", "dummy test");
-    logger_ex.debug(0, MSG_UNRECDIR, "[abc]");
-    logger_ex.debug(24, MSG_UNRECDIR, "[24]");
-    logger_ex.debug(25, MSG_UNRECDIR, "[25]");
-    logger_ex.debug(26, MSG_UNRECDIR, "[26]");
+    logger_ex.fatal(MSG_MSGWRTERR).arg("test1").arg("42");
+    logger_ex.error(MSG_UNRECDIR).arg("false");
+    logger_dlm.warn(MSG_MSGRDERR).arg("a.txt").arg("dummy test");
+    logger_dlm.info(MSG_OPNMSGIN).arg("example.msg").arg("dummy test");
+    logger_ex.debug(0, MSG_UNRECDIR).arg("[abc]");
+    logger_ex.debug(24, MSG_UNRECDIR).arg("[24]");
+    logger_ex.debug(25, MSG_UNRECDIR).arg("[25]");
+    logger_ex.debug(26, MSG_UNRECDIR).arg("[26]");
     return (0);
 }

+ 2 - 2
src/lib/log/tests/run_time_init_test.sh.in

@@ -30,8 +30,8 @@ passfail() {
 
 cat > $localmes << .
 NOTHERE     this message is not in the global dictionary
-MSGRDERR    replacement read error, parameters: '%s' and '%s'
-UNRECDIR    replacement unrecognised directive message, parameter is '%s'
+MSGRDERR    replacement read error, parameters: '%1' and '%2'
+UNRECDIR    replacement unrecognised directive message, parameter is '%1'
 .
 
 echo -n "1. runInitTest default parameters: "