src/Logger.h

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 
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 #define LOG_DEBUG 800
00072 #define LOG_INFO 900
00073 #define LOG_WARN 1000
00074 #define LOG_ERROR 1100
00075 #define LOG_FATAL 1200
00076 #define LOG_SOMI 2000
00077 
00078 #define LOG_PRINT(level, string) Log.log(level, "[%s:%d] " string, __FILE__, __LINE__)
00079 
00080 #define LOG_PRINTP(level, string, ...) Log.log(level, "[%s:%d] " string, __FILE__, __LINE__, __VA_ARGS__)
00081 
00082 /*****************************************************************************/
00083 /*********************** CLASS TIMING ****************************************/
00084 /*****************************************************************************/
00085 
00088 class Timing
00089 {
00090 #ifdef WIN32
00091   DWORD  time1;                  
00093 #else
00094   struct timeval time1;          
00095 #endif
00096   
00097 public:
00098   // methods to restart the timer, get the elapsed time and print messages
00099 #ifdef WIN32
00100   static double getTimeDifference    ( DWORD tv1, DWORD tv2                  );
00101 #else
00102   static double getTimeDifference    ( struct   timeval t1,
00103                                        struct   timeval t2                   );
00104 #endif
00105   void          printTimeDiffWithText( ostream& os,
00106                                        char     *str,
00107                                        int      iFactor = 1000               );
00108   double        getElapsedTime       ( int      iFactor = 1                  );
00109   void          restartTime          (                                       );
00110 } ;
00111 
00112 
00113 /*****************************************************************************/
00114 /**************************** LOGGER *****************************************/
00115 /*****************************************************************************/
00116 
00130 class Logger
00131 {
00132   Timing   m_timing;               
00133   char     m_buf[MAX_LOG_LINE];    
00134   set<int> m_setLogLevels;         
00136 //  pthread_mutex_t mutex_stream; 
00137   char     m_strHeader[MAX_HEADER];
00138   ostream* m_os;                   
00139   string   m_strSignal;            
00141 public:
00142   Logger( ostream& os=cout, int iMinLogLevel=0, int iMaxLogLevel = 0);
00143 
00144   // different methods associated with logging messages
00145   bool     log              ( int         iLevel, string str              );
00146   bool     log              ( int         i,      char   *str, ...        );
00147   bool     logWithTime      ( int         iLevel, char   *str, ...        );
00148   bool     logFromSignal    ( int         iLevel, char   *str, ...        );
00149   bool     logSignal        (                                             );
00150   
00151   void     restartTimer     (                                             );
00152   Timing   getTiming        (                                             );
00153   bool     isInLogLevel     ( int         iLevel                          );
00154 
00155   bool     addLogLevel      ( int         iLevel                          );
00156   bool     addLogRange      ( int         iMin,   int iMax                );
00157 
00158   char*    getHeader        (                                             );
00159   bool     setHeader        ( char        *str                            );
00160   bool     setHeader        ( int         i                               );
00161   bool     setHeader        ( int         i1,     int    i2               );
00162 
00163   bool     setOutputStream  ( ostream&    os                              );
00164   ostream& getOutputStream  (                                             );
00165   void     showLogLevels    ( ostream&    os                              );
00166 };
00167 
00168 
00169 #endif

Generated on Thu Apr 26 22:45:27 2007 for GangOfSix(GOS)-RoboCupTeamProject by  doxygen 1.5.1-p1