log_stream_uniq.cpp
Go to the documentation of this file.
/*
CLAW - a C++ Library Absolutely Wonderful
CLAW is a free library without any particular aim but being useful to
anyone.
Copyright (C) 2005-2011 Julien Jorge
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation; either
version 2.1 of the License, or (at your option) any later version.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
contact: julien.jorge@gamned.org
*/
#include <claw/log_stream_uniq.hpp>
#include <claw/claw_gettext.hpp>
#include <sstream>
/*----------------------------------------------------------------------------*/
claw::log_stream_uniq::log_stream_uniq( log_stream* s )
: m_stream(s), m_repetition_count(0)
{
} // log_stream_uniq::log_stream_uniq()
/*----------------------------------------------------------------------------*/
claw::log_stream_uniq::~log_stream_uniq()
{
delete m_stream;
} // log_stream_uniq::~log_stream_uniq()
/*----------------------------------------------------------------------------*/
void claw::log_stream_uniq::write( const std::string& str )
{
std::string::size_type p = str.find_first_of('\n');
if ( p == std::string::npos )
m_current_line += str;
else
{
++p; // includes the '\n'
m_current_line += str.substr(0, p);
output_current_line();
if ( p != str.length() )
write( str.substr(p) );
}
} // log_stream_uniq::write()
/*----------------------------------------------------------------------------*/
void claw::log_stream_uniq::flush()
{
m_stream->flush();
} // log_stream_uniq::flush()
/*----------------------------------------------------------------------------*/
void claw::log_stream_uniq::output_current_line()
{
if ( m_current_line == m_previous_line )
++m_repetition_count;
else
{
if ( m_repetition_count > 0 )
{
std::ostringstream oss;
oss << claw_gettext("(Previous line repeated ") << m_repetition_count
<< claw_ngettext(" time)", " times)", m_repetition_count) << '\n';
m_stream->write(oss.str());
}
m_repetition_count = 0;
m_previous_line = m_current_line;
m_stream->write( m_current_line );
}
m_current_line.clear();
} // log_stream_uniq::output_current_line()
claw::log_stream_uniq::flush
virtual void flush()
Flush the stream.
Definition: log_stream_uniq.cpp:84
claw::log_stream_uniq::log_stream_uniq
log_stream_uniq(log_stream *s)
Constructor.
Definition: log_stream_uniq.cpp:42
claw_gettext
#define claw_gettext(s)
Call gettext on the default text domain used by Claw.
Definition: claw_gettext.hpp:39
claw::log_stream::flush
virtual void flush()
Flush the stream.
Definition: log_stream.cpp:46
claw::log_stream_uniq::~log_stream_uniq
virtual ~log_stream_uniq()
Destructor.
Definition: log_stream_uniq.cpp:52
log_stream_uniq.hpp
A log stream that does not output successively the same message.
claw::log_stream_uniq::write
virtual void write(const std::string &str)
Write a string in the stream.
Definition: log_stream_uniq.cpp:62
claw_gettext.hpp
Macros to call gettext on the libclaw textdomain.
claw::log_stream
Base class for streams accepting log output.
Definition: log_stream.hpp:59
claw_ngettext
#define claw_ngettext(s, p, n)
Call ngettext on the default text domain used by Claw.
Definition: claw_gettext.hpp:47