00001 /* 00002 * Copyright (C) 2003-2009 Olivier Boudeville 00003 * 00004 * This file is part of the Ceylan library. 00005 * 00006 * The Ceylan library is free software: you can redistribute it and/or modify 00007 * it under the terms of either the GNU Lesser General Public License or 00008 * the GNU General Public License, as they are published by the Free Software 00009 * Foundation, either version 3 of these Licenses, or (at your option) 00010 * any later version. 00011 * 00012 * The Ceylan library is distributed in the hope that it will be useful, 00013 * but WITHOUT ANY WARRANTY; without even the implied warranty of 00014 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 00015 * GNU Lesser General Public License and the GNU General Public License 00016 * for more details. 00017 * 00018 * You should have received a copy of the GNU Lesser General Public 00019 * License and the GNU General Public License along with the Ceylan library. 00020 * If not, see <http://www.gnu.org/licenses/>. 00021 * 00022 * Author: Olivier Boudeville (olivier.boudeville@esperide.com) 00023 * 00024 */ 00025 00026 00027 #include "CeylanObjectChannel.h" 00028 00029 #include "CeylanOperators.h" 00030 #include "CeylanLogMessage.h" 00031 #include "CeylanObjectIdentifier.h" 00032 #include "CeylanLogLight.h" // for CEYLAN_LOG 00033 #include "CeylanLoggable.h" // for Loggable::getEmbeddedChannelName 00034 #include "CeylanStringUtils.h" // for formatStringList 00035 00036 #ifdef CEYLAN_USES_CONFIG_H 00037 #include "CeylanConfig.h" // for CEYLAN_DEBUG 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 #include <iostream> 00049 00050 ObjectChannel::ObjectChannel( const string & channelName ) : 00051 LogChannel( "Uninitialized object channel" ), 00052 _linkedObjectID( 0 ) 00053 { 00054 00055 CEYLAN_LOG( "Creating object channel " + Ceylan::toString( this ) 00056 + " named '" + channelName + "'" ) ; 00057 00058 try 00059 { 00060 00061 _linkedObjectID = & 00062 ObjectIdentifier::generateFromChannelName( channelName ) ; 00063 00064 } 00065 catch( const Identifier::IdentifierException & e ) 00066 { 00067 00068 throw LogException( "ObjectChannel constructor: " 00069 "unable to construct channel identifier from " 00070 + channelName + ": " + e.toString() ) ; 00071 00072 } 00073 00074 _name = _linkedObjectID->toString() ; 00075 00076 } 00077 00078 00079 00080 ObjectChannel::~ObjectChannel() throw() 00081 { 00082 00083 CEYLAN_LOG( "Deleting object channel " + Ceylan::toString( this ) 00084 + " named '" + _name + "'" ) ; 00085 00086 for ( list<LogMessage *>::iterator it = _messages.begin(); 00087 it != _messages.end(); it++ ) 00088 { 00089 00090 CEYLAN_LOG( " - deleting message '" + Ceylan::toString( *it ) + "'" ) ; 00091 00092 delete (*it) ; 00093 00094 } 00095 00096 00097 /* 00098 * Keep it, as the parent destructor would otherwise perform double 00099 * deletion: 00100 * 00101 */ 00102 00103 _messages.clear() ; 00104 00105 CEYLAN_LOG( "Object channel " + _name + " cleared." ) ; 00106 00107 if ( _linkedObjectID != 0 ) 00108 delete _linkedObjectID ; 00109 00110 CEYLAN_LOG( "Object channel " + _name + " deleted." ) ; 00111 00112 } 00113 00114 00115 00116 void ObjectChannel::addMessage( LogMessage & message, bool check ) 00117 { 00118 00119 if ( check ) 00120 { 00121 00122 if ( Loggable::GetEmbeddedChannelName( 00123 message.getChannelName() ) != _name ) 00124 throw LogException( 00125 "ObjectChannel::addMessage: trying to add to ObjectChannel " 00126 + _name + " a log message whose registered ObjectChannel is " 00127 + Loggable::GetEmbeddedChannelName( message.getChannelName() ) 00128 + " (not " + _name + ")." ) ; 00129 } 00130 00131 _messages.push_back( & message ) ; 00132 00133 } 00134 00135 00136 00137 ObjectIdentifier & ObjectChannel::getObjectIdentifier() const 00138 { 00139 00140 if ( _linkedObjectID != 0 ) 00141 return * _linkedObjectID ; 00142 00143 throw LogException( 00144 "ObjectChannel::getObjectIdentifier: no identifier to return." ) ; 00145 00146 } 00147 00148 00149 00150 const string ObjectChannel::toString( Ceylan::VerbosityLevels level ) const 00151 { 00152 00153 string result = "ObjectChannel " + _name ; 00154 00155 if ( _messages.empty() ) 00156 return result + " contains no message." ; 00157 00158 if ( _messages.size() == 1 ) 00159 result += " contains only one message" ; 00160 else 00161 result += " contains " + Ceylan::toString( 00162 static_cast<Ceylan::Uint32>( _messages.size() ) ) + " messages" ; 00163 00164 if ( level == Ceylan::low ) 00165 return result ; 00166 00167 list<string> messageList ; 00168 00169 for ( list<LogMessage *>::const_iterator it = _messages.begin(); 00170 it != _messages.end(); it++ ) 00171 { 00172 00173 #if CEYLAN_DEBUG_LOG 00174 00175 if ( ! (*it) ) 00176 { 00177 CEYLAN_LOG( "Error, ObjectChannel::toString: " 00178 "null pointer in message list, skipping." ) ; 00179 break ; 00180 } 00181 00182 CEYLAN_LOG( "ObjectChannel::toString: describing message at " 00183 + Ceylan::toString( *it ) ) ; 00184 00185 #endif // CEYLAN_DEBUG_LOG 00186 00187 messageList.push_back( (*it)->toString( level ) ) ; 00188 00189 } 00190 00191 return result + ": " + formatStringList( messageList ) ; 00192 00193 } 00194