Back to home page

EIC code displayed by LXR

 
 

    


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  * This file contains class for logging and filtering output.
0006  * This header file also includes a debug macro which
0007  * tracks any possible memory leaks within the program.
0008  *
0009  * @author Tomasz Przedzinski
0010  * @date 14 November 2009
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     /** Shows the summary of all messages. */
0038     static void Summary();
0039 
0040     /** Shows the summary at the end of the program. */
0041     static void SummaryAtExit()              { atexit(Summary);      }
0042 
0043     /** Adds the decay to the counter. The type is:
0044         0 - gun, 1 - no mothers & grandmothers, 2 - no mothers, 3 - ok. */
0045     static void AddDecay(int type);
0046 
0047     /** Four logging entries. Usage:
0048             Log::Info()<<"Logging some info: "<<8<<" > "<<7.9<<endl;
0049         Use Log::Info(false) if You don't want the message to be counted.*/
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     /** Turns off or on particular types of messages
0056         By default, only debugging messages are turned off. */
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     // TEMPORARY
0064     static void LogPhlupa(int from, int to)  { phlupy.ipoinm=from; phlupy.ipoin=to; }
0065 
0066         /** Sets the range of debug codes that will be printed.
0067             By default, the debug messages are turned off. */
0068     static void LogDebug(unsigned short s=0,unsigned short e=65535)         { dRangeS=s; dRangeE=e;   }
0069 
0070     /** Asserts logical value. If the assertion fails, the default message or 'text'
0071             will be printed and the program will terminate.
0072             Program termination can be suppressed by Log::IgnoreFailedAsserts(); */
0073     static void Assert(bool check, char *text=NULL);
0074 
0075     /** Terminates the program with added default message or 'text'.
0076             It can be suppressed by Log::IgnoreFatal(); */
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     /** Redirects output to log. Redirection can be done for a block of code
0081         or for one function only. Redirection can be turned off by using
0082         Log::IgnoreRedirection(); If the target is one of the log streams
0083         (for example): Log::RedirectOutput( someFunction, Log::Info() );
0084         You can turn the function's messages off by turning the apropriate
0085         log entries off. The redirected code will still be executed,
0086         only messages are redirected. */
0087     static void RedirectOutput(void (*func)(), ostream& where=*out);
0088     static void RedirectOutput(ostream& where=*out);
0089     /** WARNING! If You're redirecting more than one function, do not forget
0090         to use RevertOutput() afterwards. */
0091     static void RevertOutput()                      { std::cout.rdbuf(bCout); std::cerr.rdbuf(bCerr); }
0092 
0093     /** Do not exit when Log::Assert() check is false.
0094         The number of failed asserts will be listed in the summary. */
0095     static void IgnoreFailedAssert(bool flag=true)                           { asAction=!flag;        }
0096 
0097     /** Ignores redirections of functions' output.
0098         The function will still be called in a normal way. */
0099     static void IgnoreRedirection(bool flag=true)                            { rAction=!flag;         }
0100 
0101     /** Do not exit when Log::Fatal() with the code within the provided range is called.
0102             The number of ignored fatal errors will be listed in the summary. */
0103     static void IgnoreFatal(unsigned short s=0,unsigned short e=65535) { faRangeS=s; faRangeE=e; }
0104 
0105     /** Change the output of the logged messages.
0106         Log::SetOutput(cerr);                    //changes the output to cerr
0107         Log::SetOutput(new ofstream("log.txt")); //changes the output to a file "log.txt" */
0108     static void SetOutput(ostream *newOut)                                    { out=newOut;           }
0109     static void SetOutput(ostream &newOut)                                    { out=&newOut;          }
0110 
0111     /** Change the limit of warnings that will be displayed. Set to 0 for no limit. */
0112     static void SetWarningLimit(int x)                                        { warnLimit=x;          }
0113 
0114     /**  Warnings on errors from internal part of PHOTOS (originally in F77) */
0115     static void PHOERR(int IMES,const char *TEXT,double DATA);
0116 
0117     /**  Final report of warnings from internal part of PHOTOS (originally in F77) */
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     Memory leak tracking section. Compile with #define _LOG_DEBUG_MODE_ to turn it on.
0131     WARNING! Increases execution time significantly. Usefull only for debug purposes.
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 //_LOG_DEBUG_MODE_
0215 };
0216 
0217 #ifdef _LOG_DEBUG_MODE_
0218 
0219 /**
0220     Redeclare new and delete to use the tracking feature.
0221     To use __FILE__ and __LINE__ macro efficiently this header file
0222     should be included in all separately compiled libraries.
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 //_LOG_DEBUG_MODE_
0241 
0242 } // namespace Photospp
0243 #endif