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 00050 #ifndef _LOGGER_ 00051 #define _LOGGER_ 00052 00053 #include <iostream> // needed for ostream (logging to output stream) 00054 #include <fstream> // needed for fstream (logging to file) 00055 #include <string> // needed for string 00056 #include <iomanip> // needed for setw 00057 #include <set> // needed for set 00058 00059 #ifdef WIN32 00060 #include <windows.h> // needed for DWORD and GetTickCount() function 00061 #include <time.h> // needed for time_t 00062 #else 00063 #include <sys/time.h> // needed for timeval 00064 #endif 00065 00066 using namespace std; 00067 00068 #define MAX_LOG_LINE 3072 00069 #define MAX_HEADER 128 00071 /*****************************************************************************/ 00072 /*********************** CLASS TIMING ****************************************/ 00073 /*****************************************************************************/ 00074 00077 class Timing 00078 { 00079 #ifdef WIN32 00080 DWORD time1; 00082 #else 00083 struct timeval time1; 00084 #endif 00085 00086 public: 00087 // methods to restart the timer, get the elapsed time and print messages 00088 #ifdef WIN32 00089 static double getTimeDifference ( DWORD tv1, DWORD tv2 ); 00090 #else 00091 static double getTimeDifference ( struct timeval t1, 00092 struct timeval t2 ); 00093 #endif 00094 void printTimeDiffWithText( ostream& os, 00095 char *str, 00096 int iFactor = 1000 ); 00097 double getElapsedTime ( int iFactor = 1 ); 00098 void restartTime ( ); 00099 } ; 00100 00101 00102 /*****************************************************************************/ 00103 /**************************** LOGGER *****************************************/ 00104 /*****************************************************************************/ 00105 00119 class Logger 00120 { 00121 Timing m_timing; 00122 char m_buf[MAX_LOG_LINE]; 00123 set<int> m_setLogLevels; 00125 pthread_mutex_t mutex_stream; 00126 char m_strHeader[MAX_HEADER]; 00127 ostream* m_os; 00128 string m_strSignal; 00130 public: 00131 Logger( ostream& os=cout, int iMinLogLevel=0, int iMaxLogLevel = 0); 00132 00133 // different methods associated with logging messages 00134 bool log ( int iLevel, string str ); 00135 bool log ( int i, char *str, ... ); 00136 bool logWithTime ( int iLevel, char *str, ... ); 00137 bool logFromSignal ( int iLevel, char *str, ... ); 00138 bool logSignal ( ); 00139 00140 void restartTimer ( ); 00141 Timing getTiming ( ); 00142 bool isInLogLevel ( int iLevel ); 00143 00144 bool addLogLevel ( int iLevel ); 00145 bool addLogRange ( int iMin, int iMax ); 00146 00147 char* getHeader ( ); 00148 bool setHeader ( char *str ); 00149 bool setHeader ( int i ); 00150 bool setHeader ( int i1, int i2 ); 00151 00152 bool setOutputStream ( ostream& os ); 00153 ostream& getOutputStream ( ); 00154 void showLogLevels ( ostream& os ); 00155 }; 00156 00157 00158 #endif