claw  1.9.0
log_stream_uniq.cpp
Go to the documentation of this file.
1 /*
2  CLAW - a C++ Library Absolutely Wonderful
3 
4  CLAW is a free library without any particular aim but being useful to
5  anyone.
6 
7  Copyright (C) 2005-2011 Julien Jorge
8 
9  This library is free software; you can redistribute it and/or
10  modify it under the terms of the GNU Lesser General Public
11  License as published by the Free Software Foundation; either
12  version 2.1 of the License, or (at your option) any later version.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library; if not, write to the Free Software
21  Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
22 
23  contact: julien.jorge@stuff-o-matic.com
24 */
31 
32 #include <claw/claw_gettext.hpp>
33 
34 #include <sstream>
35 
42  : m_stream(s)
43  , m_repetition_count(0)
44 {}
45 
50 {
51  delete m_stream;
52 }
53 
58 void claw::log_stream_uniq::write(const std::string& str)
59 {
60  std::string::size_type p = str.find_first_of('\n');
61 
62  if(p == std::string::npos)
63  m_current_line += str;
64  else
65  {
66  ++p; // includes the '\n'
67  m_current_line += str.substr(0, p);
68 
69  output_current_line();
70 
71  if(p != str.length())
72  write(str.substr(p));
73  }
74 }
75 
80 {
81  m_stream->flush();
82 }
83 
87 void claw::log_stream_uniq::output_current_line()
88 {
89  if(m_current_line == m_previous_line)
90  ++m_repetition_count;
91  else
92  {
93  if(m_repetition_count > 0)
94  {
95  std::ostringstream oss;
96  oss << claw_gettext("(Previous line repeated ") << m_repetition_count
97  << claw_ngettext(" time)", " times)", m_repetition_count)
98  << '\n';
99 
100  m_stream->write(oss.str());
101  }
102 
103  m_repetition_count = 0;
104 
105  m_previous_line = m_current_line;
106  m_stream->write(m_current_line);
107  }
108 
109  m_current_line.clear();
110 }
virtual void flush()
Flush the stream.
log_stream_uniq(log_stream *s)
Constructor.
#define claw_gettext(s)
Call gettext on the default text domain used by Claw.
virtual ~log_stream_uniq()
Destructor.
A log stream that does not output successively the same message.
virtual void write(const std::string &str)
Write a string in the stream.
Macros to call gettext on the libclaw textdomain.
Base class for streams accepting log output.
Definition: log_stream.hpp:60
#define claw_ngettext(s, p, n)
Call ngettext on the default text domain used by Claw.