CMS 3D CMS Logo

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