Back to home page

Project CMSSW displayed by LXR

 
 

    


File indexing completed on 2021-02-14 12:50:25

0001 #include "CondCore/CondDB/interface/Logger.h"
0002 #include "CondCore/CondDB/interface/Auth.h"
0003 #include "CondCore/CondDB/interface/Exception.h"
0004 //
0005 #include "DbCore.h"
0006 #include "CoralMsgReporter.h"
0007 #include "RelationalAccess/ITransaction.h"
0008 //
0009 #include "RelationalAccess/ConnectionService.h"
0010 #include "RelationalAccess/ISessionProxy.h"
0011 #include <boost/date_time/posix_time/posix_time_io.hpp>
0012 #include <fstream>
0013 //
0014 namespace cond {
0015 
0016   namespace persistency {
0017 
0018     conddb_table(O2O_RUN) {
0019       conddb_column(JOB_NAME, std::string);
0020       conddb_column(START_TIME, boost::posix_time::ptime);
0021       conddb_column(END_TIME, boost::posix_time::ptime);
0022       conddb_column(STATUS_CODE, int);
0023       conddb_column(LOG, std::string);
0024       class Table {
0025       public:
0026         explicit Table(coral::ISchema& schema) : m_schema(schema) {}
0027         ~Table() {}
0028         void insert(const std::string& jobName,
0029                     const boost::posix_time::ptime& start,
0030                     const boost::posix_time::ptime& end,
0031                     int retCode,
0032                     const std::string& log) {
0033           RowBuffer<JOB_NAME, START_TIME, END_TIME, STATUS_CODE, LOG> dataToInsert(
0034               std::tie(jobName, start, end, retCode, log));
0035           insertInTable(m_schema, tname, dataToInsert.get());
0036         }
0037 
0038       private:
0039         coral::ISchema& m_schema;
0040       };
0041     }
0042 
0043     void Logger::clearBuffer() {
0044       m_log.str("");
0045       m_log.clear();
0046     }
0047 
0048     Logger::Logger(const std::string& jobName)
0049         : m_jobName(jobName),
0050           m_connectionString(""),
0051           m_started(false),
0052           m_startTime(),
0053           m_endTime(),
0054           m_retCode(0),
0055           m_log() {}
0056 
0057     //
0058     Logger::~Logger() {
0059       auto dispatcher = m_dispatcher.lock();
0060       if (dispatcher.get())
0061         dispatcher->unsubscribe();
0062     }
0063 
0064     void Logger::subscribeCoralMessages(const std::weak_ptr<MsgDispatcher>& dispatcher) { m_dispatcher = dispatcher; }
0065 
0066     void Logger::setDbDestination(const std::string& connectionString) { m_connectionString = connectionString; }
0067 
0068     //
0069     void Logger::start() {
0070       if (!m_started) {
0071         if (!m_log.str().empty())
0072           clearBuffer();
0073         m_startTime = boost::posix_time::microsec_clock::universal_time();
0074         m_started = true;
0075         log("START_JOB") << " " << m_jobName;
0076       }
0077     }
0078 
0079     //
0080     void Logger::end(int retCode) {
0081       if (m_started) {
0082         m_endTime = boost::posix_time::microsec_clock::universal_time();
0083         m_started = false;
0084         m_retCode = retCode;
0085         log("END_JOB") << ": return code:" << retCode;
0086         save();
0087         clearBuffer();
0088       }
0089     }
0090 
0091     std::string print_timestamp(const boost::posix_time::ptime& t, const char* format_s = "%Y-%m-%d %H:%M:%S.%f") {
0092       boost::posix_time::time_facet* facet = new boost::posix_time::time_facet();
0093       facet->format(format_s);
0094       std::stringstream timestamp;
0095       timestamp.imbue(std::locale(std::locale::classic(), facet));
0096       timestamp << t;
0097       return timestamp.str();
0098     }
0099 
0100     std::string get_timestamp() {
0101       auto now = boost::posix_time::microsec_clock::universal_time();
0102       return print_timestamp(now);
0103     }
0104     std::string get_timestamp_for_filename() {
0105       auto now = boost::posix_time::microsec_clock::universal_time();
0106       return print_timestamp(now, "%Y-%m-%d_%H-%M-%S");
0107     }
0108 
0109     //
0110     void Logger::saveOnFile() {
0111       if (!m_log.str().empty()) {
0112         std::string fileName(get_timestamp_for_filename() + ".log");
0113         std::ofstream fout(fileName, std::ofstream::app);
0114         fout << m_log.str() << std::endl;
0115         fout.close();
0116       }
0117     }
0118 
0119     //
0120     void Logger::saveOnDb() {
0121       if (!m_log.str().empty()) {
0122         if (m_connectionString.empty()) {
0123           throwException("Connection string for destination database has not been provided.", "Logger::saveOnDb");
0124         }
0125         coral::ConnectionService connServ;
0126         std::unique_ptr<coral::ISessionProxy> coralSession(
0127             connServ.connect(m_connectionString, auth::COND_WRITER_ROLE, coral::Update));
0128         coralSession->transaction().start(false);
0129         try {
0130           O2O_RUN::Table destinationTable(coralSession->nominalSchema());
0131           destinationTable.insert(m_jobName, m_startTime, m_endTime, m_retCode, m_log.str());
0132           coralSession->transaction().commit();
0133         } catch (const std::exception& e) {
0134           coralSession->transaction().rollback();
0135           // dump on file on this circumstance...
0136           logError() << e.what();
0137           saveOnFile();
0138           throwException(std::string("Failure while saving log on database:") + e.what(), "Logger::saveOnDb");
0139         }
0140       }
0141     }
0142 
0143     void Logger::save() {
0144       if (!m_connectionString.empty())
0145         saveOnDb();
0146       else
0147         saveOnFile();
0148     }
0149 
0150     std::iostream& Logger::log(const std::string& tag) {
0151       if (std::size(m_log.str()) != 0)
0152         m_log << std::endl;
0153       m_log << "[" << get_timestamp() << "] " << tag << ": ";
0154       return m_log;
0155     }
0156 
0157     EchoedLogStream<edm::LogInfo> Logger::logInfo() {
0158       log("INFO");
0159       return EchoedLogStream<edm::LogInfo>(m_jobName, m_log);
0160     }
0161     EchoedLogStream<edm::LogDebug_> Logger::logDebug() {
0162       log("DEBUG");
0163       return EchoedLogStream<edm::LogDebug_>(m_jobName, m_log);
0164     }
0165     EchoedLogStream<edm::LogError> Logger::logError() {
0166       log("ERROR");
0167       return EchoedLogStream<edm::LogError>(m_jobName, m_log);
0168     }
0169     EchoedLogStream<edm::LogWarning> Logger::logWarning() {
0170       log("WARNING");
0171       return EchoedLogStream<edm::LogWarning>(m_jobName, m_log);
0172     }
0173 
0174   }  // namespace persistency
0175 }  // namespace cond