TDbiLogEntry.cxx

Go to the documentation of this file.
00001 // $Id: TDbiLogEntry.cxx,v 1.1 2011/01/18 05:49:20 finch Exp $
00002 
00003 #include <iostream>
00004 #include <sstream>
00005 
00006 #include "TString.h"
00007 #include "TSystem.h"
00008 #include "TUrl.h"
00009 
00010 #include "TDbi.hxx"
00011 #include "TDbiCascader.hxx"
00012 #include "TDbiLogEntry.hxx"
00013 #include "TDbiOutRowStream.hxx"
00014 #include "TDbiResultSetHandle.hxx"
00015 #include "TDbiInRowStream.hxx"
00016 #include "TDbiTableProxy.hxx"
00017 #include "TDbiDatabaseManager.hxx"
00018 #include "TDbiValidityRec.hxx"
00019 #include <TSK_DBI_Log.hxx>
00020 #include <MsgFormat.h>
00021 using std::endl;
00022 
00023 
00024 ClassImp(TDbiLogEntry)
00025 
00026 
00027 //   Definition of static data members
00028 //   *********************************
00029 
00030 
00031 //  Instantiate associated Result Pointer and writer classes.
00032 //  ********************************************************
00033 
00034 #include "TDbiResultSetHandle.tpl"
00035 template class  TDbiResultSetHandle<TDbiLogEntry>;
00036 
00037 #include "TDbiWriter.tpl"
00038 template class  TDbiWriter<TDbiLogEntry>;
00039 
00040 //    Definition of all member functions (static or otherwise)
00041 //    *******************************************************
00042 //
00043 //    -  ordered: ctors, dtor, operators then in alphabetical order.
00044 
00045 //.....................................................................
00046 
00047 TDbiLogEntry::TDbiLogEntry(const string& tableName, /* = "" */
00048                          const string& reason,    /* = "" */
00049                          Int_t detMask,           /* = full mask */
00050                          Int_t simMask,           /* = full mask */
00051                          TDbi::Task task,          /* = 0  */
00052                          Int_t logSeqNoMin,       /* = 0  */
00053                          Int_t logSeqNoMax,       /* = 0  */
00054                          Int_t logNumSeqNo):      /* = 0  */
00055 fDbNo(0),
00056 fSeqNo(0),
00057 fLogTableName(tableName),
00058 fLogDetMask(detMask),
00059 fLogSimMask(simMask),
00060 fLogTask(task),
00061 fLogSeqNoMin(logSeqNoMin),
00062 fLogSeqNoMax(logSeqNoMax),
00063 fLogNumSeqNo(logNumSeqNo),
00064 fUserName("unknown user"),
00065 fProcessName(gProgName),
00066 fHostName(gSystem->HostName())
00067 {
00068 //
00069 //
00070 //  Purpose:  Constructor
00071 
00072 
00073   if ( fLogSeqNoMax == 0 ) fLogSeqNoMax = logSeqNoMin;
00074   if ( fLogNumSeqNo == 0 && logSeqNoMin != 0
00075        ) fLogNumSeqNo = fLogSeqNoMax - fLogSeqNoMin +1;
00076 
00077   this->SetServerName();
00078   this->SetReason(reason);
00079 
00080   // Try to get username from the environment.
00081   const char* userName =  gSystem->Getenv("USER");
00082   if ( ! userName ) userName =  gSystem->Getenv("USERNAME");
00083   if ( ! userName ) userName =  gSystem->Getenv("LOGNAME");
00084   if ( userName ) fUserName = userName;
00085 
00086 }
00087 //.....................................................................
00088 
00089 TDbiLogEntry::~TDbiLogEntry() {
00090 //
00091 //
00092 //  Purpose:  Destructor
00093 
00094 
00095 }
00096 //.....................................................................
00097 
00098 std::ostream& operator<<(ostream& s, const TDbiLogEntry& logEntry) {
00099 //
00100 //
00101 //  Purpose:  Output Log Entry to message stream.
00102 //
00103 //  Arguments:
00104 //    s            in    Message stream
00105 //    logEntry        in    Configuration set to be output
00106 //
00107 //  Return:        Message stream
00108 //
00109 //  Contact:   N. West
00110 //
00111 //  Specification:-
00112 //  =============
00113 //
00114 //  o Output configuration set to message stream.
00115 
00116 //  Program Notes:-
00117 //  =============
00118 
00119 //  None.
00120 
00121   s << "TDbiLogEntry: Table  " << logEntry.GetLogTableName();
00122   if ( logEntry.GetLogSeqNoMin() ==  logEntry.GetLogSeqNoMax() )
00123     s << " SEQNO: " << logEntry.GetLogSeqNoMin();
00124   else
00125      s << " SEQNO min: " << logEntry.GetLogSeqNoMin()
00126        << " SEQNO max: " << logEntry.GetLogSeqNoMax()
00127        << " No. SEQNOs: " << logEntry.GetLogNumSeqNo();
00128   s << " DetectorMask: " << logEntry.GetDetectorMask()
00129     << " SimMask: " << logEntry.GetSimMask()
00130     << " Task: " << logEntry.GetTask()
00131     << "\n Updated on " << logEntry.GetUpdateTime().AsString("s")
00132     << " by " << logEntry.GetUserName()
00133     << " running " << logEntry.GetProcessName()
00134     << " on " << logEntry.GetHostName()
00135     << " connected to " << logEntry.GetServerName()
00136     << "\n Reason for update:-\n " << logEntry.GetReason() << endl;
00137 
00138   return s;
00139 
00140 }
00141 
00142 //.....................................................................
00143 
00144 void TDbiLogEntry::Fill(TDbiInRowStream& rs,
00145                        const TDbiValidityRec* vrec) {
00146 //
00147 //
00148 //  Purpose:  Fill oject from Result Set
00149 
00150   rs >> fLogTableName
00151      >> fLogSeqNoMin
00152      >> fLogSeqNoMax
00153      >> fLogNumSeqNo
00154      >> fUserName
00155      >> fProcessName
00156      >> fHostName
00157      >> fServerName
00158      >> fReason;
00159   fLogDetMask = fLogSimMask = fLogTask = 0;
00160   if ( vrec ) {
00161     fLogDetMask = vrec->GetVldRange().GetDetectorMask();
00162     fLogSimMask = vrec->GetVldRange().GetSimMask();
00163     fLogTask    = vrec->GetTask();
00164     fUpdateTime = vrec->GetVldRange().GetTimeStart();
00165   }
00166 }
00167 
00168 //.....................................................................
00169 
00170 void TDbiLogEntry::Recreate(const string& tableName, /* = "" */
00171                            const string& reason,    /* = "" */
00172                            Int_t detMask,           /* = full mask */
00173                            Int_t simMask,           /* = full mask */
00174                            TDbi::Task task,          /* = 0  */
00175                            Int_t logSeqNoMin,       /* = 0  */
00176                            Int_t logSeqNoMax,       /* = 0  */
00177                            Int_t logNumSeqNo)       /* = 0  */
00178 {
00179 //
00180 //
00181 //  Purpose:  Recreate, unless new state consistent with old.
00182 
00183   if (    fSeqNo > 0
00184        && ( tableName    == ""                       || tableName   == fLogTableName )
00185        && ( detMask      == DbiDetector::FullMask()     || detMask     == fLogDetMask )
00186        && ( simMask      == DbiSimFlag::FullMask()      || simMask     == fLogSimMask )
00187        && ( task         == 0                        || task        == fLogTask )
00188        && ( logSeqNoMin  == 0                        || logSeqNoMin ==  fLogSeqNoMin)
00189        && ( logSeqNoMax  == 0                        || logSeqNoMax == fLogSeqNoMax )
00190        && ( logNumSeqNo  == 0                        || logNumSeqNo == fLogNumSeqNo )
00191           ) {
00192     if ( reason != "" )  this->SetReason(reason);
00193     return;
00194   }
00195 
00196 
00197   fDbNo = 0;
00198   fSeqNo =0;
00199   fLogTableName = tableName;
00200   fLogDetMask = detMask;
00201   fLogSimMask = simMask;
00202   fLogTask = task;
00203   fLogSeqNoMin = logSeqNoMin;
00204   fLogSeqNoMax = logSeqNoMax;
00205   fLogNumSeqNo = logNumSeqNo;
00206   this->SetReason(reason);
00207 
00208 }
00209 
00210 //.....................................................................
00211 
00212 void TDbiLogEntry::SetReason(const string& reason) {
00213 //
00214 //
00215 //  Purpose: Set reason.
00216 //           String can  be "@file-containing-reason" in which case
00217 //           contents of file is used to define the reason excluding
00218 //           any line begining:-
00219 //
00220 //               FIXUP-FILE:
00221 //           or  BROADCAST:
00222 //
00223 //           This allows DMauto fix-up files to provide log entries as well
00224 //           as updates.
00225 
00226   fReason = reason;
00227 
00228   // If fReason starts '@' treat remainder as file name
00229   // to be read into fReason.
00230   if ( fReason.size() && fReason[0] == '@' ) {
00231     string fileName(fReason,1);
00232     fReason.clear();
00233     ifstream reasonFile(fileName.c_str());
00234     if ( ! reasonFile.is_open() ) {
00235       SK_DBI_Severe( "Cannot read \"Reason File\" " << fileName << "  ");
00236     }
00237     else {
00238       string line;
00239       while ( ! reasonFile.eof() ) {
00240         getline(reasonFile,line);
00241         if (    line.substr(0,11) == "FIXUP-FILE:"
00242              || line.substr(0,10) == "BROADCAST:" ) {
00243           continue;
00244         }
00245         if ( fReason.size() ) fReason += '\n';
00246         fReason += line;
00247       }
00248       reasonFile.close();
00249     }
00250   }
00251 }
00252 
00253 //.....................................................................
00254 
00255 void TDbiLogEntry::SetServerName() {
00256 //
00257 //
00258 //  Purpose: Set DB Server name from cascade number.
00259 
00260   string urlStr = TDbiDatabaseManager::Instance().GetCascader().GetURL(fDbNo);
00261   TUrl url(urlStr.c_str());
00262   fServerName = url.GetHost();
00263 
00264 }
00265 //.....................................................................
00266 
00267 void TDbiLogEntry::Store(TDbiOutRowStream& ors,
00268                          const TDbiValidityRec* /* vrec */) const {
00269 //
00270 //
00271 //  Purpose:  Stream object to output row stream
00272 
00273  ors << fLogTableName
00274      << fLogSeqNoMin
00275      << fLogSeqNoMax
00276      << fLogNumSeqNo
00277      << fUserName
00278      << fProcessName
00279      << fHostName
00280      << fServerName
00281      << fReason;
00282 }
00283 
00284 //.....................................................................
00285 
00286 Bool_t TDbiLogEntry::Write(UInt_t dbNo,
00287                           Int_t logSeqNo)     /* =0 */ {
00288 //
00289 //
00290 //  Purpose: Write, or rewrite this entry, adding logSeqNo to range of
00291 //           update SEQNOs if non-zero.
00292 
00293   if ( logSeqNo > 0 ) {
00294     if ( fLogNumSeqNo == 0 ) {
00295       fLogSeqNoMin = logSeqNo;
00296       fLogSeqNoMax = logSeqNo;
00297     }
00298     if ( logSeqNo < fLogSeqNoMin ) fLogSeqNoMin = logSeqNo;
00299     if ( logSeqNo > fLogSeqNoMax ) fLogSeqNoMax = logSeqNo;
00300     ++fLogNumSeqNo;
00301   }
00302 
00303   if ( ! this->HasReason() || fLogNumSeqNo == 0) {
00304     SK_DBI_Severe( "Cannot write LogEntry - no reason and/or no SEQNOs defined "
00305                            << "  ");
00306     return kFALSE;
00307   }
00308 
00309   if ( fSeqNo != 0 && dbNo != fDbNo ) {
00310     SK_DBI_Severe( "Cannot write LogEntry - attempting to switch from database"
00311                            << fDbNo << " to " << dbNo << "  ");
00312     return kFALSE;
00313   }
00314 
00315   fDbNo = dbNo;
00316   TDbiTableProxy& tblProxy = TDbiDatabaseManager::Instance()
00317                             .GetTableProxy("DBILOGENTRY",this);
00318   bool replace = true;
00319 
00320   // Allocate SEQNO before first write.
00321   if ( fSeqNo == 0 ) {
00322     replace = false;
00323     int seqNo = tblProxy.GetCascader().AllocateSeqNo("DBILOGENTRY",0,fDbNo);
00324     if ( seqNo <= 0 ) {
00325          SK_DBI_Severe(  "Cannot get sequence number for table DBILOGENTRY" << "  ");
00326       return kFALSE;
00327     }
00328     fSeqNo = seqNo;
00329   }
00330 
00331   // Construct a TDbiValidityRec.
00332   TVldRange     vr(fLogDetMask,fLogSimMask,fUpdateTime,fUpdateTime,"TDbiLogEntry");
00333   TDbiValidityRec vrec(vr,fLogTask,-1,0);
00334 
00335   // Now build and output TDbiSqlValPacket.
00336   TDbiSqlValPacket packet;
00337   packet.Recreate("DBILOGENTRY",
00338                   vrec.GetVldRange(),
00339                   -1,
00340                   vrec.GetTask(),
00341                   vrec.GetCreationDate());
00342   packet.AddDataRow(tblProxy,0,*this);
00343   packet.SetSeqNo(fSeqNo);
00344   return packet.Store(fDbNo,replace);
00345 ;
00346 }
00347 /*    Template for New Member Function
00348 
00349 //.....................................................................
00350 
00351 TDbiLogEntry:: {
00352 //
00353 //
00354 //  Purpose:
00355 //
00356 //  Arguments:
00357 //    xxxxxxxxx    in    yyyyyy
00358 //
00359 //  Return:
00360 //
00361 //  Contact:   N. West
00362 //
00363 //  Specification:-
00364 //  =============
00365 //
00366 //  o
00367 
00368 //  Program Notes:-
00369 //  =============
00370 
00371 //  None.
00372 
00373 
00374 }
00375 
00376 */
00377 
00378 

Generated on 11 Aug 2013 for SKDatabase by  doxygen 1.6.1