00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00014
00015 #include <algorithm>
00016 #include <cctype>
00017 #include <iomanip>
00018 #include <sstream>
00019
00020 #include "DatabaseMaintenance/DbmCmdOptions.h"
00021 #include "DatabaseInterface/Dbi.h"
00022 #include "DatabaseInterface/DbiLogEntry.h"
00023 #include "DatabaseInterface/DbiResultPtr.h"
00024 #include "DatabaseInterface/DbiTableProxy.h"
00025 #include "DatabaseInterface/DbiValidityRec.h"
00026 #include "DatabaseMaintenance/DbmLogFile.h"
00027 #include "LeakChecker/Lea.h"
00028 #include "MessageService/MsgService.h"
00029 #include "Validity/VldTimeStamp.h"
00030
00031 ClassImp(DbmLogFile)
00032
00033
00034
00035
00036
00037 CVSID("$Id: DbmLogFile.cxx,v 1.15 2008/06/13 12:40:30 nwest Exp $");
00038
00039
00040
00041
00042
00043
00044
00045 DbmLogFile::DbmLogFile(const string& fileName,UInt_t dbNo) :
00046 fDbNo(dbNo),
00047 fLogFile(0)
00048 {
00049
00050
00051
00052
00053
00054
00055
00056
00057
00058
00059
00060
00061
00062
00063
00064
00065
00066
00067
00068
00069
00070
00071 LEA_CTOR
00072 MSG("Dbm", Msg::kVerbose) << "Creating DbmLogFile" << endl;
00073
00074 if ( fileName == "") return;
00075
00076 fLogFile = new ofstream(fileName.c_str(),ios::app);
00077 if ( ! fLogFile->is_open() ) {
00078 MSG("Dbm",Msg::kError) << "Cannot open " << fileName << endl;
00079 return;
00080 }
00081 }
00082
00083
00084
00085 DbmLogFile::~DbmLogFile() {
00086
00087
00088
00089
00090
00091
00092
00093
00094
00095
00096
00097
00098
00099
00100
00101
00102
00103
00104
00105
00106
00107
00108 LEA_DTOR
00109
00110 MSG("Dbm", Msg::kVerbose) << "Destroying DbmLogFile" << endl;
00111
00112 LogRec(0);
00113 delete fLogFile;
00114 fLogFile = 0;
00115
00116 }
00117
00118
00119
00120 void DbmLogFile::LogCmd(const string& cmd,
00121 const DbmCmdOptions& opts) {
00122
00123
00124
00125
00126
00127
00128
00129
00130
00131
00132
00133
00134
00135
00136
00137
00138
00139
00140
00141
00142
00143
00144 if ( ! IsValid() ) return;
00145
00146 this->LogMsg("",kFALSE);
00147 string msg(cmd);
00148 msg += " ";
00149 msg += opts.GetOpts();
00150
00151
00152 string insert("+\n ");
00153 UInt_t locOpt = 0;
00154 while ( (locOpt = msg.find("--",locOpt)) != string::npos ) {
00155 msg.insert(locOpt,insert);
00156 locOpt += insert.size() + 2;
00157 }
00158 this->LogMsg(msg);
00159 return;
00160
00161
00162 }
00163
00164
00165 void DbmLogFile::LogMsg(const string& msg,
00166 Bool_t addDateStamp) {
00167
00168
00169
00170
00171
00172
00173
00174
00175
00176
00177 if ( ! IsValid() ) return;
00178 if ( addDateStamp ) {
00179 VldTimeStamp now;
00180 (*fLogFile) << Dbi::MakeDateTimeString( now )
00181 << " ";
00182 }
00183 (*fLogFile) << msg << endl;
00184
00185 return;
00186
00187
00188 }
00189
00190
00191 void DbmLogFile::LogRec(const DbiValidityRec* vrec ) {
00192
00193
00194
00195
00196
00197
00198
00199
00200
00201
00202
00203
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 if ( ! IsValid() ) return;
00216 string tableName = vrec ? vrec->GetTableProxy()->GetTableName() : "";
00217 UInt_t seqNo = vrec ? vrec->GetSeqNo() : 0;
00218 LogRec(tableName,seqNo);
00219
00220 }
00221
00222
00223 void DbmLogFile::LogRec(const string tableName, UInt_t seqNo) {
00224
00225
00226
00227
00228
00229
00230
00231
00232
00233
00234
00235
00236
00237
00238
00239
00240
00241
00242
00243
00244
00245
00246
00247 if ( ! IsValid() ) return;
00248
00249
00250 if ( tableName != fLogTableName ) {
00251 if ( fLogDBSrcNos.size() ) {
00252 list<UInt_t>::const_iterator itr = fLogDBSrcNos.begin();
00253 list<UInt_t>::const_iterator itrEnd = fLogDBSrcNos.end();
00254 while ( itr != itrEnd ) {
00255 UInt_t dbSrcNo = *itr;
00256 (*fLogFile) << " "
00257 << fLogTableName << " "
00258 << fLogNumSeqNo[dbSrcNo] << " recs: "
00259 << fLogSeqNoMin[dbSrcNo];
00260 if ( fLogSeqNoMin[dbSrcNo] != fLogSeqNoMax[dbSrcNo]
00261 ) (*fLogFile) << ".." << fLogSeqNoMax[dbSrcNo];
00262 (*fLogFile) << endl;
00263 ++itr;
00264 }
00265 }
00266 if ( tableName == "DBILOGENTRY"
00267 ) (*fLogFile) << "Start of update log entries..." << endl;
00268 else if ( fLogTableName == "DBILOGENTRY"
00269 ) (*fLogFile) << "End of update log entries..." << endl;
00270
00271 fLogTableName = tableName;
00272 fLogDBSrcNos.clear();
00273 fLogNumSeqNo.clear();
00274 fLogSeqNoMin.clear();
00275 fLogSeqNoMax.clear();
00276
00277 }
00278
00279
00280 if ( tableName != "" ) {
00281 UInt_t dbSrcNo = seqNo/(Dbi::kMAXLOCALSEQNO+1);
00282 if ( find(fLogDBSrcNos.begin(), fLogDBSrcNos.end(), dbSrcNo)
00283 == fLogDBSrcNos.end() ) {
00284 fLogDBSrcNos.push_back(dbSrcNo);
00285 fLogSeqNoMin[dbSrcNo] = seqNo;
00286 fLogSeqNoMax[dbSrcNo] = seqNo;
00287 fLogNumSeqNo[dbSrcNo] = 1;
00288 }
00289 else {
00290 if ( fLogSeqNoMin[dbSrcNo] > seqNo ) fLogSeqNoMin[dbSrcNo] = seqNo;
00291 if ( fLogSeqNoMax[dbSrcNo] < seqNo ) fLogSeqNoMax[dbSrcNo] = seqNo;
00292 ++fLogNumSeqNo[dbSrcNo];
00293 }
00294 }
00295
00296
00297
00298 if ( tableName == "DBILOGENTRY" ) {
00299 DbiResultPtr<DbiLogEntry> rp("DBILOGENTRY",seqNo,fDbNo);
00300 const DbiLogEntry* le = rp.GetRow(0);
00301 if ( le ) (*fLogFile) << *le << endl;
00302 }
00303
00304 }
00305
00306
00307
00308
00309
00310
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324
00325
00326
00327
00328
00329
00330
00331
00332
00333
00334
00335
00336