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

Logger.C

Go to the documentation of this file.
00001 /*
00002 Copyright (c) 2000,2001, 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 */
00046 #include "Logger.h"
00047 #include <stdio.h>    // needed for vsprintf
00048 #include <string>     // needed for string
00049 #ifdef Solaris
00050  #include <varargs.h> // needed for va_list and va_start under Solaris
00051 #endif
00052 
00053 Logger Log;          
00055 /*****************************************************************************/
00056 /**************************** LOGGER *****************************************/
00057 /*****************************************************************************/
00058 
00066 Logger::Logger( ostream& o, int iMin, int iMax )
00067 {
00068   iMinLogLevel = iMin;
00069   iMaxLogLevel = iMax;
00070   strcpy( strHeader, "" );
00071   timing.restartTime();
00072   os = &o;
00073 }
00074 
00082 bool Logger::log( int iLevel, string str)
00083 {
00084   if( isInLogLevel( iLevel ) )
00085   {
00086     *os << strHeader << str;
00087     return true;
00088   }
00089 
00090   return false;
00091 }
00092 
00103 bool Logger::log( int iLevel, char *str, ... )
00104 {
00105   if( isInLogLevel( iLevel ) )
00106   {
00107     va_list ap;
00108 #ifdef Solaris
00109     va_start( ap );
00110 #else
00111     va_start( ap, str );
00112 #endif
00113     vsprintf( buf, str, ap );
00114     va_end(ap);
00115 
00116     *os << strHeader << buf << endl;
00117     return true;
00118   }
00119 
00120   return false;
00121 }
00122 
00134 bool Logger::logWithTime( int iLevel, char *str, ... )
00135 {
00136   if( isInLogLevel( iLevel ) )
00137   {
00138     va_list ap;
00139 #ifdef Solaris
00140     va_start( ap );
00141 #else
00142     va_start( ap, str );
00143 #endif
00144     vsprintf( buf, str, ap );
00145     va_end(ap);
00146 
00147     string s = strHeader;
00148     s.append( buf );
00149     s.copy( buf, string::npos );
00150     buf[s.length()] = '\0';
00151     timing.printTimeDiffWithText( *os, buf );
00152     return true;
00153   }
00154 
00155   return false;
00156 }
00157 
00159 void Logger::restartTimer()
00160 {
00161   return timing.restartTime();
00162 }
00163 
00169 bool Logger::isInLogLevel( int iLevel )
00170 {
00171   return ( iLevel >= getMinLogLevel() && iLevel <= getMaxLogLevel() ) ||
00172          iLevel == iExtraLogLevel; ;
00173 }
00174 
00178 int Logger::getMinLogLevel( ) const
00179 {
00180   return iMinLogLevel;
00181 }
00182 
00187 bool Logger::setMinLogLevel( int iLevel )
00188 {
00189   iMinLogLevel = ( iLevel > 0 ) ? iLevel : 0;
00190   return true;
00191 }
00192 
00196 int Logger::getMaxLogLevel( ) const
00197 {
00198   return iMaxLogLevel;
00199 }
00200 
00205 bool Logger::setMaxLogLevel( int iLevel )
00206 {
00207   iMaxLogLevel = ( iLevel > 0 ) ? iLevel : 0;
00208   return true;
00209 }
00210 
00214 int Logger::getExtraLogLevel( ) const
00215 {
00216   return iExtraLogLevel;
00217 }
00218 
00223 bool Logger::setExtraLogLevel( int iLevel )
00224 {
00225   iExtraLogLevel = iLevel;
00226   return true;
00227 }
00228 
00229 
00230 
00234 char* Logger::getHeader( )
00235 {
00236   return strHeader;
00237 }
00238 
00243 bool Logger::setHeader( char *str )
00244 {
00245   strcpy( strHeader, str );
00246   return true;
00247 }
00248 
00255 bool Logger::setHeader( int i1, int i2 )
00256 {
00257   sprintf( strHeader, "(%d, %d) ", i1, i2 );
00258   return true;
00259 }
00260 
00266 bool Logger::setOutputStream( ostream& o )
00267 {
00268   os = &o;
00269   return true;
00270 }
00271 
00272 /******************************************************************************/
00273 /*********************** CLASS TIMING *****************************************/
00274 /******************************************************************************/
00275 
00280 double Timing::getTimeDifference( struct timeval tv1, struct timeval tv2 )
00281 {
00282 
00283   return  ((double)tv1.tv_sec + (double)tv1.tv_usec/1000000 ) -
00284           ((double)tv2.tv_sec + (double)tv2.tv_usec/1000000 ) ;
00285 }
00286 
00293 void Timing::printTimeDiffWithText( ostream &os, char *str, int iFactor )
00294 {
00295   // set the with to 6 and fille remaining places with '0'.
00296   os <<setw(6)<< setfill('0')<< getElapsedTime()*iFactor << ":" << str << endl;
00297 }
00298 
00302 double Timing::getElapsedTime( )
00303 {
00304   struct timeval time2;
00305   gettimeofday( &time2, NULL );
00306   return getTimeDifference( time2, time1 );
00307 }
00308 
00310 void Timing::restartTime( )
00311 {
00312   gettimeofday( &time1, NULL );
00313 }
00314 
00315 
00316 /*****************************************************************************/
00317 /**************************** TESTING PURPOSES *******************************/
00318 /*****************************************************************************/
00319 
00320 /*
00321 #include<sys/poll.h>
00322 
00323 int main( void )
00324 {
00325   ofstream fout( "temp.txt" );
00326   Logger log( fout, 0, 2 );
00327   log.log( 0, "hello" );
00328   log.setHeader( "jelle" );
00329   poll(0,0,1000);
00330   log.log( 2, "hello" );
00331   log.log( 3, "hello" );
00332   int j = 2;
00333   double i = 2.324234;
00334   printf( "hoi: "); fflush(stdout);
00335   log.logWithTime( 1, "|%f %d|", i, j);
00336 }
00337 
00338 */

Generated on Thu Mar 7 00:37:42 2002 for UvA Trilearn 2001 by doxygen1.2.12 written by Dimitri van Heesch, © 1997-2001