00001 /* 00002 Copyright (c) 2000-2002, 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 */ 00049 #ifndef _LOGGER_ 00050 #define _LOGGER_ 00051 00052 #include <ostream.h> // needed for ostream (logging to output stream) 00053 #include <fstream.h> // needed for fstream (logging to file) 00054 #include <sys/time.h> // needed for timeval 00055 #include <string> // needed for string 00056 #include <iomanip> // needed for setw 00057 #include <set> // needed for set 00058 00059 #define MAX_LOG_LINE 2048 00060 #define MAX_HEADER 128 00062 /******************************************************************************/ 00063 /*********************** CLASS TIMING *****************************************/ 00064 /******************************************************************************/ 00065 00068 class Timing 00069 { 00070 struct timeval time1; 00072 public: 00073 // methods to restart the timer, get the elapsed time and print messages 00074 static double getTimeDifference ( struct timeval t1, 00075 struct timeval t2 ); 00076 void printTimeDiffWithText( ostream& os, 00077 char *str, 00078 int iFactor = 1000 ); 00079 double getElapsedTime ( ); 00080 void restartTime ( ); 00081 } ; 00082 00083 00084 /*****************************************************************************/ 00085 /**************************** LOGGER *****************************************/ 00086 /*****************************************************************************/ 00087 00101 class Logger 00102 { 00103 Timing m_timing; 00104 char m_buf[MAX_LOG_LINE]; 00105 set<int> m_setLogLevels; 00106 char m_strHeader[MAX_HEADER]; 00107 ostream* m_os; 00109 public: 00110 Logger( ostream& os=cout, int iMinLogLevel=0, int iMaxLogLevel = 0); 00111 00112 // different methods associated with logging messages 00113 bool log ( int iLevel, string str ); 00114 bool log ( int i, char *str, ... ); 00115 bool logWithTime ( int iLevel, char *str, ... ); 00116 void restartTimer ( ); 00117 Timing getTiming ( ); 00118 bool isInLogLevel ( int iLevel ); 00119 00120 bool addLogLevel ( int iLevel ); 00121 bool addLogRange ( int iMin, int iMax ); 00122 00123 char* getHeader ( ); 00124 bool setHeader ( char *str ); 00125 bool setHeader ( int i1, int i2 ); 00126 00127 bool setOutputStream ( ostream& os ); 00128 ostream& getOutputStream ( ); 00129 void showLogLevels ( ostream& os ); 00130 }; 00131 00132 00133 #endif