File indexing completed on 2026-09-27 09:20:26
0001 #ifndef __PHOTOS_LOG_CLASS_HEADER__
0002 #define __PHOTOS_LOG_CLASS_HEADER__
0003
0004
0005
0006
0007
0008
0009
0010
0011
0012
0013 #include <iostream>
0014 #include <string>
0015 #include <sstream>
0016 #include <stdlib.h>
0017 #include <list>
0018 #include "Photos.h"
0019
0020 using std::stringstream;
0021 using std::string;
0022 using std::streambuf;
0023 using std::ostream;
0024 using std::list;
0025 using std::cout;
0026 using std::endl;
0027
0028 namespace Photospp
0029 {
0030
0031 extern void (*PHOERR)(int,const char*,double);
0032 extern void (*PHOREP)();
0033
0034 class Log
0035 {
0036 public:
0037
0038 static void Summary();
0039
0040
0041 static void SummaryAtExit() { atexit(Summary); }
0042
0043
0044
0045 static void AddDecay(int type);
0046
0047
0048
0049
0050 static ostream& Debug(unsigned short int code=0, bool count=true);
0051 static ostream& Info(bool count=true);
0052 static ostream& Warning(bool count=true);
0053 static ostream& Error(bool count=true);
0054
0055
0056
0057 static void LogInfo (bool flag=true) { iAction=flag; }
0058 static void LogWarning(bool flag=true) { wAction=flag; }
0059 static void LogError (bool flag=true) { eAction=flag; }
0060
0061 static void LogAll (bool flag=true) { iAction=wAction=eAction=flag; dRangeS=0; dRangeE=65535; }
0062
0063
0064 static void LogPhlupa(int from, int to) { phlupy.ipoinm=from; phlupy.ipoin=to; }
0065
0066
0067
0068 static void LogDebug(unsigned short s=0,unsigned short e=65535) { dRangeS=s; dRangeE=e; }
0069
0070
0071
0072
0073 static void Assert(bool check, char *text=NULL);
0074
0075
0076
0077 static void Fatal(string text, unsigned short int code=0);
0078 static void Fatal(unsigned short int code=0) { Fatal(NULL,code); }
0079
0080
0081
0082
0083
0084
0085
0086
0087 static void RedirectOutput(void (*func)(), ostream& where=*out);
0088 static void RedirectOutput(ostream& where=*out);
0089
0090
0091 static void RevertOutput() { std::cout.rdbuf(bCout); std::cerr.rdbuf(bCerr); }
0092
0093
0094
0095 static void IgnoreFailedAssert(bool flag=true) { asAction=!flag; }
0096
0097
0098
0099 static void IgnoreRedirection(bool flag=true) { rAction=!flag; }
0100
0101
0102
0103 static void IgnoreFatal(unsigned short s=0,unsigned short e=65535) { faRangeS=s; faRangeE=e; }
0104
0105
0106
0107
0108 static void SetOutput(ostream *newOut) { out=newOut; }
0109 static void SetOutput(ostream &newOut) { out=&newOut; }
0110
0111
0112 static void SetWarningLimit(int x) { warnLimit=x; }
0113
0114
0115 static void PHOERR(int IMES,const char *TEXT,double DATA);
0116
0117
0118 static void PHOREP();
0119
0120 protected:
0121 static streambuf *bCout,*bCerr;
0122 static ostream *out;
0123 static stringstream buf;
0124 static int warnLimit;
0125 static int decays[4];
0126 static int dCount,dRangeS,dRangeE,faCount,faRangeS,faRangeE;
0127 static int iCount, wCount, eCount, asCount, asFailedCount;
0128 static bool iAction,wAction,eAction,asAction,rAction;
0129
0130
0131
0132
0133 protected:
0134 typedef struct
0135 {
0136 unsigned long address;
0137 unsigned long size;
0138 char file[64];
0139 unsigned long line;
0140 } Pointer;
0141 static list<Pointer*> *PointerList;
0142 public:
0143 #ifdef _LOG_DEBUG_MODE_
0144 static void NewPointer(unsigned long address, unsigned long size, const char *file, unsigned long line)
0145 {
0146 if(!PointerList)
0147 {
0148 PointerList = new list<Pointer *>();
0149 atexit(PrintAllocatedPointers);
0150 }
0151 Pointer *info = new Pointer();
0152 info->address = address;
0153 info->size = size;
0154 info->line = line;
0155 strncpy(info->file, file, 63);
0156 PointerList->push_front(info);
0157 }
0158 static void DeletePointer(unsigned long address)
0159 {
0160 if(!PointerList) return;
0161 for(list<Pointer*>::iterator i = PointerList->begin(); i!=PointerList->end(); i++)
0162 {
0163 if((*i)->address == address)
0164 {
0165 PointerList->remove((*i));
0166 break;
0167 }
0168 }
0169 }
0170 static bool PointerCompare(Pointer *one, Pointer *two)
0171 {
0172 int eq = strcmp(one->file,two->file);
0173 if(eq<0) return true;
0174 else if(eq>0) return false;
0175 return (one->line <= two->line);
0176 }
0177 static void PrintAllocatedPointers()
0178 {
0179 if(!PointerList) return;
0180 int pointers=0,buf=0;
0181 unsigned long total=0;
0182 char *lastS=" ";
0183 unsigned int lastL=0;
0184 if(PointerList->size()==0)
0185 {
0186 cout<<"----------------------------UNFREED MEMORY POINTERS----------------------------\n";
0187 cout<<" ... NONE ...\n";
0188 cout<<"-------------------------------------------------------------------------------\n";
0189 return;
0190 }
0191 PointerList->sort(PointerCompare);
0192 cout<<"---------------------------UNFREED MEMORY POINTERS---------------------------\n";
0193 for(list<Pointer*>::iterator i = PointerList->begin(); i!=PointerList->end(); i++)
0194 {
0195 total+=(*i)->size;
0196 ++pointers;
0197 if(strcmp(lastS,(*i)->file)==0)
0198 {
0199 if(lastL==(*i)->line)
0200 {
0201 printf("%56s%10lub (%lu)\n"," ",(*i)->size,(*i)->address);
0202 continue;
0203 }
0204 }
0205 lastS=(*i)->file;
0206 lastL=(*i)->line;
0207 printf("%s%n:",(*i)->file,&buf);
0208 printf("%-*lu%10lub (%lu)\n",55-buf,(*i)->line,(*i)->size,(*i)->address);
0209 }
0210 cout<<endl<<total<<"\tbytes"<<endl;
0211 cout<<pointers<<"\tpointers"<<endl;
0212 cout<<"-------------------------------------------------------------------------------\n";
0213 };
0214 #endif
0215 };
0216
0217 #ifdef _LOG_DEBUG_MODE_
0218
0219
0220
0221
0222
0223
0224
0225 inline void* operator new(long unsigned int size, const char *filename, int line)
0226 {
0227 void *ptr = (void *)malloc(size);
0228 Photos::Log::NewPointer((unsigned long)ptr, size, filename, line);
0229 return(ptr);
0230 }
0231
0232 inline void operator delete(void *p)
0233 {
0234 Photos::Log::DeletePointer((unsigned long)p);
0235 free(p);
0236 }
0237
0238 #define new new(__FILE__, __LINE__)
0239
0240 #endif
0241
0242 }
0243 #endif