FairRoot/PandaRoot
3.52/src/utilities/Log.h
Go to the documentation of this file.
1 #ifndef __PHOTOS_LOG_CLASS_HEADER__
2 #define __PHOTOS_LOG_CLASS_HEADER__
3 
13 #include <iostream>
14 #include <string>
15 #include <sstream>
16 #include <stdlib.h>
17 #include <list>
18 #include "Photos.h"
19 // TEMPORARY
20 #include "f_Init.h"
21 
22 using std::stringstream;
23 using std::string;
24 using std::streambuf;
25 using std::ostream;
26 using std::list;
27 using std::cout;
28 using std::endl;
29 
30 namespace Photospp
31 {
32 
33 class Log
34 {
35 public:
37  static void Summary();
38 
40  static void SummaryAtExit() { atexit(Summary); }
41 
44  static void AddDecay(int type);
45 
49  static ostream& Debug(unsigned short int code=0, bool count=true);
50  static ostream& Info(bool count=true);
51  static ostream& Warning(bool count=true);
52  static ostream& Error(bool count=true);
53 
56  static void LogInfo (bool flag=true) { iAction=flag; }
57  static void LogWarning(bool flag=true) { wAction=flag; }
58  static void LogError (bool flag=true) { eAction=flag; }
59 
60  static void LogAll (bool flag=true) { iAction=wAction=eAction=flag; dRangeS=0; dRangeE=65535; }
61 
62  // TEMPORARY
63  static void LogPhlupa(int from, int to) { phlupy_.ipoinm=from; phlupy_.ipoin=to; }
64 
67  static void LogDebug(unsigned short s=0,unsigned short e=65535) { dRangeS=s; dRangeE=e; }
68 
72  static void Assert(bool check, char *text=NULL);
73 
76  static void Fatal(string text, unsigned short int code=0);
77  static void Fatal(unsigned short int code=0) { Fatal(NULL,code); }
78 
86  static void RedirectOutput(void (*func)(), ostream& where=*out);
87  static void RedirectOutput(ostream& where=*out);
90  static void RevertOutput() { std::cout.rdbuf(bCout); std::cerr.rdbuf(bCerr); }
91 
94  static void IgnoreFailedAssert(bool flag=true) { asAction=!flag; }
95 
98  static void IgnoreRedirection(bool flag=true) { rAction=!flag; }
99 
102  static void IgnoreFatal(unsigned short s=0,unsigned short e=65535) { faRangeS=s; faRangeE=e; }
103 
107  static void SetOutput(ostream *newOut) { out=newOut; }
108  static void SetOutput(ostream &newOut) { out=&newOut; }
109 
111  static void SetWarningLimit(int x) { warnLimit=x; }
112 
113 protected:
114  static streambuf *bCout,*bCerr;
115  static ostream *out;
116  static stringstream buf;
117  static int warnLimit;
118  static int decays[4];
120  static int iCount, wCount, eCount, asCount, asFailedCount;
121  static bool iAction,wAction,eAction,asAction,rAction;
126 protected:
127  typedef struct
128  {
129  unsigned long address;
130  unsigned long size;
131  char file[64];
132  unsigned long line;
133  } Pointer;
134  static list<Pointer*> *PointerList;
135 public:
136 #ifdef _LOG_DEBUG_MODE_
137  static void NewPointer(unsigned long address, unsigned long size, const char *file, unsigned long line)
138  {
139  if(!PointerList)
140  {
141  PointerList = new list<Pointer *>();
142  atexit(PrintAllocatedPointers);
143  }
144  Pointer *info = new Pointer();
145  info->address = address;
146  info->size = size;
147  info->line = line;
148  strncpy(info->file, file, 63);
149  PointerList->push_front(info);
150  }
151  static void DeletePointer(unsigned long address)
152  {
153  if(!PointerList) return;
154  for(list<Pointer*>::iterator i = PointerList->begin(); i!=PointerList->end(); i++)
155  {
156  if((*i)->address == address)
157  {
158  PointerList->remove((*i));
159  break;
160  }
161  }
162  }
163  static bool PointerCompare(Pointer *one, Pointer *two)
164  {
165  int eq = strcmp(one->file,two->file);
166  if(eq<0) return true;
167  else if(eq>0) return false;
168  return (one->line <= two->line);
169  }
170  static void PrintAllocatedPointers()
171  {
172  if(!PointerList) return;
173  int pointers=0,buf=0;
174  unsigned long total=0;
175  char *lastS=" ";
176  unsigned int lastL=0;
177  if(PointerList->size()==0)
178  {
179  cout<<"----------------------------UNFREED MEMORY POINTERS----------------------------\n";
180  cout<<" ... NONE ...\n";
181  cout<<"-------------------------------------------------------------------------------\n";
182  return;
183  }
184  PointerList->sort(PointerCompare);
185  cout<<"---------------------------UNFREED MEMORY POINTERS---------------------------\n";
186  for(list<Pointer*>::iterator i = PointerList->begin(); i!=PointerList->end(); i++)
187  {
188  total+=(*i)->size;
189  ++pointers;
190  if(strcmp(lastS,(*i)->file)==0)
191  {
192  if(lastL==(*i)->line)
193  {
194  printf("%56s%10lub (%lu)\n"," ",(*i)->size,(*i)->address);
195  continue;
196  }
197  }
198  lastS=(*i)->file;
199  lastL=(*i)->line;
200  printf("%s%n:",(*i)->file,&buf);
201  printf("%-*lu%10lub (%lu)\n",55-buf,(*i)->line,(*i)->size,(*i)->address);
202  }
203  cout<<endl<<total<<"\tbytes"<<endl;
204  cout<<pointers<<"\tpointers"<<endl;
205  cout<<"-------------------------------------------------------------------------------\n";
206  };
207 #endif //_LOG_DEBUG_MODE_
208 };
209 
210 #ifdef _LOG_DEBUG_MODE_
211 
218 inline void* operator new(long unsigned int size, const char *filename, int line)
219 {
220  void *ptr = (void *)malloc(size);
221  Photos::Log::NewPointer((unsigned long)ptr, size, filename, line);
222  return(ptr);
223 }
224 
225 inline void operator delete(void *p)
226 {
227  Photos::Log::DeletePointer((unsigned long)p);
228  free(p);
229 }
230 
231 #define new new(__FILE__, __LINE__)
232 
233 #endif //_LOG_DEBUG_MODE_
234 
235 } // namespace Photospp
236 #endif
Double_t p
Definition: anasim.C:58
printf("RealTime=%f seconds, CpuTime=%f seconds\n", rtime, ctime)
static int faRangeS
Definition: Log.h:119
static int wCount
Definition: Log.h:120
Int_t i
Definition: run_full.C:25
TFile * file
TF1 * func
static void Assert(bool check, char *text=NULL)
Definition: Log.cxx:72
static void LogPhlupa(int from, int to)
TLorentzVector s
Definition: Pnd2DStar.C:50
static void LogAll(bool flag=true)
static list< Pointer * > * PointerList
Definition: Log.h:134
static void SetWarningLimit(int x)
static void AddDecay(int type)
Definition: Log.cxx:25
static void Fatal(unsigned short int code=0)
static int warnLimit
Definition: Log.h:117
static void IgnoreFailedAssert(bool flag=true)
static void IgnoreFatal(unsigned short s=0, unsigned short e=65535)
static void IgnoreRedirection(bool flag=true)
static ostream & Info(bool count=true)
Definition: Log.cxx:38
struct @25 phlupy_
static ostream & Debug(unsigned short int code=0, bool count=true)
Definition: Log.cxx:30
basic_ostream< char, char_traits< char > > ostream
static ostream * out
Definition: Log.h:115
static int decays[4]
Definition: Log.h:118
static void LogError(bool flag=true)
static int asFailedCount
Definition: Log.h:120
static void LogWarning(bool flag=true)
TF1 * total
static int dRangeE
Definition: Log.h:119
static streambuf * bCout
Definition: Log.h:114
static int iCount
Definition: Log.h:120
static bool wAction
Definition: Log.h:121
static ostream & Warning(bool count=true)
Definition: Log.cxx:46
static stringstream buf
Definition: Log.h:116
static void SetOutput(ostream &newOut)
static void Summary()
Definition: Log.cxx:110
static void SummaryAtExit()
static bool rAction
Definition: Log.h:121
static streambuf * bCerr
Definition: Log.h:114
Double_t x
static bool iAction
Definition: Log.h:121
static T Log(const T &x)
Definition: PndCAMath.h:40
static void Fatal(string text, unsigned short int code=0)
static int dRangeS
Definition: Log.h:119
static bool eAction
Definition: Log.h:121
static int faCount
Definition: Log.h:119
static bool asAction
Definition: Log.h:121
int count
static void LogDebug(unsigned short s=0, unsigned short e=65535)
static int eCount
Definition: Log.h:120
static int faRangeE
Definition: Log.h:119
static void RevertOutput()
static void SetOutput(ostream *newOut)
static void RedirectOutput(void(*func)(), ostream &where=*out)
Definition: Log.cxx:90
static void LogInfo(bool flag=true)
static int asCount
Definition: Log.h:120
static ostream & Error(bool count=true)
Definition: Log.cxx:64
const string filename
static int dCount
Definition: Log.h:119