Main Page   Namespace List   Class Hierarchy   Compound List   File List   Compound Members   File Members  

Logger.cpp

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2000-2003, Jelle Kok, University of Amsterdam
00003 All rights reserved.
00004 
00005 Redistribution and use in source and binary forms, with or without
00006 modification, are permitted provided that the following conditions are met:
00007 
00008 1. Redistributions of source code must retain the above copyright notice, this
00009 list of conditions and the following disclaimer.
00010 
00011 2. Redistributions in binary form must reproduce the above copyright notice,
00012 this list of conditions and the following disclaimer in the documentation
00013 and/or other materials provided with the distribution.
00014 
00015 3. Neither the name of the University of Amsterdam nor the names of its
00016 contributors may be used to endorse or promote products derived from this
00017 software without specific prior written permission.
00018 
00019 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00020 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00021 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00022 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE
00023 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00024 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00025 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00026 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00027 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00028 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00029 */
00030 
00051 #include "Logger.h"
00052 #include <stdio.h>    // needed for vsprintf
00053 #include <string>     // needed for string
00054 #ifdef Solaris
00055  #include <varargs.h> // needed for va_list and va_start under Solaris
00056 #else
00057  #include <stdarg.h>
00058 #endif
00059 
00060 Logger Log;          
00061 Logger LogDraw;      
00063 /*****************************************************************************/
00064 /**************************** LOGGER *****************************************/
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(); // test whether there are no old strings left to log
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(); // test whether there are no old strings left to log
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 /********************** CLASS TIMING *****************************************/
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   // set the with to 6 and fill remaining places with '0'.
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 /**************************** TESTING PURPOSES *******************************/
00405 /*****************************************************************************/
00406 
00407 /*
00408 #ifdef WIN32
00409   #include <windows.h>
00410 #else
00411   #include<sys/poll.h>
00412 #endif
00413 
00414 int main( void )
00415 {
00416   ofstream fout( "temp.txt" );
00417   Logger log( fout, 0, 2 );
00418   log.log( 0, "hello" );
00419   log.setHeader( "jelle" );
00420 #ifdef WIN32
00421   Sleep(1000);
00422 #else
00423   poll(0,0,1000);
00424 #endif
00425   log.log( 2, "hello" );
00426   log.log( 3, "hello" );
00427   int j = 2;
00428   double i = 2.324234;
00429   printf( "hoi: "); fflush(stdout);
00430   log.logWithTime( 1, "|%f %d|", i, j);
00431 }
00432 
00433 */

Generated on Fri Nov 7 11:45:39 2003 for UvA Trilearn 2003 Base Code by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001