00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016 #include "TGo4Log.h"
00017
00018 #include "Riostream.h"
00019 #include "TDatime.h"
00020 #include "TMutex.h"
00021 #include "TSystem.h"
00022 #include "snprintf.h"
00023
00024 #include "TGo4LockGuard.h"
00025
00026 const char* TGo4Log::fgcLEFT = "GO4-";
00027
00028 const char* TGo4Log::fgcRIGHT = " ";
00029 const char* TGo4Log::fgcDEBUG = "d";
00030 const char* TGo4Log::fgcINFO = "*";
00031 const char* TGo4Log::fgcWARN = "#";
00032 const char* TGo4Log::fgcERR = "!";
00033 const char* TGo4Log::fgcDEFAULTLOG = "go4logfile.txt";
00034
00035 char TGo4Log::fgcMessagetext[__MESSAGETEXTLENGTH__];
00036 Int_t TGo4Log::fgiIgnoreLevel=1;
00037 Bool_t TGo4Log::fgbOutputEnabled=kTRUE;
00038 Bool_t TGo4Log::fgbLogfileEnabled=kTRUE;
00039 Bool_t TGo4Log::fgbAutoMode=kFALSE;
00040 void* TGo4Log::fgxLogfile=0;
00041 TMutex* TGo4Log::fgxMutex=0;
00042 TGo4Log* TGo4Log::fgxInstance=0;
00043
00044 TString TGo4Log::fgxLogName=TGo4Log::fgcDEFAULTLOG;
00045
00046 TGo4Log::TGo4Log()
00047 {
00048
00049 if(fgxMutex==0)
00050 {
00051 fgxMutex= new TMutex(kTRUE);
00052 }
00053 OpenLogfile(fgcDEFAULTLOG,"--- This is the default Go4 Message logfile ---");
00054 LogfileEnable(kFALSE);
00055 }
00056
00057 TGo4Log::~TGo4Log()
00058 {
00059 CloseLogfile();
00060 }
00061
00062 TGo4Log *TGo4Log::Instance()
00063 {
00064 if(fgxInstance == 0)
00065 fgxInstance = new TGo4Log();
00066
00067 return fgxInstance;
00068 }
00069
00070 const char* TGo4Log::Message(Int_t prio, const char* text,...)
00071 {
00072 Instance();
00073 TGo4LockGuard(fxMutex);
00074 if(prio>-1 && prio<fgiIgnoreLevel) return 0;
00075 Text_t txtbuf[fguMESLEN];
00076 va_list args;
00077 va_start(args, text);
00078 vsnprintf(txtbuf, fguMESLEN, text, args);
00079 va_end(args);
00080 const Text_t* prefix;
00081 switch(prio)
00082 {
00083 case -1:
00084
00085 prefix=fgcINFO;
00086 break;
00087 case 0:
00088 prefix=fgcDEBUG;
00089 break;
00090 case 1:
00091 prefix=fgcINFO;
00092 break;
00093 case 2:
00094 prefix=fgcWARN;
00095 break;
00096 case 3:
00097 prefix=fgcERR;
00098 break;
00099 default:
00100 prefix=fgcINFO;
00101 break;
00102 }
00103
00104 if(fgbLogfileEnabled)
00105 {
00106
00107 snprintf(fgcMessagetext,fguMESLEN,"%s %s",prefix,txtbuf);
00108 WriteLogfile(fgcMessagetext);
00109 } else {}
00110
00111
00112 snprintf(fgcMessagetext,fguMESLEN, "%s%s> %s %s",
00113 fgcLEFT, prefix, txtbuf,fgcRIGHT);
00114 if(fgbOutputEnabled)
00115 {
00116 cout << fgcMessagetext << endl;
00117 } else {}
00118
00119 return fgcMessagetext;
00120 }
00121
00122 void TGo4Log::Debug(const char* text,...)
00123 {
00124 if(fgiIgnoreLevel>0) return;
00125 Instance();
00126 TGo4LockGuard(fxMutex);
00127 Text_t txtbuf[fguMESLEN];
00128 va_list args;
00129 va_start(args, text);
00130 vsnprintf(txtbuf, fguMESLEN, text, args);
00131 va_end(args);
00132 Message(0,txtbuf);
00133 }
00134
00135 void TGo4Log::Info(const char* text,...)
00136 {
00137 if(fgiIgnoreLevel>1) return;
00138 Instance();
00139 TGo4LockGuard(fxMutex);
00140 Text_t txtbuf[fguMESLEN];
00141 va_list args;
00142 va_start(args, text);
00143 vsnprintf(txtbuf, fguMESLEN, text, args);
00144 va_end(args);
00145 Message(1,txtbuf);
00146 }
00147
00148 void TGo4Log::Warn(const char* text,...)
00149 {
00150 if(fgiIgnoreLevel>2) return;
00151 Instance();
00152 TGo4LockGuard(fxMutex);
00153 Text_t txtbuf[fguMESLEN];
00154 va_list args;
00155 va_start(args, text);
00156 vsnprintf(txtbuf, fguMESLEN, text, args);
00157 va_end(args);
00158 Message(2,txtbuf);
00159 }
00160
00161 void TGo4Log::Error(const char* text,...)
00162 {
00163 Instance();
00164 TGo4LockGuard(fxMutex);
00165 Text_t txtbuf[fguMESLEN];
00166 va_list args;
00167 va_start(args, text);
00168 vsnprintf(txtbuf, fguMESLEN, text, args);
00169 va_end(args);
00170 Message(3,txtbuf);
00171 }
00172
00173
00174 void TGo4Log::SetIgnoreLevel(Int_t level)
00175 {
00176 TGo4LockGuard(fxMutex);
00177 fgiIgnoreLevel=level;
00178
00179 }
00180
00181 Int_t TGo4Log::GetIgnoreLevel()
00182 {
00183 return fgiIgnoreLevel;
00184 }
00185
00186 const char* TGo4Log::GetLogname()
00187 {
00188 return fgxLogName.Data();
00189 }
00190
00191
00192 void TGo4Log::OutputEnable(Bool_t on)
00193 {
00194 TGo4LockGuard(fxMutex);
00195 fgbOutputEnabled=on;
00196
00197 }
00198
00199 Bool_t TGo4Log::IsOutputEnabled()
00200 {
00201 return fgbOutputEnabled;
00202 }
00203
00204 void TGo4Log::LogfileEnable(Bool_t on)
00205 {
00206 TGo4LockGuard(fxMutex);
00207 fgbLogfileEnabled=on;
00208 }
00209
00210 Bool_t TGo4Log::IsLogfileEnabled()
00211 {
00212 return fgbLogfileEnabled;
00213 }
00214
00215 void TGo4Log::AutoEnable(Bool_t on)
00216 {
00217 TGo4LockGuard(fxMutex);
00218 fgbAutoMode=on;
00219 }
00220
00221 Bool_t TGo4Log::IsAutoEnabled()
00222 {
00223 return fgbAutoMode;
00224 }
00225
00226
00227 void TGo4Log::OpenLogfile(const char* name,
00228 const char* headercomment,
00229 Bool_t appendmode)
00230 {
00231 TGo4LockGuard(fxMutex);
00232 try
00233 {
00234 CloseLogfile();
00235 Text_t txtbuf[fguMESLEN];
00236 if(name==0)
00237 {
00238
00239 Int_t pid=gSystem->GetPid();
00240 snprintf(txtbuf,fguMESLEN,"go4log-%d.txt",pid);
00241 }
00242 else
00243 {
00244 snprintf(txtbuf,fguMESLEN,"%s",name);
00245 }
00246
00247 if(appendmode)
00248 fgxLogfile = new std::ofstream(txtbuf,ios::app);
00249 else
00250 fgxLogfile = new std::ofstream(txtbuf);
00251 if(((std::ofstream*)fgxLogfile)->fail())
00252 {
00253 LogfileEnable(kFALSE);
00254 cerr <<"TGo4Log::OpenLogfile() - Error opening logfile "<<name << endl;
00255 }
00256 else
00257 {
00258 fgxLogName=txtbuf;
00259 }
00260
00261 if(headercomment) WriteLogfile(headercomment,kFALSE);
00262 }
00263
00264 catch(std::exception& ex)
00265 {
00266 cerr <<"standard exception "<<ex.what()<<"in TGo4Log::OpenLogfile" << endl;
00267 }
00268 catch(...)
00269 {
00270 cerr <<"!!! Unexpected exception in TGo4Log::OpenLogfile !!!" << endl;
00271 }
00272
00273
00274
00275
00276 }
00277
00278 void TGo4Log::WriteLogfile(const char* text, Bool_t withtime)
00279 {
00280 TGo4LockGuard(fxMutex);
00281 if(text==0) return;
00282 if((fgxLogfile!=0) && fgbLogfileEnabled)
00283 {
00284 try
00285 {
00286 if(withtime)
00287 {
00288 Text_t buffer[fguMESLEN];
00289 TDatime now;
00290 snprintf(buffer,fguMESLEN,"%s: %s",
00291 now.AsSQLString(),
00292 text);
00293 *((std::ofstream*)fgxLogfile) << buffer << endl;
00294 }
00295 else
00296 {
00297 *((std::ofstream*)fgxLogfile) << text << endl;
00298 }
00299 }
00300 catch(std::exception& ex)
00301 {
00302 cerr <<"standard exception "<<ex.what()<<"in TGo4Log::WriteLogfile" << endl;
00303 }
00304 catch(...)
00305 {
00306 cerr <<"!!! Unexpected exception in TGo4Log::WriteLogfile !!!" << endl;
00307 }
00308
00309
00310 }
00311
00312 }
00313
00314
00315
00316 void TGo4Log::CloseLogfile()
00317 {
00318 TGo4LockGuard(fxMutex);
00319 if(fgxLogfile!=0)
00320 {
00321 try
00322 {
00323 delete (std::ofstream*)fgxLogfile;
00324 fgxLogfile=0;
00325 }
00326 catch(std::exception& ex)
00327 {
00328 cerr <<"standard exception "<<ex.what()<<"in TGo4Log::CloseLogfile" << endl;
00329 }
00330 catch(...)
00331 {
00332 cerr <<"!!! Unexpected exception in TGo4Log::CloseLogfile !!!" << endl;
00333 }
00334 }
00335
00336 }
00337
00338