EIC Software
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups Pages
FairDbLogEntry.cxx
Go to the documentation of this file. Or view the newest version in sPHENIX GitHub for file FairDbLogEntry.cxx
1 
2 #include <iostream>
3 #include <sstream>
4 
5 #include "TString.h"
6 #include "TSystem.h"
7 #include "TUrl.h"
8 
9 #include "FairDbLogEntry.h"
10 
11 #include "FairDb.h"
12 #include "FairDbMultConnector.h"
13 #include "FairDbOutRowStream.h"
14 #include "FairDbResPtr.h"
15 #include "FairDbWriter.h"
16 #include "FairDbResult.h"
17 #include "FairDbTableProxy.h"
19 #include "FairDbValidityRec.h"
20 
21 
23 
24 
25 
26 #include "FairDbResPtr.tpl"
27 template class FairDbResultPtr<FairDbLogEntry>;
28 
29 #include "FairDbWriter.tpl"
30 template class FairDbWriter<FairDbLogEntry>;
31 
32 
33 FairDbLogEntry::FairDbLogEntry(const string& tableName, /* = "" */
34  const string& reason, /* = "" */
35  Int_t detMask, /* = full mask */
36  Int_t simMask, /* = full mask */
37  FairDb::Task task, /* = 0 */
38  Int_t logSeqNoMin, /* = 0 */
39  Int_t logSeqNoMax, /* = 0 */
40  Int_t logNumSeqNo): /* = 0 */
42  fDbNo(0),
43  fSeqNo(0),
44  fLogTableName(tableName),
45  fLogDetMask(detMask),
46  fLogSimMask(simMask),
47  fLogTask(task),
48  fLogSeqNoMin(logSeqNoMin),
49  fLogSeqNoMax(logSeqNoMax),
50  fLogNumSeqNo(logNumSeqNo),
51  fUpdateTime(),
52  fUserName("unknown user"),
53  fProcessName(gProgName),
54  fHostName(gSystem->HostName()),
55  fServerName(),
56  fReason()
57 {
58 
59  if ( fLogSeqNoMax == 0 ) { fLogSeqNoMax = logSeqNoMin; }
60  if ( fLogNumSeqNo == 0 && logSeqNoMin != 0
62 
63  this->SetServerName();
64  this->SetReason(reason);
65 
66  // Try to get username from the environment.
67  const char* userName = gSystem->Getenv("USER");
68  if ( ! userName ) { userName = gSystem->Getenv("USERNAME"); }
69  if ( ! userName ) { userName = gSystem->Getenv("LOGNAME"); }
70  if ( userName ) { fUserName = userName; }
71 
72 }
73 //.....................................................................
74 
76 {
77 
78 }
79 //.....................................................................
80 
81 std::ostream& operator<<(ostream& s, const FairDbLogEntry& logEntry)
82 {
83 
84  s << "FairDbLogEntry: Table " << logEntry.GetLogTableName();
85  if ( logEntry.GetLogSeqNoMin() == logEntry.GetLogSeqNoMax() ) {
86  s << " SEQNO: " << logEntry.GetLogSeqNoMin();
87  } else
88  s << " SEQNO min: " << logEntry.GetLogSeqNoMin()
89  << " SEQNO max: " << logEntry.GetLogSeqNoMax()
90  << " No. SEQNOs: " << logEntry.GetLogNumSeqNo();
91  s << " DetectorMask: " << logEntry.GetDetectorMask()
92  << " SimMask: " << logEntry.GetSimMask()
93  << " Task: " << logEntry.GetTask()
94  << "\n Updated on " << logEntry.GetUpdateTime().AsString("s")
95  << " by " << logEntry.GetUserName()
96  << " running " << logEntry.GetProcessName()
97  << " on " << logEntry.GetHostName()
98  << " connected to " << logEntry.GetServerName()
99  << "\n Reason for update:-\n " << logEntry.GetReason() << endl;
100 
101  return s;
102 
103 }
104 
106  const FairDbValidityRec* vrec)
107 {
108  rs >> fLogTableName
109  >> fLogSeqNoMin
110  >> fLogSeqNoMax
111  >> fLogNumSeqNo
112  >> fUserName
113  >> fProcessName
114  >> fHostName
115  >> fServerName
116  >> fReason;
118  if ( vrec ) {
120  fLogSimMask = vrec->GetValRange().GetSimMask();
121  fLogTask = vrec->GetTask();
123  }
124 }
125 
126 
127 void FairDbLogEntry::Recreate(const string& tableName, /* = "" */
128  const string& reason, /* = "" */
129  Int_t detMask, /* = full mask */
130  Int_t simMask, /* = full mask */
131  FairDb::Task task, /* = 0 */
132  Int_t logSeqNoMin, /* = 0 */
133  Int_t logSeqNoMax, /* = 0 */
134  Int_t logNumSeqNo) /* = 0 */
135 {
136  if ( fSeqNo > 0
137  && ( tableName == "" || tableName == fLogTableName )
138  && ( detMask == Detector::FullMask() || detMask == fLogDetMask )
139  && ( simMask == SimFlag::FullMask() || simMask == fLogSimMask )
140  && ( task == 0 || task == fLogTask )
141  && ( logSeqNoMin == 0 || logSeqNoMin == fLogSeqNoMin)
142  && ( logSeqNoMax == 0 || logSeqNoMax == fLogSeqNoMax )
143  && ( logNumSeqNo == 0 || logNumSeqNo == fLogNumSeqNo )
144  ) {
145  if ( reason != "" ) { this->SetReason(reason); }
146  return;
147  }
148 
149 
150  fDbNo = 0;
151  fSeqNo =0;
152  fLogTableName = tableName;
153  fLogDetMask = detMask;
154  fLogSimMask = simMask;
155  fLogTask = task;
156  fLogSeqNoMin = logSeqNoMin;
157  fLogSeqNoMax = logSeqNoMax;
158  fLogNumSeqNo = logNumSeqNo;
159  this->SetReason(reason);
160 
161 }
162 
163 
164 void FairDbLogEntry::SetReason(const string& reason)
165 {
166 
167  fReason = reason;
168 
169  // If fReason starts '@' treat remainder as file name
170  // to be read into fReason.
171  if ( fReason.size() && fReason[0] == '@' ) {
172  string fileName(fReason,1);
173  fReason.clear();
174  ifstream reasonFile(fileName.c_str());
175  if ( ! reasonFile.is_open() ) {
176  cout << "Cannot read \"Reason File\" " << fileName << endl;
177  } else {
178  string line;
179  while ( ! reasonFile.eof() ) {
180  getline(reasonFile,line);
181  if ( line.substr(0,11) == "FIXUP-FILE:"
182  || line.substr(0,10) == "BROADCAST:" ) {
183  continue;
184  }
185  if ( fReason.size() ) { fReason += '\n'; }
186  fReason += line;
187  }
188  reasonFile.close();
189  }
190  }
191 }
192 
193 
195 {
196 
198  TUrl url(urlStr.c_str());
199  fServerName = url.GetHost();
200 
201 }
202 //.....................................................................
203 
205  const FairDbValidityRec* /* vrec */) const
206 {
207 
208  ors << fLogTableName
209  << fLogSeqNoMin
210  << fLogSeqNoMax
211  << fLogNumSeqNo
212  << fUserName
213  << fProcessName
214  << fHostName
215  << fServerName
216  << fReason;
217 }
218 
219 //.....................................................................
220 
221 Bool_t FairDbLogEntry::Write(UInt_t dbNo,
222  Int_t logSeqNo) /* =0 */
223 {
224 
225  if ( logSeqNo > 0 ) {
226  if ( fLogNumSeqNo == 0 ) {
227  fLogSeqNoMin = logSeqNo;
228  fLogSeqNoMax = logSeqNo;
229  }
230  if ( logSeqNo < fLogSeqNoMin ) { fLogSeqNoMin = logSeqNo; }
231  if ( logSeqNo > fLogSeqNoMax ) { fLogSeqNoMax = logSeqNo; }
232  ++fLogNumSeqNo;
233  }
234 
235  if ( ! this->HasReason() || fLogNumSeqNo == 0) {
236  cout << "Cannot write LogEntry - no reason and/or no SEQNOs defined "
237  << endl;
238  return kFALSE;
239  }
240 
241  if ( fSeqNo != 0 && dbNo != fDbNo ) {
242  cout << "Cannot write LogEntry - attempting to switch from database"
243  << fDbNo << " to " << dbNo << endl;
244  return kFALSE;
245  }
246 
247  fDbNo = dbNo;
249  .GetTableProxy("DBILOGENTRY",this);
250  bool replace = true;
251 
252  // Allocate SEQNO before first write.
253  if ( fSeqNo == 0 ) {
254  replace = false;
255  int seqNo = tblProxy.GetMultConnector().AllocateSeqNo("DBILOGENTRY",0,fDbNo);
256  if ( seqNo <= 0 ) {
257  cout << "Cannot get sequence number for table DBILOGENTRY" << endl;
258  return kFALSE;
259  }
260  fSeqNo = seqNo;
261  }
262 
263  // Construct a FairDbValidityRec.
265  FairDbValidityRec vrec(vr,fLogTask,-1,0);
266 
267  // Now build and output FairDbSqlValPacket.
268  FairDbSqlValPacket packet;
269  packet.Recreate("DBILOGENTRY",
270  vrec.GetValRange(),
271  -1,
272  vrec.GetTask(),
273  vrec.GetCreationDate());
274  packet.AddDataRow(tblProxy,0,*this);
275  packet.SetSeqNo(fSeqNo);
276  return packet.Store(fDbNo,replace);
277  ;
278 }