Commit 2aca19e7 authored by Michal 'vorner' Vaner's avatar Michal 'vorner' Vaner
Browse files

[trac901] Output message at destruction

parent cf46f370
......@@ -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
///
......
......@@ -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)
......
// 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);
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment