00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027 #include "CeylanLogChannel.h"
00028
00029 #include "CeylanLogMessage.h"
00030 #include "CeylanLogLight.h"
00031
00032
00033 #include "CeylanOperators.h"
00034 #include "CeylanStringUtils.h"
00035
00036 #ifdef CEYLAN_USES_CONFIG_H
00037 #include "CeylanConfig.h"
00038 #endif // CEYLAN_USES_CONFIG_H
00039
00040
00041 using std::string ;
00042 using std::list ;
00043
00044
00045 using namespace Ceylan::Log ;
00046
00047
00048 LogChannel::LogChannel( const string & name ):
00049 _name( name )
00050 {
00051
00052 CEYLAN_LOG( "Creating log channel " + Ceylan::toString( this )
00053 + " named '" + name + "'" ) ;
00054
00055 }
00056
00057
00058
00059 LogChannel::~LogChannel() throw()
00060 {
00061
00062 CEYLAN_LOG( "Deleting log channel " + Ceylan::toString( this )
00063 + " named '" + _name + "', in which there are "
00064 + Ceylan::toString( _messages.size() ) + " remaining messages." ) ;
00065
00066 for ( list<LogMessage *>::iterator it = _messages.begin();
00067 it != _messages.end(); it++ )
00068 {
00069
00070 CEYLAN_LOG( "Deleting message " + (*it)->toString() ) ;
00071 delete (*it) ;
00072
00073 }
00074
00075 CEYLAN_LOG( "Log channel " + _name + " deleted." ) ;
00076
00077 }
00078
00079
00080
00081 void LogChannel::addMessage( LogMessage & message, bool check )
00082 {
00083
00084 CEYLAN_LOG( "Adding message " + Ceylan::toString( & message ) + ": '"
00085 + message.toString() + "'..." ) ;
00086
00087
00088 static volatile bool inUse = false ;
00089
00090 while ( inUse )
00091 ;
00092
00093 inUse = true ;
00094
00095 if ( check )
00096 {
00097 if ( message.getChannelName() != _name )
00098 {
00099
00100 inUse = false ;
00101 throw LogException(
00102 "LogChannel::addMessage: trying to add to LogChannel "
00103 + _name
00104 + " a log message whose registered LogChannel is "
00105 + message.getChannelName() + " (not " + _name + ")." ) ;
00106
00107 }
00108 }
00109
00110 _messages.push_back( & message ) ;
00111
00112 inUse = false ;
00113
00114 CEYLAN_LOG( "... message added." ) ;
00115
00116 }
00117
00118
00119
00120 const string LogChannel::getName() const
00121 {
00122
00123 return _name ;
00124
00125 }
00126
00127
00128
00129 LogChannel::MessageCount LogChannel::getMessageCount() const
00130 {
00131
00132
00133
00134
00135
00136
00137 return static_cast<Ceylan::Uint32>( _messages.size() ) ;
00138
00139 }
00140
00141
00142
00143 const string LogChannel::toString( Ceylan::VerbosityLevels level ) const
00144 {
00145
00146 string result = "LogChannel " + _name ;
00147
00148 if ( _messages.empty() )
00149 return result + " contains no message" ;
00150
00151 if ( _messages.size() == 1 )
00152 {
00153
00154 result += " contains only one message" ;
00155 if ( level == low )
00156 return result ;
00157
00158 result += ": " + _messages.back()->toString( level ) ;
00159
00160 return result ;
00161 }
00162
00163
00164
00165 result += " contains " + Ceylan::toString(
00166 static_cast<Ceylan::Uint32>( _messages.size() ) ) + " messages" ;
00167
00168 if ( level == low )
00169 return result ;
00170
00171 list<string> res ;
00172
00173 for ( list<LogMessage *>::const_iterator it = _messages.begin();
00174 it != _messages.end(); it++ )
00175 {
00176
00177 #if CEYLAN_DEBUG
00178
00179 if ( (*it) == 0 )
00180 {
00181 CEYLAN_LOG( "Error, LogChannel::toString: "
00182 "null pointer in message list, skipping." ) ;
00183 break ;
00184 }
00185
00186 #endif // CEYLAN_DEBUG
00187
00188 res.push_back( (*it)->toString( level ) ) ;
00189
00190 }
00191
00192 return result + ": " + formatStringList( res ) ;
00193
00194 }
00195