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
00028
00029
00030
00051 #include "Logger.h"
00052 #include <stdio.h>
00053 #include <string>
00054 #ifdef Solaris
00055 #include <varargs.h>
00056 #else
00057 #include <stdarg.h>
00058 #endif
00059
00060 Logger Log;
00061 Logger LogDraw;
00063
00064
00065
00066
00074 Logger::Logger( ostream& o, int iMin, int iMax )
00075 {
00076 pthread_mutex_init( &mutex_stream, NULL );
00077 strcpy( m_strHeader, "" );
00078 m_timing.restartTime();
00079 addLogRange( iMin, iMax );
00080 m_os = &o;
00081 }
00082
00090 bool Logger::log( int iLevel, string str)
00091 {
00092 if( isInLogLevel( iLevel ) )
00093 {
00094 *m_os << m_strHeader << str;
00095 return true;
00096 }
00097
00098 return false;
00099 }
00100
00111 bool Logger::log( int iLevel, char *str, ... )
00112 {
00113 if( isInLogLevel( iLevel ) )
00114 {
00115 logSignal();
00116 va_list ap;
00117 #ifdef Solaris
00118 va_start( ap );
00119 #else
00120 va_start( ap, str );
00121 #endif
00122 if( vsnprintf( m_buf, MAX_LOG_LINE-1, str, ap ) == -1 )
00123 cerr << "Logger::log, buffer is too small!\n" ;
00124 va_end(ap);
00125 *m_os << m_strHeader << m_buf << "\n";
00126 return true;
00127 }
00128
00129 return false;
00130 }
00131
00143 bool Logger::logWithTime( int iLevel, char *str, ... )
00144 {
00145 if( isInLogLevel( iLevel ) )
00146 {
00147 logSignal();
00148 va_list ap;
00149 #ifdef Solaris
00150 va_start( ap );
00151 #else
00152 va_start( ap, str );
00153 #endif
00154 if( vsnprintf( m_buf, MAX_LOG_LINE-1, str, ap ) == -1 )
00155 cerr << "Logger::log, buffer is too small!" << "\n";
00156 va_end(ap);
00157
00158 string s = m_strHeader;
00159 s.append( m_buf );
00160 s.copy( m_buf, string::npos );
00161 m_buf[s.length()] = '\0';
00162 m_timing.printTimeDiffWithText( *m_os, m_buf );
00163
00164 return true;
00165 }
00166
00167 return false;
00168 }
00169
00181 bool Logger::logFromSignal( int iLevel, char *str, ... )
00182 {
00183 char buf[MAX_LOG_LINE];
00184 if( isInLogLevel( iLevel ) )
00185 {
00186 va_list ap;
00187 #ifdef Solaris
00188 va_start( ap );
00189 #else
00190 va_start( ap, str );
00191 #endif
00192 if( vsnprintf( buf, MAX_LOG_LINE-1, str, ap ) == -1 )
00193 cerr << "Logger::log, buffer is too small!" << "\n";
00194 va_end(ap);
00195
00196 char str[16];
00197 sprintf( str, "%2.2f: ", m_timing.getElapsedTime()*1000 );
00198 m_strSignal.append( str );
00199 m_strSignal.append( m_strHeader );
00200 m_strSignal.append( buf );
00201 m_strSignal.append( "\n\0" );
00202
00203 return true;
00204 }
00205
00206 return false;
00207
00208 }
00209
00213 bool Logger::logSignal( )
00214 {
00215 if( ! m_strSignal.empty() )
00216 {
00217 *m_os << "\n" << m_strSignal ;
00218 m_strSignal = "";
00219 return true;
00220 }
00221 return false;
00222 }
00223
00225 void Logger::restartTimer()
00226 {
00227 return m_timing.restartTime();
00228 }
00229
00232 Timing Logger::getTiming( )
00233 {
00234 return m_timing;
00235 }
00236
00237
00243 bool Logger::isInLogLevel( int iLevel )
00244 {
00245 return m_setLogLevels.find( iLevel ) != m_setLogLevels.end() ;
00246 }
00247
00252 bool Logger::addLogLevel( int iLevel )
00253 {
00254 m_setLogLevels.insert( iLevel );
00255 return true;
00256 }
00257
00263 bool Logger::addLogRange( int iMin, int iMax )
00264 {
00265 bool bReturn = true;
00266 for( int i = iMin ; i <= iMax; i++ )
00267 bReturn &= addLogLevel( i );
00268 return bReturn;
00269 }
00270
00274 char* Logger::getHeader( )
00275 {
00276 return m_strHeader;
00277 }
00278
00282 bool Logger::setHeader( char *str )
00283 {
00284 strcpy( m_strHeader, str );
00285 return true;
00286 }
00287
00294 bool Logger::setHeader( int i1, int i2 )
00295 {
00296 sprintf( m_strHeader, "(%d, %d) ", i1, i2 );
00297 return true;
00298 }
00299
00305 bool Logger::setHeader( int i )
00306 {
00307 sprintf( m_strHeader, " %d: ", i );
00308 return true;
00309 }
00310
00311
00317 bool Logger::setOutputStream( ostream& o )
00318 {
00319 m_os = &o;
00320 return true;
00321 }
00322
00327 ostream& Logger::getOutputStream( )
00328 {
00329 return *m_os;
00330 }
00331
00335 void Logger::showLogLevels( ostream &os )
00336 {
00337 set<int>::iterator itr;
00338 for (itr = m_setLogLevels.begin() ; itr != m_setLogLevels.end() ; itr++)
00339 os << *itr << " " ;
00340 os << "\n";
00341 }
00342
00343
00344
00345
00350 #ifdef WIN32
00351 double Timing::getTimeDifference( DWORD tv1, DWORD tv2 )
00352 {
00353 return ((double)(tv1 - tv2) / 1000.0) ;
00354 }
00355 #else
00356 double Timing::getTimeDifference( struct timeval tv1, struct timeval tv2 )
00357 {
00358
00359 return ((double)tv1.tv_sec + (double)tv1.tv_usec/1000000 ) -
00360 ((double)tv2.tv_sec + (double)tv2.tv_usec/1000000 ) ;
00361 }
00362 #endif
00363
00372 void Timing::printTimeDiffWithText( ostream &os, char *str, int iFactor )
00373 {
00374
00375 os <<setw(6)<< setfill('0')<< getElapsedTime()*iFactor << ":" << str << "\n";
00376 }
00377
00381 double Timing::getElapsedTime( int iFactor )
00382 {
00383 #ifdef WIN32
00384 DWORD time2 = GetTickCount();
00385 #else
00386 struct timeval time2;
00387 gettimeofday( &time2, NULL );
00388 #endif
00389 return getTimeDifference( time2, time1 )*iFactor;
00390 }
00391
00393 void Timing::restartTime( )
00394 {
00395 #ifdef WIN32
00396 time1 = GetTickCount();
00397 #else
00398 gettimeofday( &time1, NULL );
00399 #endif
00400 }
00401
00402
00403
00404
00405
00406
00407
00408
00409
00410
00411
00412
00413
00414
00415
00416
00417
00418
00419
00420
00421
00422
00423
00424
00425
00426
00427
00428
00429
00430
00431
00432
00433