Belle II Software  release-06-02-00
Log.h
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 
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 Photospp {
29 
30  extern void (*PHOERR)(int, const char*, double);
31  extern void (*PHOREP)();
32 
33  class Log {
34  public:
36  static void Summary();
37 
39  static void SummaryAtExit() { atexit(Summary); }
40 
43  static void AddDecay(int type);
44 
48  static ostream& Debug(unsigned short int code = 0, bool count = true);
49  static ostream& Info(bool count = true);
50  static ostream& Warning(bool count = true);
51  static ostream& Error(bool count = true);
52 
55  static void LogInfo(bool flag = true) { iAction = flag; }
56  static void LogWarning(bool flag = true) { wAction = flag; }
57  static void LogError(bool flag = true) { eAction = flag; }
58 
59  static void LogAll(bool flag = true) { iAction = wAction = eAction = flag; dRangeS = 0; dRangeE = 65535; }
60 
61  // TEMPORARY
62  static void LogPhlupa(int from, int to) { phlupy.ipoinm = from; phlupy.ipoin = to; }
63 
66  static void LogDebug(unsigned short s = 0, unsigned short e = 65535) { dRangeS = s; dRangeE = e; }
67 
71  static void Assert(bool check, char* text = NULL);
72 
75  static void Fatal(string text, unsigned short int code = 0);
76  static void Fatal(unsigned short int code = 0) { Fatal(NULL, code); }
77 
85  static void RedirectOutput(void (*func)(), ostream& where = *out);
86  static void RedirectOutput(ostream& where = *out);
89  static void RevertOutput() { std::cout.rdbuf(bCout); std::cerr.rdbuf(bCerr); }
90 
93  static void IgnoreFailedAssert(bool flag = true) { asAction = !flag; }
94 
97  static void IgnoreRedirection(bool flag = true) { rAction = !flag; }
98 
101  static void IgnoreFatal(unsigned short s = 0, unsigned short e = 65535) { faRangeS = s; faRangeE = e; }
102 
106  static void SetOutput(ostream* newOut) { out = newOut; }
107  static void SetOutput(ostream& newOut) { out = &newOut; }
108 
110  static void SetWarningLimit(int x) { warnLimit = x; }
111 
113  static void PHOERR(int IMES, const char* TEXT, double DATA);
114 
116  static void PHOREP();
117 
118  protected:
119  static streambuf* bCout, *bCerr;
120  static ostream* out;
121  static stringstream buf;
122  static int warnLimit;
123  static int decays[4];
124  static int dCount, dRangeS, dRangeE, faCount, faRangeS, faRangeE;
125  static int iCount, wCount, eCount, asCount, asFailedCount;
126  static bool iAction, wAction, eAction, asAction, rAction;
131  protected:
132  typedef struct {
133  unsigned long address;
134  unsigned long size;
135  char file[64];
136  unsigned long line;
137  } Pointer;
138  static list<Pointer*>* PointerList;
139  public:
140 #ifdef _LOG_DEBUG_MODE_
141  static void NewPointer(unsigned long address, unsigned long size, const char* file, unsigned long line)
142  {
143  if (!PointerList) {
144  PointerList = new list<Pointer*>();
145  atexit(PrintAllocatedPointers);
146  }
147  Pointer* info = new Pointer();
148  info->address = address;
149  info->size = size;
150  info->line = line;
151  strncpy(info->file, file, 63);
152  PointerList->push_front(info);
153  }
154  static void DeletePointer(unsigned long address)
155  {
156  if (!PointerList) return;
157  for (list<Pointer*>::iterator i = PointerList->begin(); i != PointerList->end(); i++) {
158  if ((*i)->address == address) {
159  PointerList->remove((*i));
160  break;
161  }
162  }
163  }
164  static bool PointerCompare(Pointer* one, Pointer* two)
165  {
166  int eq = strcmp(one->file, two->file);
167  if (eq < 0) return true;
168  else if (eq > 0) return false;
169  return (one->line <= two->line);
170  }
171  static void PrintAllocatedPointers()
172  {
173  if (!PointerList) return;
174  int pointers = 0, buf = 0;
175  unsigned long total = 0;
176  char* lastS = " ";
177  unsigned int lastL = 0;
178  if (PointerList->size() == 0) {
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  total += (*i)->size;
188  ++pointers;
189  if (strcmp(lastS, (*i)->file) == 0) {
190  if (lastL == (*i)->line) {
191  printf("%56s%10lub (%lu)\n", " ", (*i)->size, (*i)->address);
192  continue;
193  }
194  }
195  lastS = (*i)->file;
196  lastL = (*i)->line;
197  printf("%s%n:", (*i)->file, &buf);
198  printf("%-*lu%10lub (%lu)\n", 55 - buf, (*i)->line, (*i)->size, (*i)->address);
199  }
200  cout << endl << total << "\tbytes" << endl;
201  cout << pointers << "\tpointers" << endl;
202  cout << "-------------------------------------------------------------------------------\n";
203  };
204 #endif //_LOG_DEBUG_MODE_
205  };
206 
207 #ifdef _LOG_DEBUG_MODE_
208 
215  inline void* operator new(long unsigned int size, const char* filename, int line)
216  {
217  void* ptr = (void*)malloc(size);
218  Photos::Log::NewPointer((unsigned long)ptr, size, filename, line);
219  return (ptr);
220  }
221 
222  inline void operator delete(void* p)
223  {
224  Photos::Log::DeletePointer((unsigned long)p);
225  free(p);
226  }
227 
228 #define new new(__FILE__, __LINE__)
229 
230 #endif //_LOG_DEBUG_MODE_
231 
232 } // namespace Photospp
233 #endif
static ostream & Debug(unsigned short int code=0, bool count=true)
Four logging entries.
Definition: Log.cc:32
static void AddDecay(int type)
Adds the decay to the counter.
Definition: Log.cc:27
static void Assert(bool check, char *text=NULL)
Asserts logical value.
Definition: Log.cc:72
static void SetWarningLimit(int x)
Change the limit of warnings that will be displayed.
Definition: Log.h:110
static void RedirectOutput(void(*func)(), ostream &where= *out)
Redirects output to log.
Definition: Log.cc:90
static void IgnoreFatal(unsigned short s=0, unsigned short e=65535)
Do not exit when Log::Fatal() with the code within the provided range is called.
Definition: Log.h:101
static void SetOutput(ostream *newOut)
Change the output of the logged messages.
Definition: Log.h:106
static void LogInfo(bool flag=true)
Turns off or on particular types of messages By default, only debugging messages are turned off.
Definition: Log.h:55
static void Fatal(string text, unsigned short int code=0)
Terminates the program with added default message or 'text'.
static void PHOREP()
Final report of warnings from internal part of PHOTOS (originally in F77)
Definition: Log.cc:283
static void RevertOutput()
WARNING! If You're redirecting more than one function, do not forget to use RevertOutput() afterwards...
Definition: Log.h:89
static void IgnoreRedirection(bool flag=true)
Ignores redirections of functions' output.
Definition: Log.h:97
static void PHOERR(int IMES, const char *TEXT, double DATA)
Warnings on errors from internal part of PHOTOS (originally in F77)
Definition: Log.cc:155
static void SummaryAtExit()
Shows the summary at the end of the program.
Definition: Log.h:39
static void IgnoreFailedAssert(bool flag=true)
Do not exit when Log::Assert() check is false.
Definition: Log.h:93
static void LogDebug(unsigned short s=0, unsigned short e=65535)
Sets the range of debug codes that will be printed.
Definition: Log.h:66
static void Summary()
Shows the summary of all messages.
Definition: Log.cc:110
Memory leak tracking section.
Definition: Log.h:132