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 "CeylanLogAggregatorRaw.h"
00028
00029
00030 #include "CeylanOperators.h"
00031 #include "CeylanFile.h"
00032 #include "CeylanTimestamp.h"
00033
00034 #include "CeylanLogChannel.h"
00035 #include "CeylanLogMessage.h"
00036 #include "CeylanLogLight.h"
00037
00038
00039
00040 #include <iostream>
00041
00042
00043 using std::string ;
00044 using std::list ;
00045
00046 using namespace Ceylan::Log ;
00047 using namespace Ceylan::System ;
00048
00049
00050
00051 LogAggregatorRaw::LogAggregatorRaw(
00052 const string & logFilename,
00053 bool immediateWrite,
00054 bool useGlobalLevelOfDetail,
00055 bool beSmart ) :
00056 LogAggregator( beSmart, useGlobalLevelOfDetail ),
00057 _logFilename( logFilename ),
00058 _outputFile( 0 ),
00059 _immediateWrite( immediateWrite )
00060 {
00061
00062
00063 try
00064 {
00065
00066 CEYLAN_LOG( "LogAggregatorRaw constructor: creating file "
00067 + logFilename ) ;
00068
00069
00070 OpeningFlag logFlags = File::CreateFile | File::TruncateFile
00071 | File::Write ;
00072
00073
00074 if ( _immediateWrite )
00075 logFlags |= File::Synchronous ;
00076
00077 _outputFile = & System::File::Create( logFilename,
00078 logFlags ) ;
00079
00080
00081 }
00082 catch( const System::FileCreationFailed & e )
00083 {
00084
00085 throw LogAggregatorException( "LogAggregatorRaw constructor: "
00086 "could not create LogAggregatorRaw output file: " + e.toString() ) ;
00087
00088 }
00089
00090 }
00091
00092
00093
00094 LogAggregatorRaw::~LogAggregatorRaw() throw()
00095 {
00096
00097 CEYLAN_LOG( "LogAggregatorRaw destructor called." ) ;
00098
00099
00100 if ( _beSmart )
00101 {
00102 CEYLAN_LOG( "LogAggregatorRaw is smart, therefore "
00103 "automatically triggers log aggregation on exit." ) ;
00104
00105 try
00106 {
00107 aggregate() ;
00108 }
00109 catch( const LogAggregatorException & e )
00110 {
00111
00112 std::cerr << "Error while aggregating logs in "
00113 "LogAggregatorRaw destructor: "
00114 << e.toString() << "." << std::endl ;
00115
00116
00117 }
00118
00119 }
00120
00121
00122 if ( _outputFile != 0 )
00123 delete _outputFile ;
00124
00125 }
00126
00127
00128
00129 void LogAggregatorRaw::aggregate()
00130 {
00131
00132 std::cout << "Logs can be inspected in file "
00133 << _logFilename << std::endl ;
00134
00135 if ( _immediateWrite )
00136 {
00137 CEYLAN_LOG( "LogAggregatorRaw::aggregate: "
00138 "write mode is immediate, nothing to do." ) ;
00139 return ;
00140 }
00141
00142 CEYLAN_LOG( "LogAggregatorRaw aggregation started" ) ;
00143
00144 Timestamp stamp ;
00145
00146 _outputFile->write( stamp.toString()
00147 + " Aggregating Log messages.\n\n" ) ;
00148
00149 for ( list<LogChannel *>::const_iterator it = _channelList.begin() ;
00150 it != _channelList.end() ; it ++ )
00151 {
00152 write( * (*it) ) ;
00153 }
00154
00155 _outputFile->write( stamp.toString() + " Log messages aggregated." ) ;
00156
00157 }
00158
00159
00160
00161 void LogAggregatorRaw::store( LogMessage & message )
00162 {
00163
00164 CEYLAN_LOG( "Storing a new message " + message.toString() ) ;
00165
00166
00167 LogAggregator::store( message ) ;
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177
00178 if ( _immediateWrite )
00179 write( message ) ;
00180
00181 }
00182
00183
00184
00185 void LogAggregatorRaw::write( const LogChannel & channel ) const
00186 {
00187
00188 CEYLAN_LOG( "Writing on disk channel " + channel.toString() ) ;
00189
00190 try
00191 {
00192
00193 _outputFile->write( '\t'
00194 + channel.toString( getOverallVerbosityLevel() ) + '\n' ) ;
00195
00196 }
00197 catch( const OutputStream::WriteFailedException & e )
00198 {
00199
00200 throw LogException( "LogAggregatorRaw::write (first) failed: "
00201 + e.toString() ) ;
00202
00203 }
00204
00205 }
00206
00207
00208
00209 void LogAggregatorRaw::write( const LogMessage & message ) const
00210 {
00211
00212 CEYLAN_LOG( "Writing on disk message " + message.toString() ) ;
00213
00214 try
00215 {
00216
00217 _outputFile->write(
00218 message.toString( getMessageVerbosityLevel( message ) ) + '\n' ) ;
00219 }
00220 catch( const OutputStream::WriteFailedException & e )
00221 {
00222
00223 throw LogException( "LogAggregatorRaw::write (second) failed: "
00224 + e.toString() ) ;
00225
00226 }
00227
00228
00229
00230 }
00231
00232
00233
00234 const string LogAggregatorRaw::toString( Ceylan::VerbosityLevels level ) const
00235 {
00236
00237 return string( "This is LogAggregatorRaw in " )
00238 + ( _immediateWrite ? "": "non-" )
00239 + string( "immediate mode. It " )
00240 + ( _useGlobalLevelOfDetail ? "uses" : "does not use" )
00241 + string( " a global level of detail for message output. " )
00242 + LogAggregator::toString( level ) ;
00243
00244 }
00245