BIND 10 trac901, updated. 2aca19e705f02400a6b213ef84fa81c86dff0375 [trac901] Output message at destruction
BIND 10 source code commits
bind10-changes at lists.isc.org
Wed May 4 14:30:43 UTC 2011
The branch, trac901 has been updated
via 2aca19e705f02400a6b213ef84fa81c86dff0375 (commit)
from cf46f370333b15487f6c02f85749d90c2e0bd710 (commit)
Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.
- Log -----------------------------------------------------------------
commit 2aca19e705f02400a6b213ef84fa81c86dff0375
Author: Michal 'vorner' Vaner <michal.vaner at nic.cz>
Date: Wed May 4 16:30:21 2011 +0200
[trac901] Output message at destruction
-----------------------------------------------------------------------
Summary of changes:
src/lib/log/log_formatter.h | 16 ++++++---
src/lib/log/tests/Makefile.am | 1 +
src/lib/log/tests/log_formatter_unittest.cc | 52 +++++++++++++++++++++++++++
3 files changed, 64 insertions(+), 5 deletions(-)
create mode 100644 src/lib/log/tests/log_formatter_unittest.cc
-----------------------------------------------------------------------
diff --git a/src/lib/log/log_formatter.h b/src/lib/log/log_formatter.h
index a33264c..df42feb 100644
--- a/src/lib/log/log_formatter.h
+++ b/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
/// 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 {
private:
/// \brief The logger we will use to output the final message
- Logger& logger_;
+ Logger* logger_;
/// \brief Prefix (eg. "ERROR", "DEBUG" or like that)
const char* prefix_;
/// \brief The messages with %1, %2... placeholders
@@ -74,15 +78,15 @@ public:
/// \param logger The logger where the final output will go.
Formatter(const char* prefix, const std::string& message,
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
///
/// This will create a formatter that produces no output.
Formatter() :
+ nextPlaceholder_(0),
active_(false)
{
}
@@ -90,7 +94,9 @@ public:
//
/// This is the place where output happens if the formatter is active.
~ Formatter() {
-
+ if (active_) {
+ logger_->output(prefix_, message_);
+ }
}
/// \brief Replaces another placeholder
///
diff --git a/src/lib/log/tests/Makefile.am b/src/lib/log/tests/Makefile.am
index b9fe150..46065e8 100644
--- a/src/lib/log/tests/Makefile.am
+++ b/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_2.cc
run_unittests_SOURCES += run_unittests.cc
+run_unittests_SOURCES += log_formatter_unittest.cc
run_unittests_CPPFLAGS = $(AM_CPPFLAGS) $(GTEST_INCLUDES)
run_unittests_LDFLAGS = $(AM_LDFLAGS) $(GTEST_LDFLAGS)
diff --git a/src/lib/log/tests/log_formatter_unittest.cc b/src/lib/log/tests/log_formatter_unittest.cc
new file mode 100644
index 0000000..e3bf369
--- /dev/null
+++ b/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);
+}
+
+}
More information about the bind10-changes
mailing list