Claw  1.7.3
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@gamned.org
24 */
30 #include <claw/log_stream_uniq.hpp>
31 
32 #include <claw/claw_gettext.hpp>
33 
34 #include <sstream>
35 
36 /*----------------------------------------------------------------------------*/
43  : m_stream(s), m_repetition_count(0)
44 {
45 
46 } // log_stream_uniq::log_stream_uniq()
47 
48 /*----------------------------------------------------------------------------*/
53 {
54  delete m_stream;
55 } // log_stream_uniq::~log_stream_uniq()
56 
57 /*----------------------------------------------------------------------------*/
62 void claw::log_stream_uniq::write( const std::string& str )
63 {
64  std::string::size_type p = str.find_first_of('\n');
65 
66  if ( p == std::string::npos )
67  m_current_line += str;
68  else
69  {
70  ++p; // includes the '\n'
71  m_current_line += str.substr(0, p);
72 
73  output_current_line();
74 
75  if ( p != str.length() )
76  write( str.substr(p) );
77  }
78 } // log_stream_uniq::write()
79 
80 /*----------------------------------------------------------------------------*/
85 {
86  m_stream->flush();
87 } // log_stream_uniq::flush()
88 
89 /*----------------------------------------------------------------------------*/
93 void claw::log_stream_uniq::output_current_line()
94 {
95  if ( m_current_line == m_previous_line )
96  ++m_repetition_count;
97  else
98  {
99  if ( m_repetition_count > 0 )
100  {
101  std::ostringstream oss;
102  oss << claw_gettext("(Previous line repeated ") << m_repetition_count
103  << claw_ngettext(" time)", " times)", m_repetition_count) << '\n';
104 
105  m_stream->write(oss.str());
106  }
107 
108  m_repetition_count = 0;
109 
110  m_previous_line = m_current_line;
111  m_stream->write( m_current_line );
112  }
113 
114  m_current_line.clear();
115 } // log_stream_uniq::output_current_line()