00001 #ifndef SLIC_LOGSTREAM_HH
00002 #define SLIC_LOGSTREAM_HH 1
00003
00004
00005 #include "LogMessenger.hh"
00006 #include "TimeUtil.hh"
00007
00008
00009 #include <iostream>
00010 #include <fstream>
00011 #include <iomanip>
00012 #include <string>
00013 #include <vector>
00014
00015 using std::ofstream;
00016 using std::string;
00017 using std::ostream;
00018 using std::ios;
00019 using std::ios_base;
00020
00021 namespace LOG
00022 {
00023 enum EManip {
00024 done,
00025 head,
00026 endl
00027 };
00028
00029 enum ELevel {
00030 none=0,
00031 always,
00032 fatal,
00033 error,
00034 warning,
00035 okay,
00036 verbose,
00037 debug,
00038 nlevels
00039 };
00040 }
00041
00042 static std::string levelNames[ LOG::nlevels ] =
00043 { "NONE", "ALWAYS", "FATAL", "ERROR", "WARNING", "OKAY", "VERBOSE", "DEBUG" };
00044
00045 namespace slic
00046 {
00062 class LogStream
00063 {
00064
00065 public:
00066
00067 LogStream( const std::string& name,
00068 LOG::ELevel level,
00069 ostream* os = &std::cout )
00070 : m_name( name ),
00071 m_outputLevel( level ),
00072 m_os( os ),
00073 m_defaultOutputLevel( level ),
00074 m_active( true ),
00075 m_fos( 0 )
00076 {
00077 m_messenger = new LogMessenger( this );
00078 }
00079
00080 LogStream( )
00081 : m_name( "NONE" ),
00082 m_outputLevel( LOG::okay ),
00083 m_os ( &std::cout ),
00084 m_defaultOutputLevel( LOG::okay ),
00085 m_active( true ),
00086 m_fos( 0 ),
00087 m_messenger( 0 )
00088 {;}
00089
00090 virtual ~LogStream()
00091 {
00092
00093
00094 }
00095
00096 const std::string& getName() const
00097 {
00098 return m_name;
00099 }
00100
00101 bool isActive() const
00102 {
00103 return m_active;
00104 }
00105
00106 LOG::ELevel getOutputLevel() const
00107 {
00108 return m_outputLevel;
00109 }
00110
00111 ostream& getOutputStream() const
00112 {
00113 return *m_os;
00114 }
00115
00116 void setOutputLevel( const LOG::ELevel& outputLevel )
00117 {
00118 m_outputLevel = outputLevel;
00119 }
00120
00124 LogStream& operator << ( const LOG::ELevel& x )
00125 {
00126 if ( x <= getOutputLevel() ) {
00127
00128
00129 m_active = true;
00130
00131
00132 m_outputLevel = x;
00133
00134 }
00135 else {
00136
00137
00138 m_active = false;
00139 }
00140 return *this;
00141 }
00142
00143 LogStream& operator << ( const LOG::EManip& x )
00144 {
00145 if ( isActive() ) {
00146 if ( x == LOG::done ) {
00147 getOutputStream() << std::endl;
00148 m_outputLevel = m_defaultOutputLevel;
00149 }
00150 else if ( x == LOG::head ) {
00151 getOutputStream() <<
00152 "[ " << TimeUtil::getAscTime() << " ] :: " <<
00153 levelNames[ getOutputLevel() ] << " :: " <<
00154 getName() << " :: ";
00155 }
00156 else if ( x == LOG::endl ) {
00157 getOutputStream() << std::endl;
00158 }
00159 }
00160 return *this;
00161 }
00162
00163 LogStream& operator << ( const std::_Setiosflags &manip )
00164 {
00165 if ( isActive() ) {
00166 getOutputStream() << manip;
00167 }
00168 return *this;
00169 }
00170
00171 LogStream& operator << (const std::_Resetiosflags &manip)
00172 {
00173 if ( isActive() ) {
00174 getOutputStream() << manip;
00175 }
00176 return *this;
00177 }
00178
00179 LogStream& operator << (const std::_Setbase &manip)
00180 {
00181 if ( isActive() ) {
00182 getOutputStream() << manip;
00183 }
00184 return *this;
00185 }
00186
00187 LogStream& operator << (const std::_Setprecision &manip)
00188 {
00189 if ( isActive() ) {
00190 getOutputStream() << manip;
00191 }
00192 return *this;
00193 }
00194
00195 LogStream& operator << (const std::_Setw &manip)
00196 {
00197 if ( isActive() ) {
00198 getOutputStream() << manip;
00199 }
00200 return *this;
00201 }
00202
00203
00204 LogStream& operator << ( ios& (*manip)(std::ios&) )
00205 {
00206 if ( isActive() ) {
00207 getOutputStream() << manip;
00208 }
00209 return *this;
00210 }
00211
00212
00213 LogStream& operator << ( std::ios_base& (*manip)(std::ios_base&) )
00214 {
00215 if ( isActive() ) {
00216 getOutputStream() << manip;
00217 }
00218 return *this;
00219 }
00220
00221
00222 template <typename T>
00223 LogStream& operator<< ( const T& arg )
00224 {
00225 if ( isActive() ) {
00226 getOutputStream() << arg;
00227 }
00228 return *this;
00229 }
00230
00231
00232 template <typename T>
00233 LogStream& operator << ( const std::vector<T>& v ) {
00234 if( isActive() ) {
00235 for ( typename std::vector<T>::const_iterator i = v.begin();
00236 i != v.end(); ++i ) { getOutputStream() << *i << " "; }
00237 }
00238 return *this;
00239 }
00240
00241
00242 int width() const
00243 {
00244 return isActive() ? getOutputStream().width() : 0;
00245 }
00246
00247 int width( int v )
00248 {
00249 return isActive() ? getOutputStream().width(v) : 0;
00250 }
00251
00252
00253 long flags() const
00254 {
00255 return isActive() ? getOutputStream().flags() : 0;
00256 }
00257
00258 long flags( std::ios_base::fmtflags v )
00259 {
00260 return isActive() ? getOutputStream().flags(v) : 0;
00261 }
00262
00263 long setf( std::ios_base::fmtflags v )
00264 {
00265 return isActive() ? getOutputStream().setf(v) : 0;
00266 }
00267
00268 char fill() const
00269 {
00270 return isActive() ? getOutputStream().fill() : -1;
00271 }
00272
00273 char fill(char v)
00274 {
00275 return isActive() ? getOutputStream().fill(v) : -1;
00276 }
00277
00278 int precision() const
00279 {
00280 return isActive() ? getOutputStream().precision(): 0;
00281 }
00282
00283 int precision(int v)
00284 {
00285 return isActive() ? getOutputStream().precision(v): 0;
00286 }
00287
00288 int rdstate() const
00289 {
00290 return isActive() ? getOutputStream().rdstate () : std::ios_base::failbit;
00291 }
00292
00293 int good() const
00294 {
00295 return isActive() ? getOutputStream().good () : 0;
00296 }
00297
00298 int eof() const
00299 {
00300 return isActive() ? getOutputStream().eof () : 0;
00301 }
00302
00303 int bad() const
00304 {
00305 return isActive() ? getOutputStream().bad() : 0;
00306 }
00307
00308 long setf( std::ios_base::fmtflags _f, std::ios_base::fmtflags _m )
00309 {
00310 return isActive() ? getOutputStream().setf(_f, _m) : 0;
00311 }
00312
00313 void unsetf( std::ios_base::fmtflags _l )
00314 {
00315 if ( isActive() ) {
00316 getOutputStream().unsetf(_l);
00317 }
00318 }
00319
00320 void clear( std::ios_base::iostate _i = std::ios_base::failbit )
00321 {
00322 if ( isActive() ) {
00323 getOutputStream().clear(_i);
00324 }
00325 }
00326
00327
00328 void setOutputFile(const std::string& file)
00329 {
00330 ofstream* m_fos = new ofstream();
00331 m_fos->open( file.c_str(), ios::app | ios::out );
00332 m_os = m_fos;
00333 }
00334
00335 LogStream& debug()
00336 {
00337 return *this << LOG::debug;
00338 }
00339
00340 LogStream& verbose()
00341 {
00342 return *this << LOG::verbose;
00343 }
00344
00345 LogStream& okay()
00346 {
00347 return *this << LOG::okay;
00348 }
00349
00350 LogStream& warning()
00351 {
00352 return *this << LOG::warning;
00353 }
00354
00355 LogStream& error()
00356 {
00357 return *this << LOG::error;
00358 }
00359
00360 LogStream& fatal()
00361 {
00362 return *this << LOG::fatal;
00363 }
00364
00365 LogStream& always()
00366 {
00367 return *this << LOG::always;
00368 }
00369
00370 void log( const std::string& mesg, const LOG::ELevel& level )
00371 {
00372 (*this) << level << mesg << LOG::done;
00373 }
00374
00375 void log( const std::string& mesg )
00376 {
00377 log( mesg, getOutputLevel() );
00378 }
00379
00380 void debug( const std::string& mesg )
00381 {
00382 log( mesg, LOG::debug );
00383 }
00384
00385 void verbose( const std::string& mesg )
00386 {
00387 log( mesg, LOG::verbose );
00388 }
00389
00390 void okay( const std::string& mesg )
00391 {
00392 log( mesg, LOG::okay );
00393 }
00394
00395 void warning( const std::string& mesg )
00396 {
00397 log( mesg, LOG::warning );
00398 }
00399
00400 void error( const std::string& mesg )
00401 {
00402 log( mesg, LOG::error );
00403 }
00404
00405 void fatal( const std::string& mesg )
00406 {
00407 log( mesg, LOG::fatal );
00408 }
00409
00410 void always( const std::string& mesg )
00411 {
00412 log( mesg, LOG::always );
00413 }
00414
00415 private:
00416 std::string m_name;
00417 LOG::ELevel m_outputLevel;
00418 ostream* m_os;
00419 LOG::ELevel m_defaultOutputLevel;
00420 bool m_active;
00421 std::ofstream* m_fos;
00422 LogMessenger* m_messenger;
00423 };
00424 }
00425
00426 #endif