CMS 3D CMS Logo

Logger.cc
Go to the documentation of this file.
4 //
5 #include "DbCore.h"
6 #include "CoralMsgReporter.h"
7 #include "RelationalAccess/ITransaction.h"
8 //
9 #include "RelationalAccess/ConnectionService.h"
10 #include "RelationalAccess/ISessionProxy.h"
11 #include <boost/date_time/posix_time/posix_time_io.hpp>
12 #include <fstream>
13 //
14 namespace cond {
15 
16  namespace persistency {
17 
18  conddb_table(O2O_RUN) {
20  conddb_column(START_TIME, boost::posix_time::ptime);
21  conddb_column(END_TIME, boost::posix_time::ptime);
24  class Table {
25  public:
26  explicit Table(coral::ISchema& schema) : m_schema(schema) {}
27  ~Table() {}
28  void insert(const std::string& jobName,
29  const boost::posix_time::ptime& start,
30  const boost::posix_time::ptime& end,
31  int retCode,
32  const std::string& log) {
34  std::tie(jobName, start, end, retCode, log));
35  insertInTable(m_schema, tname, dataToInsert.get());
36  }
37 
38  private:
39  coral::ISchema& m_schema;
40  };
41  }
42 
44  m_log.str("");
45  m_log.clear();
46  }
47 
49  : m_jobName(jobName),
50  m_connectionString(""),
51  m_started(false),
52  m_startTime(),
53  m_endTime(),
54  m_retCode(0),
55  m_log() {}
56 
57  //
59  auto dispatcher = m_dispatcher.lock();
60  if (dispatcher.get())
61  dispatcher->unsubscribe();
62  }
63 
64  void Logger::subscribeCoralMessages(const std::weak_ptr<MsgDispatcher>& dispatcher) { m_dispatcher = dispatcher; }
65 
67 
68  //
69  void Logger::start() {
70  if (!m_started) {
71  if (!m_log.str().empty())
72  clearBuffer();
73  m_startTime = boost::posix_time::microsec_clock::universal_time();
74  m_started = true;
75  log("START_JOB") << " " << m_jobName;
76  }
77  }
78 
79  //
80  void Logger::end(int retCode) {
81  if (m_started) {
82  m_endTime = boost::posix_time::microsec_clock::universal_time();
83  m_started = false;
84  m_retCode = retCode;
85  log("END_JOB") << ": return code:" << retCode;
86  save();
87  clearBuffer();
88  }
89  }
90 
91  std::string print_timestamp(const boost::posix_time::ptime& t, const char* format_s = "%Y-%m-%d %H:%M:%S.%f") {
92  boost::posix_time::time_facet* facet = new boost::posix_time::time_facet();
93  facet->format(format_s);
94  std::stringstream timestamp;
95  timestamp.imbue(std::locale(std::locale::classic(), facet));
96  timestamp << t;
97  return timestamp.str();
98  }
99 
101  auto now = boost::posix_time::microsec_clock::universal_time();
102  return print_timestamp(now);
103  }
105  auto now = boost::posix_time::microsec_clock::universal_time();
106  return print_timestamp(now, "%Y-%m-%d_%H-%M-%S");
107  }
108 
109  //
111  if (!m_log.str().empty()) {
113  std::ofstream fout(fileName, std::ofstream::app);
114  fout << m_log.str() << std::endl;
115  fout.close();
116  }
117  }
118 
119  //
121  if (!m_log.str().empty()) {
122  if (m_connectionString.empty()) {
123  throwException("Connection string for destination database has not been provided.", "Logger::saveOnDb");
124  }
125  coral::ConnectionService connServ;
126  std::unique_ptr<coral::ISessionProxy> coralSession(
127  connServ.connect(m_connectionString, auth::COND_WRITER_ROLE, coral::Update));
128  coralSession->transaction().start(false);
129  try {
130  O2O_RUN::Table destinationTable(coralSession->nominalSchema());
131  destinationTable.insert(m_jobName, m_startTime, m_endTime, m_retCode, m_log.str());
132  coralSession->transaction().commit();
133  } catch (const std::exception& e) {
134  coralSession->transaction().rollback();
135  // dump on file on this circumstance...
136  logError() << e.what();
137  saveOnFile();
138  throwException(std::string("Failure while saving log on database:") + e.what(), "Logger::saveOnDb");
139  }
140  }
141  }
142 
143  void Logger::save() {
144  if (!m_connectionString.empty())
145  saveOnDb();
146  else
147  saveOnFile();
148  }
149 
150  std::iostream& Logger::log(const std::string& tag) {
151  if (std::size(m_log.str()) != 0)
152  m_log << std::endl;
153  m_log << "[" << get_timestamp() << "] " << tag << ": ";
154  return m_log;
155  }
156 
158  log("INFO");
160  }
162  log("DEBUG");
164  }
166  log("ERROR");
168  }
170  log("WARNING");
172  }
173 
174  } // namespace persistency
175 } // namespace cond
cond::persistency::Logger::m_jobName
std::string m_jobName
Definition: Logger.h:129
start
Definition: start.py:1
cond::persistency::O2O_RUN::END_TIME
Definition: Logger.cc:21
cond::persistency::Logger::log
std::iostream & log(const std::string &tag)
Definition: Logger.cc:150
funct::false
false
Definition: Factorize.h:29
Exception.h
submitPVValidationJobs.now
now
Definition: submitPVValidationJobs.py:639
cond::persistency::Logger::setDbDestination
void setDbDestination(const std::string &connectionString)
Definition: Logger.cc:66
cond::persistency::O2O_RUN::Table
Definition: Logger.cc:24
cond::persistency::Logger::Logger
Logger()=delete
cond::persistency::Logger::end
void end(int retCode)
Definition: Logger.cc:80
cond::persistency::get_timestamp
std::string get_timestamp()
Definition: Logger.cc:100
MillePedeFileConverter_cfg.fileName
fileName
Definition: MillePedeFileConverter_cfg.py:32
cond::persistency::Logger::logInfo
EchoedLogStream< edm::LogInfo > logInfo()
Definition: Logger.cc:157
cond::persistency::O2O_RUN::LOG
Definition: Logger.cc:23
cond::persistency::Logger::m_startTime
boost::posix_time::ptime m_startTime
Definition: Logger.h:132
cond::persistency::O2O_RUN::STATUS_CODE
Definition: Logger.cc:22
conddb_column
#define conddb_column(...)
Definition: DbCore.h:76
cond::persistency::Logger::save
void save()
Definition: Logger.cc:143
cond::auth::COND_WRITER_ROLE
static constexpr const char *const COND_WRITER_ROLE
Definition: Auth.h:17
cond::persistency::Logger::clearBuffer
void clearBuffer()
Definition: Logger.cc:43
cond::persistency::EchoedLogStream< edm::LogDebug_ >
Definition: Logger.h:56
cond::persistency::Logger::m_started
bool m_started
Definition: Logger.h:131
cond::timestamp
Definition: Time.h:19
GlobalPosition_Frontier_DevDB_cff.tag
tag
Definition: GlobalPosition_Frontier_DevDB_cff.py:11
submitPVValidationJobs.jobName
list jobName
Initialize all the variables.
Definition: submitPVValidationJobs.py:657
cond::persistency::O2O_RUN::Table::Table
Table(coral::ISchema &schema)
Definition: Logger.cc:26
mps_fire.end
end
Definition: mps_fire.py:242
cond::persistency::Logger::start
void start()
Definition: Logger.cc:69
cond::persistency::Logger::logDebug
EchoedLogStream< edm::LogDebug_ > logDebug()
Definition: Logger.cc:161
cond::persistency::Logger::saveOnFile
void saveOnFile()
Definition: Logger.cc:110
AlCaHLTBitMon_QueryRunRegistry.string
string
Definition: AlCaHLTBitMon_QueryRunRegistry.py:256
cond::persistency::O2O_RUN::Table::m_schema
coral::ISchema & m_schema
Definition: Logger.cc:39
cond
Definition: plugin.cc:23
cppFunctionSkipper.exception
exception
Definition: cppFunctionSkipper.py:10
cond::persistency::Logger::logWarning
EchoedLogStream< edm::LogWarning > logWarning()
Definition: Logger.cc:169
CoralMsgReporter.h
cond::persistency::Logger::~Logger
virtual ~Logger()
Definition: Logger.cc:58
groupFilesInBlocks.fout
fout
Definition: groupFilesInBlocks.py:162
DbCore.h
cond::persistency::O2O_RUN::Table::insert
void insert(const std::string &jobName, const boost::posix_time::ptime &start, const boost::posix_time::ptime &end, int retCode, const std::string &log)
Definition: Logger.cc:28
tname
std::string tname(const std::string &tableName, const std::string &schemaVersion)
Definition: CredentialStore.cc:150
conddb_table
#define conddb_table(NAME)
Definition: DbCore.h:49
cond::persistency::RowBuffer
Definition: DbCore.h:175
cond::persistency::Logger::saveOnDb
void saveOnDb()
Definition: Logger.cc:120
cond::persistency::RowBuffer::get
const coral::AttributeList & get() const
Definition: DbCore.h:201
cond::persistency::get_timestamp_for_filename
std::string get_timestamp_for_filename()
Definition: Logger.cc:104
cond::persistency::O2O_RUN::START_TIME
Definition: Logger.cc:20
cond::persistency::Logger::m_endTime
boost::posix_time::ptime m_endTime
Definition: Logger.h:133
cond::persistency::print_timestamp
std::string print_timestamp(const boost::posix_time::ptime &t, const char *format_s="%Y-%m-%d %H:%M:%S.%f")
Definition: Logger.cc:91
dqm-mbProfile.log
log
Definition: dqm-mbProfile.py:17
cond::persistency::throwException
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:12
cond::persistency::Logger::m_log
std::stringstream m_log
Definition: Logger.h:135
Auth.h
cond::persistency::Logger::logError
EchoedLogStream< edm::LogError > logError()
Definition: Logger.cc:165
cond::persistency::Logger::m_connectionString
std::string m_connectionString
Definition: Logger.h:130
cond::persistency::Logger::m_dispatcher
std::weak_ptr< MsgDispatcher > m_dispatcher
Definition: Logger.h:136
cond::persistency::Logger::m_retCode
int m_retCode
Definition: Logger.h:134
l1RCTOmdsFedVectorProducer_cfi.connectionString
connectionString
Definition: l1RCTOmdsFedVectorProducer_cfi.py:4
cond::persistency::O2O_RUN::JOB_NAME
Definition: Logger.cc:19
Logger.h
submitPVValidationJobs.t
string t
Definition: submitPVValidationJobs.py:644
cond::persistency::O2O_RUN::Table::~Table
~Table()
Definition: Logger.cc:27
cond::persistency::Logger::subscribeCoralMessages
void subscribeCoralMessages(const std::weak_ptr< MsgDispatcher > &dispatcher)
Definition: Logger.cc:64
findQualityFiles.size
size
Write out results.
Definition: findQualityFiles.py:443
MillePedeFileConverter_cfg.e
e
Definition: MillePedeFileConverter_cfg.py:37
cond::persistency::EchoedLogStream
Definition: Logger.h:27