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
Definition: start.py:1
std::string m_connectionString
Definition: Logger.h:130
list jobName
Initialize all the variables.
EchoedLogStream< edm::LogWarning > logWarning()
Definition: Logger.cc:169
#define conddb_column(...)
Definition: DbCore.h:76
Table(coral::ISchema &schema)
Definition: Logger.cc:26
EchoedLogStream< edm::LogError > logError()
Definition: Logger.cc:165
boost::posix_time::ptime m_endTime
Definition: Logger.h:133
std::stringstream m_log
Definition: Logger.h:135
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
void subscribeCoralMessages(const std::weak_ptr< MsgDispatcher > &dispatcher)
Definition: Logger.cc:64
coral::ISchema & m_schema
Definition: Logger.cc:39
boost::posix_time::ptime m_startTime
Definition: Logger.h:132
std::string get_timestamp_for_filename()
Definition: Logger.cc:104
const coral::AttributeList & get() const
Definition: DbCore.h:201
std::string get_timestamp()
Definition: Logger.cc:100
std::iostream & log(const std::string &tag)
Definition: Logger.cc:150
std::string m_jobName
Definition: Logger.h:129
std::weak_ptr< MsgDispatcher > m_dispatcher
Definition: Logger.h:136
EchoedLogStream< edm::LogDebug_ > logDebug()
Definition: Logger.cc:161
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
std::string tname(const std::string &tableName, const std::string &schemaVersion)
void setDbDestination(const std::string &connectionString)
Definition: Logger.cc:66
static constexpr const char *const COND_WRITER_ROLE
Definition: Auth.h:17
EchoedLogStream< edm::LogInfo > logInfo()
Definition: Logger.cc:157
void end(int retCode)
Definition: Logger.cc:80
void throwException(const std::string &message, const std::string &methodName)
Definition: Exception.cc:12
#define conddb_table(NAME)
Definition: DbCore.h:49