Claw  1.7.3
code/logger.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/logger.hpp>
00031 
00032 #include <algorithm>
00033 
00034 namespace claw
00035 {
00037   log_system logger;
00038 } // namespace claw
00039 
00040 /*----------------------------------------------------------------------------*/
00046 claw::log_system::log_system()
00047   : m_log_level(-1), m_message_level(0)
00048 {
00049 
00050 } // log_system::~log_system()
00051 
00052 /*----------------------------------------------------------------------------*/
00056 claw::log_system::~log_system()
00057 {
00058   clear();
00059 } // log_system::~log_system()
00060 
00061 /*----------------------------------------------------------------------------*/
00065 void claw::log_system::clear()
00066 {
00067   stream_list_type::iterator it;
00068 
00069   for ( it=m_stream.begin(); it!=m_stream.end(); ++it )
00070     delete *it;
00071 
00072   m_stream.clear();
00073 } // log_system::clear()
00074 
00075 /*----------------------------------------------------------------------------*/
00080 void claw::log_system::merge( stream_type* s )
00081 {
00082   m_stream.push_front(s);
00083 } // log_system::merge()
00084 
00085 /*----------------------------------------------------------------------------*/
00091 void claw::log_system::remove( const stream_type* s )
00092 {
00093   stream_list_type::iterator it =
00094     std::find(m_stream.begin(), m_stream.end(), s);
00095 
00096   if ( it!=m_stream.end() )
00097     m_stream.erase(it);
00098 } // log_system::remove()
00099 
00100 /*----------------------------------------------------------------------------*/
00105 void claw::log_system::set( stream_type* s )
00106 {
00107   clear();
00108   m_stream.push_front(s);
00109 } // log_system::set()
00110 
00111 /*----------------------------------------------------------------------------*/
00116 void claw::log_system::set_level( int lvl )
00117 {
00118   m_log_level = lvl;
00119 } // log_system::set_level()
00120 
00121 /*----------------------------------------------------------------------------*/
00126 void claw::log_system::set_level( const log_level& lvl )
00127 {
00128   m_log_level = lvl.get();
00129 } // log_system::set_level()
00130 
00131 /*----------------------------------------------------------------------------*/
00135 void claw::log_system::flush()
00136 {
00137   if (m_message_level <= m_log_level)
00138     {
00139       stream_list_type::iterator it;
00140       
00141       for ( it=m_stream.begin(); it!=m_stream.end(); ++it )
00142   (*it)->flush();
00143     }
00144 } // log_system::flush()
00145 
00146 /*----------------------------------------------------------------------------*/
00151 claw::log_system& claw::log_system::operator<<( const log_level& that )
00152 {
00153   m_message_level = that.get();
00154   
00155   if (m_message_level <= m_log_level)
00156     *this << that.get_string();
00157   
00158   return *this;
00159 } // log_system::operator<<() [log_level]
00160 
00161 /*----------------------------------------------------------------------------*/
00166 claw::log_system&
00167 claw::log_system::operator<<( log_system& (*pf)(log_system&) )
00168 {
00169   return pf(*this);
00170 } // log_system::operator<<() [log_system& (*pf)(log_system&)]
00171 
00172 /*----------------------------------------------------------------------------*/
00177 claw::log_system& claw::lendl( claw::log_system& log )
00178 {
00179   return log << std::endl;
00180 } // lendl()
00181 
00182 claw::log_system& std::endl( claw::log_system& log )
00183 {
00184   (log << "\n").flush();
00185   return log;
00186 } // endl()