Browse Source

[trac901] Output message at destruction

Michal 'vorner' Vaner 14 years ago
parent
commit
2aca19e705

+ 11 - 5
src/lib/log/log_formatter.h

@@ -48,10 +48,14 @@ namespace log {
 ///
 ///
 /// User of logging code should not really care much about this class, only
 /// User of logging code should not really care much about this class, only
 /// call the .arg method to generate the correct output.
 /// call the .arg method to generate the correct output.
+///
+/// The class is a template to allow easy testing. Also, we want everything
+/// here in the header anyway and it doesn't depend on the details of what
+/// Logger really is, so it doesn't hurt anything.
 template<class Logger> class Formatter {
 template<class Logger> class Formatter {
 private:
 private:
     /// \brief The logger we will use to output the final message
     /// \brief The logger we will use to output the final message
-    Logger& logger_;
+    Logger* logger_;
     /// \brief Prefix (eg. "ERROR", "DEBUG" or like that)
     /// \brief Prefix (eg. "ERROR", "DEBUG" or like that)
     const char* prefix_;
     const char* prefix_;
     /// \brief The messages with %1, %2... placeholders
     /// \brief The messages with %1, %2... placeholders
@@ -74,15 +78,15 @@ public:
     /// \param logger The logger where the final output will go.
     /// \param logger The logger where the final output will go.
     Formatter(const char* prefix, const std::string& message,
     Formatter(const char* prefix, const std::string& message,
               const unsigned& nextPlaceholder, Logger& logger) :
               const unsigned& nextPlaceholder, Logger& logger) :
-        prefix_(prefix), message_(message),
-        nextPlaceholder_(nextPlaceholder), logger_(logger),
-        active_(true)
+        logger_(&logger), prefix_(prefix), message_(message),
+        nextPlaceholder_(nextPlaceholder), active_(true)
     {
     {
     }
     }
     /// \brief Constructor of "inactive" formatter
     /// \brief Constructor of "inactive" formatter
     ///
     ///
     /// This will create a formatter that produces no output.
     /// This will create a formatter that produces no output.
     Formatter() :
     Formatter() :
+        nextPlaceholder_(0),
         active_(false)
         active_(false)
     {
     {
     }
     }
@@ -90,7 +94,9 @@ public:
     //
     //
     /// This is the place where output happens if the formatter is active.
     /// This is the place where output happens if the formatter is active.
     ~ Formatter() {
     ~ Formatter() {
-
+        if (active_) {
+            logger_->output(prefix_, message_);
+        }
     }
     }
     /// \brief Replaces another placeholder
     /// \brief Replaces another placeholder
     ///
     ///

+ 1 - 0
src/lib/log/tests/Makefile.am

@@ -22,6 +22,7 @@ run_unittests_SOURCES += message_reader_unittest.cc
 run_unittests_SOURCES += message_initializer_unittest.cc
 run_unittests_SOURCES += message_initializer_unittest.cc
 run_unittests_SOURCES += message_initializer_unittest_2.cc
 run_unittests_SOURCES += message_initializer_unittest_2.cc
 run_unittests_SOURCES += run_unittests.cc
 run_unittests_SOURCES += run_unittests.cc
+run_unittests_SOURCES += log_formatter_unittest.cc
 
 
 run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
 run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
 run_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS)
 run_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS)

+ 52 - 0
src/lib/log/tests/log_formatter_unittest.cc

@@ -0,0 +1,52 @@
+// Copyright (C) 2011  Internet Systems Consortium, Inc. ("ISC")
+//
+// Permission to use, copy, modify, and/or distribute this software for any
+// purpose with or without fee is hereby granted, provided that the above
+// copyright notice and this permission notice appear in all copies.
+//
+// THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
+// REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
+// AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
+// INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
+// LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
+// OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
+// PERFORMANCE OF THIS SOFTWARE.
+
+#include <gtest/gtest.h>
+#include <log/log_formatter.h>
+
+#include <vector>
+#include <string>
+
+using namespace std;
+
+namespace {
+
+class FormatterTest : public ::testing::Test {
+protected:
+    typedef pair<const char*, string> Output;
+    typedef isc::log::Formatter<FormatterTest> Formatter;
+    vector<Output> outputs;
+public:
+    void output(const char* prefix, const string& message) {
+        outputs.push_back(Output(prefix, message));
+    }
+};
+
+// Create an inactive formatter and check it doesn't produce any output
+TEST_F(FormatterTest, inactive) {
+    Formatter();
+    EXPECT_EQ(0, outputs.size());
+}
+
+// Create an active formatter and check it produces output. Does no arg
+// substitution yet
+TEST_F(FormatterTest, active) {
+    Formatter("TEST", "Text of message", 1, *this);
+    ASSERT_LE(1, outputs.size());
+    EXPECT_EQ(1, outputs.size());
+    EXPECT_STREQ("TEST", outputs[0].first);
+    EXPECT_EQ("Text of message", outputs[0].second);
+}
+
+}