PandaRoot
3.52/src/utilities/Log.h
Go to the documentation of this file.
1 //****************************************************************************
2 //* This file is part of PandaRoot. *
3 //* *
4 //* PandaRoot is distributed under the terms of the *
5 //* GNU General Public License (GPL) version 3, *
6 //* copied verbatim in the file "LICENSE". *
7 //* *
8 //* Copyright (C) 2006 - 2024 FAIR GmbH and copyright holders of PandaRoot *
9 //* The copyright holders are listed in the file "COPYRIGHTHOLDERS". *
10 //* The authors are listed in the file "AUTHORS". *
11 //****************************************************************************
12 
13 //****************************************************************************
14 //* This file is part of PandaRoot. *
15 //* *
16 //* PandaRoot is distributed under the terms of the *
17 //* GNU General Public License (GPL) version 3, *
18 //* copied verbatim in the file "LICENSE". *
19 //* *
20 //* Copyright (C) 2006 - 2024 FAIR GmbH and copyright holders of PandaRoot *
21 //* The copyright holders are listed in the file "COPYRIGHTHOLDERS". *
22 //* The authors are listed in the file "AUTHORS". *
23 //****************************************************************************
24 
25 #ifndef __PHOTOS_LOG_CLASS_HEADER__
26 #define __PHOTOS_LOG_CLASS_HEADER__
27 
37 #include <iostream>
38 #include <string>
39 #include <sstream>
40 #include <stdlib.h>
41 #include <list>
42 #include "Photos.h"
43 // TEMPORARY
44 #include "f_Init.h"
45 
46 using std::cout;
47 using std::endl;
48 using std::list;
49 using std::ostream;
50 using std::streambuf;
51 using std::string;
52 using std::stringstream;
53 
54 namespace Photospp {
55 
56 class Log {
57  public:
59  static void Summary();
60 
62  static void SummaryAtExit() { atexit(Summary); }
63 
66  static void AddDecay(int type);
67 
71  static ostream &Debug(unsigned short int code = 0, bool count = true);
72  static ostream &Info(bool count = true);
73  static ostream &Warning(bool count = true);
74  static ostream &Error(bool count = true);
75 
78  static void LogInfo(bool flag = true) { iAction = flag; }
79  static void LogWarning(bool flag = true) { wAction = flag; }
80  static void LogError(bool flag = true) { eAction = flag; }
81 
82  static void LogAll(bool flag = true)
83  {
84  iAction = wAction = eAction = flag;
85  dRangeS = 0;
86  dRangeE = 65535;
87  }
88 
89  // TEMPORARY
90  static void LogPhlupa(int from, int to)
91  {
92  phlupy_.ipoinm = from;
93  phlupy_.ipoin = to;
94  }
95 
98  static void LogDebug(unsigned short s = 0, unsigned short e = 65535)
99  {
100  dRangeS = s;
101  dRangeE = e;
102  }
103 
107  static void Assert(bool check, char *text = nullptr);
108 
111  static void Fatal(string text, unsigned short int code = 0);
112  static void Fatal(unsigned short int code = 0) { Fatal(nullptr, code); }
113 
121  static void RedirectOutput(void (*func)(), ostream &where = *out);
122  static void RedirectOutput(ostream &where = *out);
125  static void RevertOutput()
126  {
127  std::cout.rdbuf(bCout);
128  std::cerr.rdbuf(bCerr);
129  }
130 
133  static void IgnoreFailedAssert(bool flag = true) { asAction = !flag; }
134 
137  static void IgnoreRedirection(bool flag = true) { rAction = !flag; }
138 
141  static void IgnoreFatal(unsigned short s = 0, unsigned short e = 65535)
142  {
143  faRangeS = s;
144  faRangeE = e;
145  }
146 
150  static void SetOutput(ostream *newOut) { out = newOut; }
151  static void SetOutput(ostream &newOut) { out = &newOut; }
152 
154  static void SetWarningLimit(int x) { warnLimit = x; }
155 
156  protected:
157  static streambuf *bCout, *bCerr;
158  static ostream *out;
159  static stringstream buf;
160  static int warnLimit;
161  static int decays[4];
162  static int dCount, dRangeS, dRangeE, faCount, faRangeS, faRangeE;
163  static int iCount, wCount, eCount, asCount, asFailedCount;
164  static bool iAction, wAction, eAction, asAction, rAction;
169  protected:
170  typedef struct {
171  unsigned long address;
172  unsigned long size;
173  char file[64];
174  unsigned long line;
175  } Pointer;
176  static list<Pointer *> *PointerList;
177 
178  public:
179 #ifdef _LOG_DEBUG_MODE_
180  static void NewPointer(unsigned long address, unsigned long size, const char *file, unsigned long line)
181  {
182  if (!PointerList) {
183  PointerList = new list<Pointer *>();
184  atexit(PrintAllocatedPointers);
185  }
186  Pointer *info = new Pointer();
187  info->address = address;
188  info->size = size;
189  info->line = line;
190  strncpy(info->file, file, 63);
191  PointerList->push_front(info);
192  }
193  static void DeletePointer(unsigned long address)
194  {
195  if (!PointerList)
196  return;
197  for (list<Pointer *>::iterator i = PointerList->begin(); i != PointerList->end(); i++) {
198  if ((*i)->address == address) {
199  PointerList->remove((*i));
200  break;
201  }
202  }
203  }
204  static bool PointerCompare(Pointer *one, Pointer *two)
205  {
206  int eq = strcmp(one->file, two->file);
207  if (eq < 0)
208  return true;
209  else if (eq > 0)
210  return false;
211  return (one->line <= two->line);
212  }
213  static void PrintAllocatedPointers()
214  {
215  if (!PointerList)
216  return;
217  int pointers = 0, buf = 0;
218  unsigned long total = 0;
219  char *lastS = " ";
220  unsigned int lastL = 0;
221  if (PointerList->size() == 0) {
222  cout << "----------------------------UNFREED MEMORY POINTERS----------------------------\n";
223  cout << " ... NONE ...\n";
224  cout << "-------------------------------------------------------------------------------\n";
225  return;
226  }
227  PointerList->sort(PointerCompare);
228  cout << "---------------------------UNFREED MEMORY POINTERS---------------------------\n";
229  for (list<Pointer *>::iterator i = PointerList->begin(); i != PointerList->end(); i++) {
230  total += (*i)->size;
231  ++pointers;
232  if (strcmp(lastS, (*i)->file) == 0) {
233  if (lastL == (*i)->line) {
234  printf("%56s%10lub (%lu)\n", " ", (*i)->size, (*i)->address);
235  continue;
236  }
237  }
238  lastS = (*i)->file;
239  lastL = (*i)->line;
240  printf("%s%n:", (*i)->file, &buf);
241  printf("%-*lu%10lub (%lu)\n", 55 - buf, (*i)->line, (*i)->size, (*i)->address);
242  }
243  cout << endl << total << "\tbytes" << endl;
244  cout << pointers << "\tpointers" << endl;
245  cout << "-------------------------------------------------------------------------------\n";
246  };
247 #endif //_LOG_DEBUG_MODE_
248 };
249 
250 #ifdef _LOG_DEBUG_MODE_
251 
258 inline void *operator new(long unsigned int size, const char *filename, int line)
259 {
260  void *ptr = (void *)malloc(size);
261  Photos::Log::NewPointer((unsigned long)ptr, size, filename, line);
262  return (ptr);
263 }
264 
265 inline void operator delete(void *p)
266 {
267  Photos::Log::DeletePointer((unsigned long)p);
268  free(p);
269 }
270 
271 #define new new (__FILE__, __LINE__)
272 
273 #endif //_LOG_DEBUG_MODE_
274 
275 } // namespace Photospp
276 #endif
static void Assert(bool check, char *text=nullptr)
static int faRangeS
Definition: Log.h:162
static int wCount
Definition: Log.h:163
static ostream & Info(bool count=true)
static ostream & Warning(bool count=true)
static ostream & Error(bool count=true)
static ostream & Debug(unsigned short int code=0, bool count=true)
static void LogPhlupa(int from, int to)
unsigned long line
Definition: Log.h:174
static void LogAll(bool flag=true)
static list< Pointer * > * PointerList
Definition: Log.h:176
static void SetWarningLimit(int x)
static void Fatal(unsigned short int code=0)
static int warnLimit
Definition: Log.h:160
static void IgnoreFailedAssert(bool flag=true)
static void IgnoreFatal(unsigned short s=0, unsigned short e=65535)
unsigned int i
Definition: P4_F32vec4.h:33
static void IgnoreRedirection(bool flag=true)
Definition: Log.h:54
static void AddDecay(int type)
static void Summary()
static ostream * out
Definition: Log.h:158
static int decays[4]
Definition: Log.h:161
char file[64]
Definition: Log.h:173
static void LogError(bool flag=true)
static int asFailedCount
Definition: Log.h:163
static void LogWarning(bool flag=true)
struct @23 phlupy_
static void RedirectOutput(void(*func)(), ostream &where= *out)
static int dRangeE
Definition: Log.h:162
static streambuf * bCout
Definition: Log.h:157
static int iCount
Definition: Log.h:163
static bool wAction
Definition: Log.h:164
static stringstream buf
Definition: Log.h:159
static void SetOutput(ostream &newOut)
unsigned long size
Definition: Log.h:172
static void SummaryAtExit()
static bool rAction
Definition: Log.h:164
static streambuf * bCerr
Definition: Log.h:157
static bool iAction
Definition: Log.h:164
static T Log(const T &x)
Definition: PndCAMath.h:73
static void Fatal(string text, unsigned short int code=0)
static int dRangeS
Definition: Log.h:162
static bool eAction
Definition: Log.h:164
static int faCount
Definition: Log.h:162
static bool asAction
Definition: Log.h:164
basic_ostream< char, char_traits< char > > ostream
static void LogDebug(unsigned short s=0, unsigned short e=65535)
static int eCount
Definition: Log.h:163
static int faRangeE
Definition: Log.h:162
static void RevertOutput()
unsigned long address
Definition: Log.h:171
static void SetOutput(ostream *newOut)
static void LogInfo(bool flag=true)
static int asCount
Definition: Log.h:163
static int dCount
Definition: Log.h:162