Main Page   Namespace List   Class Hierarchy   Alphabetical List   Compound List   File List   Compound Members   File Members  

/Go4Log/TGo4Log.cxx

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

Generated on Tue Nov 8 10:56:04 2005 for Go4-v2.10-5 by doxygen1.2.15