StRoot  1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Groups Pages
Log.h
1 #ifndef __LOG_CLASS_HEADER__
2 #define __LOG_CLASS_HEADER__
3 
13 #include <iostream>
14 #include <string>
15 #include <sstream>
16 #include <stdlib.h>
17 #include <cstring>
18 #include <list>
19 
20 using std::stringstream;
21 using std::string;
22 using std::streambuf;
23 using std::ostream;
24 using std::list;
25 using std::cout;
26 using std::endl;
27 
28 namespace Tauolapp
29 {
30 
31 class Log
32 {
33 public:
35  static void Summary();
36 
38  static void SummaryAtExit() { atexit(Summary); }
39 
42  static void AddDecay(int type);
43 
47  static ostream& Debug(unsigned short int code=0, bool count=true);
48  static ostream& Info(bool count=true);
49  static ostream& Warning(bool count=true);
50  static ostream& Error(bool count=true);
51 
54  static void LogInfo (bool flag=true) { iAction=flag; }
55  static void LogWarning(bool flag=true) { wAction=flag; }
56  static void LogError (bool flag=true) { eAction=flag; }
57 
58  static void LogAll (bool flag=true) { iAction=wAction=eAction=flag; dRangeS=0; dRangeE=65535; }
59 
62  static void LogDebug(unsigned short s=0,unsigned short e=65535) { dRangeS=s; dRangeE=e; }
63 
67  static void Assert(bool check, char *text=NULL);
68 
71  static void Fatal(string text, unsigned short int code=0);
72  static void Fatal(unsigned short int code=0) { Fatal("",code); }
73 
81  static void RedirectOutput(void (*func)(), ostream& where=*out);
82  static void RedirectOutput(ostream& where=*out);
85  static void RevertOutput() { std::cout.rdbuf(bCout); std::cerr.rdbuf(bCerr); }
86 
89  static void IgnoreFailedAssert(bool flag=true) { asAction=!flag; }
90 
93  static void IgnoreRedirection(bool flag=true) { rAction=!flag; }
94 
97  static void IgnoreFatal(unsigned short s=0,unsigned short e=65535) { faRangeS=s; faRangeE=e; }
98 
102  static void SetOutput(ostream *newOut) { out=newOut; }
103  static void SetOutput(ostream &newOut) { out=&newOut; }
104 
106  static void SetWarningLimit(int x) { warnLimit=x; }
107 
108 protected:
109  static streambuf *bCout,*bCerr;
110  static ostream *out;
111  static stringstream buf;
112  static int warnLimit;
113  static int decays[4];
114  static int dCount,dRangeS,dRangeE,faCount,faRangeS,faRangeE;
115  static int iCount, wCount, eCount, asCount, asFailedCount;
116  static bool iAction,wAction,eAction,asAction,rAction;
121 protected:
122  typedef struct
123  {
124  unsigned long address;
125  unsigned long size;
126  char file[64];
127  unsigned long line;
128  } Pointer;
129  static list<Pointer*> *PointerList;
130 public:
131 #ifdef _LOG_DEBUG_MODE_
132  static void NewPointer(unsigned long address, unsigned long size, const char *file, unsigned long line)
133  {
134  if(!PointerList)
135  {
136  PointerList = new list<Pointer *>();
137  atexit(PrintAllocatedPointers);
138  }
139  Pointer *info = new Pointer();
140  info->address = address;
141  info->size = size;
142  info->line = line;
143  strncpy(info->file, file, 63);
144  PointerList->push_front(info);
145  }
146  static void DeletePointer(unsigned long address)
147  {
148  if(!PointerList) return;
149  for(list<Pointer*>::iterator i = PointerList->begin(); i!=PointerList->end(); i++)
150  {
151  if((*i)->address == address)
152  {
153  PointerList->remove((*i));
154  break;
155  }
156  }
157  }
158  static bool PointerCompare(Pointer *one, Pointer *two)
159  {
160  int eq = strcmp(one->file,two->file);
161  if(eq<0) return true;
162  else if(eq>0) return false;
163  return (one->line <= two->line);
164  }
165  static void PrintAllocatedPointers()
166  {
167  if(!PointerList) return;
168  int pointers=0,buf=0;
169  unsigned long total=0;
170  char *lastS=" ";
171  int lastL=0;
172  if(PointerList->size()==0)
173  {
174  cout<<"----------------------------UNFREED MEMORY POINTERS----------------------------\n";
175  cout<<" ... NONE ...\n";
176  cout<<"-------------------------------------------------------------------------------\n";
177  return;
178  }
179  PointerList->sort(PointerCompare);
180  cout<<"---------------------------UNFREED MEMORY POINTERS---------------------------\n";
181  for(list<Pointer*>::iterator i = PointerList->begin(); i!=PointerList->end(); i++)
182  {
183  total+=(*i)->size;
184  ++pointers;
185  if(strcmp(lastS,(*i)->file)==0)
186  {
187  if(lastL==(*i)->line)
188  {
189  printf("%56s%10lub (%lu)\n"," ",(*i)->size,(*i)->address);
190  continue;
191  }
192  }
193  lastS=(*i)->file;
194  lastL=(*i)->line;
195  printf("%s%n:",(*i)->file,&buf);
196  printf("%-*lu%10lub (%lu)\n",55-buf,(*i)->line,(*i)->size,(*i)->address);
197  }
198  cout<<endl<<total<<"\tbytes"<<endl;
199  cout<<pointers<<"\tpointers"<<endl;
200  cout<<"-------------------------------------------------------------------------------\n";
201  };
202 #endif //_LOG_DEBUG_MODE_
203 };
204 
205 #ifdef _LOG_DEBUG_MODE_
206 
213 inline void* operator new(size_t size, const char *filename, int line)
214 {
215  void *ptr = (void *)malloc(size);
216  Log::NewPointer((unsigned long)ptr, size, filename, line);
217  return(ptr);
218 }
219 
220 inline void operator delete(void *p)
221 {
222  Log::DeletePointer((unsigned long)p);
223  free(p);
224 }
225 
226 #define new new(__FILE__, __LINE__)
227 
228 #endif //_LOG_DEBUG_MODE_
229 
230 } // namespace Tauolapp
231 #endif
static void LogInfo(bool flag=true)
Definition: Log.h:54
static void SummaryAtExit()
Definition: Log.h:38
Definition: FJcore.h:367
static void IgnoreFailedAssert(bool flag=true)
Definition: Log.h:89
static void IgnoreRedirection(bool flag=true)
Definition: Log.h:93
static void AddDecay(int type)
Definition: Log.cxx:25
static void SetWarningLimit(int x)
Definition: Log.h:106
static void SetOutput(ostream *newOut)
Definition: Log.h:102
static void RedirectOutput(void(*func)(), ostream &where=*out)
Definition: Log.cxx:93
static void Summary()
Definition: Log.cxx:113
static ostream & Debug(unsigned short int code=0, bool count=true)
Definition: Log.cxx:30
static void LogDebug(unsigned short s=0, unsigned short e=65535)
Definition: Log.h:62
static void IgnoreFatal(unsigned short s=0, unsigned short e=65535)
Definition: Log.h:97
static void Fatal(string text, unsigned short int code=0)
static void Assert(bool check, char *text=NULL)
Definition: Log.cxx:73
static void RevertOutput()
Definition: Log.h:85