Claw  1.7.3
code/log_stream_concise.cpp
Go to the documentation of this file.
00001 /*
00002   CLAW - a C++ Library Absolutely Wonderful
00003 
00004   CLAW is a free library without any particular aim but being useful to 
00005   anyone.
00006 
00007   Copyright (C) 2005-2011 Julien Jorge
00008 
00009   This library is free software; you can redistribute it and/or
00010   modify it under the terms of the GNU Lesser General Public
00011   License as published by the Free Software Foundation; either
00012   version 2.1 of the License, or (at your option) any later version.
00013 
00014   This library is distributed in the hope that it will be useful,
00015   but WITHOUT ANY WARRANTY; without even the implied warranty of
00016   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00017   Lesser General Public License for more details.
00018 
00019   You should have received a copy of the GNU Lesser General Public
00020   License along with this library; if not, write to the Free Software
00021   Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
00022 
00023   contact: julien.jorge@gamned.org
00024 */
00030 #include <claw/log_stream_concise.hpp>
00031 
00032 #include <algorithm>
00033 
00034 /*----------------------------------------------------------------------------*/
00041 claw::log_stream_concise::log_stream_concise
00042 ( log_stream* s, std::size_t max_history_size )
00043   : m_stream(s), m_max_history_size(max_history_size)
00044 {
00045 
00046 } // log_stream_concise::log_stream_concise()
00047 
00048 /*----------------------------------------------------------------------------*/
00052 claw::log_stream_concise::~log_stream_concise()
00053 {
00054   delete m_stream;
00055 } // log_stream_concise::~log_stream_concise()
00056 
00057 /*----------------------------------------------------------------------------*/
00062 void claw::log_stream_concise::write( const std::string& str )
00063 {
00064   std::string::size_type p = str.find_first_of('\n');
00065 
00066   if ( p == std::string::npos )
00067     m_current_line += str;
00068   else
00069     {
00070       ++p; // includes the '\n'
00071       m_current_line += str.substr(0, p);
00072 
00073       output_current_line();
00074 
00075       if ( p != str.length() )
00076         write( str.substr(p) );
00077     }
00078 } // log_stream_concise::write()
00079 
00080 /*----------------------------------------------------------------------------*/
00084 void claw::log_stream_concise::flush()
00085 {
00086   m_stream->flush();
00087 } // log_stream_concise::flush()
00088 
00089 /*----------------------------------------------------------------------------*/
00093 void claw::log_stream_concise::output_current_line()
00094 {
00095   if ( std::find
00096        (m_previous_lines.begin(), m_previous_lines.end(), m_current_line)
00097        == m_previous_lines.end() )
00098     {
00099       m_previous_lines.push_back( m_current_line );
00100       m_stream->write( m_current_line );
00101 
00102       if (m_previous_lines.size() > m_max_history_size)
00103   m_previous_lines.pop_front();
00104     }
00105 
00106   m_current_line.clear();
00107 } // log_stream_concise::output_current_line()