Log.cxx

00001 #include <fstream>
00002 #include "Log.h"
00003 using std::streambuf;
00004 using std::stringstream;
00005 using std::ostream;
00006 using std::cout;
00007 using std::cerr;
00008 using std::endl;
00009 
00010 namespace Photospp
00011 {
00012 
00013 list<Log::Pointer*> *Log::PointerList = NULL;
00014 
00015 streambuf   *Log::bCout=cout.rdbuf(),*Log::bCerr=cerr.rdbuf();
00016 ostream     *Log::out=&cout;
00017 stringstream Log::buf;
00018 int  Log::warnLimit=100;
00019 int  Log::decays[4] = {0};
00020 int  Log::dCount =0,Log::dRangeS =65535,Log::dRangeE =65534;
00021 int  Log::faCount=0,Log::faRangeS=65535,Log::faRangeE=65534;
00022 int  Log::iCount =0,Log::wCount =0,Log::eCount =0,Log::asCount=0, Log::asFailedCount=0;
00023 bool Log::iAction=1,Log::wAction=1,Log::eAction=1,Log::asAction=1,Log::rAction=1;
00024 
00025 void Log::AddDecay(int type)
00026 {
00027         decays[type]++;
00028 }
00029 
00030 ostream& Log::Debug(unsigned short int code, bool count)
00031 {
00032         if(count) ++dCount;
00033         if(code>=dRangeS && code<=dRangeE ) return *out<<"DEBUG("<<code<<") from PHOTOS:"<<endl;
00034         return buf.seekp(0);
00035 }
00036 
00037 
00038 ostream& Log::Info(bool count)
00039 {
00040         if(count) ++iCount;
00041         if(iAction) return *out<<"INFO from PHOTOS:"<<endl;
00042         return buf.seekp(0);
00043 }
00044 
00045 
00046 ostream& Log::Warning(bool count)
00047 {
00048         if(count) ++wCount;
00049         if(warnLimit>0 && wCount>=warnLimit)
00050         {
00051                 if(wAction)
00052                 {
00053                         *out<<"WARNING from PHOTOS:"<<endl<<"Limit reached ("<<warnLimit<<"). Warnings suppressed."<<endl;
00054                         wAction=false;
00055                 }
00056                 return buf.seekp(0);
00057         }
00058         if(wAction && count) return *out<<"WARNING from PHOTOS:"<<endl;
00059         if(wAction)          return *out;
00060         return buf.seekp(0);
00061 }
00062 
00063 
00064 ostream& Log::Error(bool count)
00065 {
00066         if(count) ++eCount;
00067         if(eAction) return *out<<"ERROR from PHOTOS:"<<endl;
00068         buf.seekp(0);
00069         return buf;
00070 }
00071 
00072 void Log::Assert(bool check, char *text)
00073 {
00074         ++asCount;
00075         if(check) return;
00076         ++asFailedCount;
00077         if(text==NULL)  *out<<"ASSERT from PHOTOS:"<<endl<<"Assertion failed. "<<endl;
00078         else *out<<"ASSERT from PHOTOS:"<<endl<<"Assertion failed: "<<text<<endl;
00079         if(asAction) exit(-1);
00080 }
00081 
00082 void Log::Fatal(string text,unsigned short code)
00083 {
00084         ++faCount;
00085         if(text.size()==0) *out<<"FATAL ERROR from PHOTOS:"<<endl<<"Terminated by a call to Log::Exit();"<<endl;
00086         else *out<<"FATAL ERROR from PHOTOS: "<<endl<<text<<endl;
00087         if(code<faRangeS || code>faRangeE) exit(-1);
00088 }
00089 
00090 void Log::RedirectOutput(void (*func)(), ostream& where)
00091 {
00092 
00093         if(!rAction) { func(); return; }
00094         cout.rdbuf(where.rdbuf());
00095         cerr.rdbuf(where.rdbuf());
00096         where<<endl;
00097         func();
00098         cout.rdbuf(bCout);
00099         cerr.rdbuf(bCerr);
00100 }
00101 
00102 void Log::RedirectOutput(ostream& where)
00103 {
00104         if(!rAction) return;
00105         cout.rdbuf(where.rdbuf());
00106         cerr.rdbuf(where.rdbuf());
00107         where<<endl;
00108 }
00109 
00110 void Log::Summary()
00111 {
00112         *out<<"---------------------------- Photos Log Summary ------------------------------"<<endl;
00113         *out<<" Debug:   \t";
00114         if(dRangeS>dRangeE) *out<<"(OFF)";
00115         *out<<"\t\t"<<dCount<<"\t";
00116         if(dRangeS<=dRangeE) *out<<"Debug range: "<<dRangeS<<" - "<<dRangeE;
00117         *out<<endl;
00118         *out<<" Info:    \t";
00119         if(!iAction) *out<<"(OFF)";
00120         *out<<"\t\t"<<iCount<<"\t"<<endl;
00121         *out<<" Warnings:\t";
00122         if(!wAction) if(warnLimit>0 && wCount>warnLimit) *out<<"(SUPP.)"; else *out<<"(OFF)";
00123         *out<<"\t\t"<<wCount<<"\t"<<endl;
00124         *out<<" Errors:  \t";
00125         if(!eAction) *out<<"(OFF)";
00126         *out<<"\t\t"<<eCount<<"\t"<<endl;
00127         if(asCount || !asAction || faRangeS<faRangeE) cout<<"-----------------------------------"<<endl;
00128         if(asCount>0) *out<<" Asserts:\t\t\t"<<asCount<<endl;
00129         if(!asAction) *out<<" Failed asserts ignored:\t"<<asFailedCount<<endl;
00130         if(faRangeS<=faRangeE) *out<<" Fatal errors ignored:  \t"<<faCount<<endl;
00131         cout<<"-----------------------------------"<<endl;
00132         if(decays[3]) cout<<" Normal decays:                        "<<decays[3]<<endl;
00133         if(decays[2]) cout<<" Decays without mother:                "<<decays[2]<<endl;
00134         if(decays[1]) cout<<" Decays without mother & grandmothers: "<<decays[1]<<endl;
00135         if(decays[0]) cout<<" Decayed using Tauola gun:             "<<decays[0]<<endl;
00136         *out<<"------------------------------------------------------------------------------"<<endl;
00137 }
00138 
00139 } // namespace Photospp
Generated on Sun Oct 20 20:23:56 2013 for C++InterfacetoPHOTOS by  doxygen 1.6.3