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 */ 00045 #ifndef _LOGGER_ 00046 #define _LOGGER_ 00047 00048 #include <ostream.h> // needed for ostream (logging to output stream) 00049 #include <fstream.h> // needed for fstream (logging to file) 00050 #include <sys/time.h> // needed for timeval 00051 #include <string> // needed for string 00052 #include <iomanip> // needed for setw 00053 00054 #define MAX_LOG_LINE 2048 00055 #define MAX_HEADER 128 00056 /******************************************************************************/ 00057 /*********************** CLASS TIMING *****************************************/ 00058 /******************************************************************************/ 00059 00062 class Timing 00063 { 00064 struct timeval time1; 00066 public: 00067 // methods to restart the timer, get the elapsed time and print messages 00068 static double getTimeDifference ( struct timeval t1, 00069 struct timeval t2 ); 00070 void printTimeDiffWithText( ostream& os, 00071 char *str, 00072 int iFactor = 1000 ); 00073 double getElapsedTime ( ); 00074 void restartTime ( ); 00075 } ; 00076 00077 00078 /*****************************************************************************/ 00079 /**************************** LOGGER *****************************************/ 00080 /*****************************************************************************/ 00081 00094 class Logger 00095 { 00096 Timing timing; 00097 char buf[MAX_LOG_LINE]; 00098 int iMinLogLevel; 00099 int iMaxLogLevel; 00100 int iExtraLogLevel; 00101 char strHeader[MAX_HEADER]; 00102 ostream* os; 00104 public: 00105 Logger( ostream& os=cout, int iMinLogLevel=0, int iMaxLogLevel = 0); 00106 00107 // different methods associated with logging messages 00108 // bool log ( int iLevel, char *str, string str ); 00109 bool log ( int iLevel, string str ); 00110 bool log ( int i, char *str, ... ); 00111 bool logWithTime ( int iLevel, char *str, ... ); 00112 void restartTimer ( ); 00113 00114 bool isInLogLevel ( int iLevel ); 00115 00116 // standard get and set methods for the different member variables 00117 int getMinLogLevel ( ) const; 00118 bool setMinLogLevel ( int iLevel ); 00119 00120 int getMaxLogLevel ( ) const; 00121 bool setMaxLogLevel ( int iLevel ); 00122 00123 int getExtraLogLevel ( ) const; 00124 bool setExtraLogLevel ( int iLevel ); 00125 00126 char* getHeader ( ); 00127 bool setHeader ( char *str ); 00128 bool setHeader ( int i1, int i2 ); 00129 00130 bool setOutputStream ( ostream& os ); 00131 }; 00132 00133 00134 #endif