Skip to content
GitLab
Projects
Groups
Snippets
/
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in / Register
Toggle navigation
Menu
Open sidebar
ISC Open Source Projects
Kea
Commits
2aca19e7
Commit
2aca19e7
authored
May 04, 2011
by
Michal 'vorner' Vaner
Browse files
[trac901] Output message at destruction
parent
cf46f370
Changes
3
Hide whitespace changes
Inline
Side-by-side
src/lib/log/log_formatter.h
View file @
2aca19e7
...
...
@@ -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
///
...
...
src/lib/log/tests/Makefile.am
View file @
2aca19e7
...
...
@@ -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)
...
...
src/lib/log/tests/log_formatter_unittest.cc
0 → 100644
View file @
2aca19e7
// 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
);
}
}
Write
Preview
Supports
Markdown
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment