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::cout;
23 using std::endl;
24 using std::list;
25 using std::ostream;
26 using std::streambuf;
27 using std::string;
28 using std::stringstream;
29 
30 namespace Photospp {
31 
32 class Log {
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)
59  {
60  iAction = wAction = eAction = flag;
61  dRangeS = 0;
62  dRangeE = 65535;
63  }
64 
65  // TEMPORARY
66  static void LogPhlupa(int from, int to)
67  {
68  phlupy_.ipoinm = from;
69  phlupy_.ipoin = to;
70  }
71 
74  static void LogDebug(unsigned short s = 0, unsigned short e = 65535)
75  {
76  dRangeS = s;
77  dRangeE = e;
78  }
79 
83  static void Assert(bool check, char *text = nullptr);
84 
87  static void Fatal(string text, unsigned short int code = 0);
88  static void Fatal(unsigned short int code = 0) { Fatal(nullptr, code); }
89 
97  static void RedirectOutput(void (*func)(), ostream &where = *out);
98  static void RedirectOutput(ostream &where = *out);
101  static void RevertOutput()
102  {
103  std::cout.rdbuf(bCout);
104  std::cerr.rdbuf(bCerr);
105  }
106 
109  static void IgnoreFailedAssert(bool flag = true) { asAction = !flag; }
110 
113  static void IgnoreRedirection(bool flag = true) { rAction = !flag; }
114 
117  static void IgnoreFatal(unsigned short s = 0, unsigned short e = 65535)
118  {
119  faRangeS = s;
120  faRangeE = e;
121  }
122 
126  static void SetOutput(ostream *newOut) { out = newOut; }
127  static void SetOutput(ostream &newOut) { out = &newOut; }
128 
130  static void SetWarningLimit(int x) { warnLimit = x; }
131 
132  protected:
133  static streambuf *bCout, *bCerr;
134  static ostream *out;
135  static stringstream buf;
136  static int warnLimit;
137  static int decays[4];
138  static int dCount, dRangeS, dRangeE, faCount, faRangeS, faRangeE;
139  static int iCount, wCount, eCount, asCount, asFailedCount;
140  static bool iAction, wAction, eAction, asAction, rAction;
145  protected:
146  typedef struct {
147  unsigned long address;
148  unsigned long size;
149  char file[64];
150  unsigned long line;
151  } Pointer;
152  static list<Pointer *> *PointerList;
153 
154  public:
155 #ifdef _LOG_DEBUG_MODE_
156  static void NewPointer(unsigned long address, unsigned long size, const char *file, unsigned long line)
157  {
158  if (!PointerList) {
159  PointerList = new list<Pointer *>();
160  atexit(PrintAllocatedPointers);
161  }
162  Pointer *info = new Pointer();
163  info->address = address;
164  info->size = size;
165  info->line = line;
166  strncpy(info->file, file, 63);
167  PointerList->push_front(info);
168  }
169  static void DeletePointer(unsigned long address)
170  {
171  if (!PointerList)
172  return;
173  for (list<Pointer *>::iterator i = PointerList->begin(); i != PointerList->end(); i++) {
174  if ((*i)->address == address) {
175  PointerList->remove((*i));
176  break;
177  }
178  }
179  }
180  static bool PointerCompare(Pointer *one, Pointer *two)
181  {
182  int eq = strcmp(one->file, two->file);
183  if (eq < 0)
184  return true;
185  else if (eq > 0)
186  return false;
187  return (one->line <= two->line);
188  }
189  static void PrintAllocatedPointers()
190  {
191  if (!PointerList)
192  return;
193  int pointers = 0, buf = 0;
194  unsigned long total = 0;
195  char *lastS = " ";
196  unsigned int lastL = 0;
197  if (PointerList->size() == 0) {
198  cout << "----------------------------UNFREED MEMORY POINTERS----------------------------\n";
199  cout << " ... NONE ...\n";
200  cout << "-------------------------------------------------------------------------------\n";
201  return;
202  }
203  PointerList->sort(PointerCompare);
204  cout << "---------------------------UNFREED MEMORY POINTERS---------------------------\n";
205  for (list<Pointer *>::iterator i = PointerList->begin(); i != PointerList->end(); i++) {
206  total += (*i)->size;
207  ++pointers;
208  if (strcmp(lastS, (*i)->file) == 0) {
209  if (lastL == (*i)->line) {
210  printf("%56s%10lub (%lu)\n", " ", (*i)->size, (*i)->address);
211  continue;
212  }
213  }
214  lastS = (*i)->file;
215  lastL = (*i)->line;
216  printf("%s%n:", (*i)->file, &buf);
217  printf("%-*lu%10lub (%lu)\n", 55 - buf, (*i)->line, (*i)->size, (*i)->address);
218  }
219  cout << endl << total << "\tbytes" << endl;
220  cout << pointers << "\tpointers" << endl;
221  cout << "-------------------------------------------------------------------------------\n";
222  };
223 #endif //_LOG_DEBUG_MODE_
224 };
225 
226 #ifdef _LOG_DEBUG_MODE_
227 
234 inline void *operator new(long unsigned int size, const char *filename, int line)
235 {
236  void *ptr = (void *)malloc(size);
237  Photos::Log::NewPointer((unsigned long)ptr, size, filename, line);
238  return (ptr);
239 }
240 
241 inline void operator delete(void *p)
242 {
243  Photos::Log::DeletePointer((unsigned long)p);
244  free(p);
245 }
246 
247 #define new new (__FILE__, __LINE__)
248 
249 #endif //_LOG_DEBUG_MODE_
250 
251 } // namespace Photospp
252 #endif
static void Assert(bool check, char *text=nullptr)
static int faRangeS
Definition: Log.h:138
static int wCount
Definition: Log.h:139
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:150
static void LogAll(bool flag=true)
static list< Pointer * > * PointerList
Definition: Log.h:152
static void SetWarningLimit(int x)
static void Fatal(unsigned short int code=0)
static int warnLimit
Definition: Log.h:136
static void IgnoreFailedAssert(bool flag=true)
static void IgnoreFatal(unsigned short s=0, unsigned short e=65535)
unsigned int i
Definition: P4_F32vec4.h:21
static void IgnoreRedirection(bool flag=true)
Definition: Log.h:30
static void AddDecay(int type)
static void Summary()
static ostream * out
Definition: Log.h:134
static int decays[4]
Definition: Log.h:137
char file[64]
Definition: Log.h:149
static void LogError(bool flag=true)
static int asFailedCount
Definition: Log.h:139
static void LogWarning(bool flag=true)
struct @23 phlupy_
static void RedirectOutput(void(*func)(), ostream &where= *out)
static int dRangeE
Definition: Log.h:138
static streambuf * bCout
Definition: Log.h:133
static int iCount
Definition: Log.h:139
static bool wAction
Definition: Log.h:140
static stringstream buf
Definition: Log.h:135
static void SetOutput(ostream &newOut)
unsigned long size
Definition: Log.h:148
static void SummaryAtExit()
static bool rAction
Definition: Log.h:140
static streambuf * bCerr
Definition: Log.h:133
static bool iAction
Definition: Log.h:140
static T Log(const T &x)
Definition: PndCAMath.h:61
static void Fatal(string text, unsigned short int code=0)
static int dRangeS
Definition: Log.h:138
static bool eAction
Definition: Log.h:140
static int faCount
Definition: Log.h:138
static bool asAction
Definition: Log.h:140
basic_ostream< char, char_traits< char > > ostream
static void LogDebug(unsigned short s=0, unsigned short e=65535)
static int eCount
Definition: Log.h:139
static int faRangeE
Definition: Log.h:138
static void RevertOutput()
unsigned long address
Definition: Log.h:147
static void SetOutput(ostream *newOut)
static void LogInfo(bool flag=true)
static int asCount
Definition: Log.h:139
static int dCount
Definition: Log.h:138