Log.h
00001 #ifndef __LOG_CLASS_HEADER__
00002 #define __LOG_CLASS_HEADER__
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013 #include <iostream>
00014 #include <string>
00015 #include <sstream>
00016 #include <stdlib.h>
00017 #include <cstring>
00018 #include <list>
00019
00020 using std::stringstream;
00021 using std::string;
00022 using std::streambuf;
00023 using std::ostream;
00024 using std::list;
00025 using std::cout;
00026 using std::endl;
00027
00028 namespace Tauolapp
00029 {
00030
00031 class Log
00032 {
00033 public:
00034
00035 static void Summary();
00036
00037
00038 static void SummaryAtExit() { atexit(Summary); }
00039
00040
00041
00042 static void AddDecay(int type);
00043
00044
00045
00046
00047 static ostream& Debug(unsigned short int code=0, bool count=true);
00048 static ostream& Info(bool count=true);
00049 static ostream& Warning(bool count=true);
00050 static ostream& Error(bool count=true);
00051
00052
00053
00054 static void LogInfo (bool flag=true) { iAction=flag; }
00055 static void LogWarning(bool flag=true) { wAction=flag; }
00056 static void LogError (bool flag=true) { eAction=flag; }
00057
00058 static void LogAll (bool flag=true) { iAction=wAction=eAction=flag; dRangeS=0; dRangeE=65535; }
00059
00060
00061
00062 static void LogDebug(unsigned short s=0,unsigned short e=65535) { dRangeS=s; dRangeE=e; }
00063
00064
00065
00066
00067 static void Assert(bool check, char *text=NULL);
00068
00069
00070
00071 static void Fatal(string text, unsigned short int code=0);
00072 static void Fatal(unsigned short int code=0) { Fatal("",code); }
00073
00074
00075
00076
00077
00078
00079
00080
00081 static void RedirectOutput(void (*func)(), ostream& where=*out);
00082 static void RedirectOutput(ostream& where=*out);
00083
00084
00085 static void RevertOutput() { std::cout.rdbuf(bCout); std::cerr.rdbuf(bCerr); }
00086
00087
00088
00089 static void IgnoreFailedAssert(bool flag=true) { asAction=!flag; }
00090
00091
00092
00093 static void IgnoreRedirection(bool flag=true) { rAction=!flag; }
00094
00095
00096
00097 static void IgnoreFatal(unsigned short s=0,unsigned short e=65535) { faRangeS=s; faRangeE=e; }
00098
00099
00100
00101
00102 static void SetOutput(ostream *newOut) { out=newOut; }
00103 static void SetOutput(ostream &newOut) { out=&newOut; }
00104
00105
00106 static void SetWarningLimit(int x) { warnLimit=x; }
00107
00108 protected:
00109 static streambuf *bCout,*bCerr;
00110 static ostream *out;
00111 static stringstream buf;
00112 static int warnLimit;
00113 static int decays[4];
00114 static int dCount,dRangeS,dRangeE,faCount,faRangeS,faRangeE;
00115 static int iCount, wCount, eCount, asCount, asFailedCount;
00116 static bool iAction,wAction,eAction,asAction,rAction;
00117
00118
00119
00120
00121 protected:
00122 typedef struct
00123 {
00124 unsigned long address;
00125 unsigned long size;
00126 char file[64];
00127 unsigned long line;
00128 } Pointer;
00129 static list<Pointer*> *PointerList;
00130 public:
00131 #ifdef _LOG_DEBUG_MODE_
00132 static void NewPointer(unsigned long address, unsigned long size, const char *file, unsigned long line)
00133 {
00134 if(!PointerList)
00135 {
00136 PointerList = new list<Pointer *>();
00137 atexit(PrintAllocatedPointers);
00138 }
00139 Pointer *info = new Pointer();
00140 info->address = address;
00141 info->size = size;
00142 info->line = line;
00143 strncpy(info->file, file, 63);
00144 PointerList->push_front(info);
00145 }
00146 static void DeletePointer(unsigned long address)
00147 {
00148 if(!PointerList) return;
00149 for(list<Pointer*>::iterator i = PointerList->begin(); i!=PointerList->end(); i++)
00150 {
00151 if((*i)->address == address)
00152 {
00153 PointerList->remove((*i));
00154 break;
00155 }
00156 }
00157 }
00158 static bool PointerCompare(Pointer *one, Pointer *two)
00159 {
00160 int eq = strcmp(one->file,two->file);
00161 if(eq<0) return true;
00162 else if(eq>0) return false;
00163 return (one->line <= two->line);
00164 }
00165 static void PrintAllocatedPointers()
00166 {
00167 if(!PointerList) return;
00168 int pointers=0,buf=0;
00169 unsigned long total=0;
00170 char *lastS=" ";
00171 int lastL=0;
00172 if(PointerList->size()==0)
00173 {
00174 cout<<"----------------------------UNFREED MEMORY POINTERS----------------------------\n";
00175 cout<<" ... NONE ...\n";
00176 cout<<"-------------------------------------------------------------------------------\n";
00177 return;
00178 }
00179 PointerList->sort(PointerCompare);
00180 cout<<"---------------------------UNFREED MEMORY POINTERS---------------------------\n";
00181 for(list<Pointer*>::iterator i = PointerList->begin(); i!=PointerList->end(); i++)
00182 {
00183 total+=(*i)->size;
00184 ++pointers;
00185 if(strcmp(lastS,(*i)->file)==0)
00186 {
00187 if(lastL==(*i)->line)
00188 {
00189 printf("%56s%10lub (%lu)\n"," ",(*i)->size,(*i)->address);
00190 continue;
00191 }
00192 }
00193 lastS=(*i)->file;
00194 lastL=(*i)->line;
00195 printf("%s%n:",(*i)->file,&buf);
00196 printf("%-*lu%10lub (%lu)\n",55-buf,(*i)->line,(*i)->size,(*i)->address);
00197 }
00198 cout<<endl<<total<<"\tbytes"<<endl;
00199 cout<<pointers<<"\tpointers"<<endl;
00200 cout<<"-------------------------------------------------------------------------------\n";
00201 };
00202 #endif //_LOG_DEBUG_MODE_
00203 };
00204
00205 #ifdef _LOG_DEBUG_MODE_
00206
00207
00208
00209
00210
00211
00212
00213 inline void* operator new(size_t size, const char *filename, int line)
00214 {
00215 void *ptr = (void *)malloc(size);
00216 Log::NewPointer((unsigned long)ptr, size, filename, line);
00217 return(ptr);
00218 }
00219
00220 inline void operator delete(void *p)
00221 {
00222 Log::DeletePointer((unsigned long)p);
00223 free(p);
00224 }
00225
00226 #define new new(__FILE__, __LINE__)
00227
00228 #endif //_LOG_DEBUG_MODE_
00229
00230 }
00231 #endif