Back to home page

Project CMSSW displayed by LXR

 
 

    


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

0001 #ifndef CondCore_CondDB_Logger_h
0002 #define CondCore_CondDB_Logger_h
0003 //
0004 // Package:     CondDB
0005 // Class  :     O2OLogger
0006 //
0007 /**\class Logger Logger.h CondCore/CondDB/interface/Logger.h
0008    Description: utility for collecting log information and store them into the Condition DB.  
0009 */
0010 //
0011 // Author:      Giacomo Govi
0012 // Created:     Sep 2020
0013 //
0014 #include "FWCore/MessageLogger/interface/MessageLogger.h"
0015 //
0016 #include <sstream>
0017 #include <memory>
0018 #include <boost/date_time/posix_time/posix_time.hpp>
0019 
0020 namespace cond {
0021 
0022   namespace persistency {
0023 
0024     class MsgDispatcher;
0025 
0026     template <typename EdmLogger>
0027     class EchoedLogStream {
0028     public:
0029       EchoedLogStream() = delete;
0030       explicit EchoedLogStream(const std::string& jobName, std::stringstream& buffer)
0031           : m_edmLogger(jobName), m_buffer(&buffer) {}
0032       virtual ~EchoedLogStream() {}
0033       template <typename T>
0034       EchoedLogStream& operator<<(const T& t) {
0035         *m_buffer << t;
0036         m_edmLogger << t;
0037         return *this;
0038       }
0039       EchoedLogStream& operator<<(std::ostream& (*f)(std::ostream&)) {
0040         *m_buffer << f;
0041         m_edmLogger << f;
0042         return *this;
0043       }
0044       EchoedLogStream& operator<<(std::ios_base& (*f)(std::ios_base&)) {
0045         *m_buffer << f;
0046         m_edmLogger << f;
0047         return *this;
0048       }
0049 
0050     private:
0051       EdmLogger m_edmLogger;
0052       std::stringstream* m_buffer;
0053     };
0054 
0055     template <>
0056     class EchoedLogStream<edm::LogDebug_> {
0057     public:
0058       EchoedLogStream() = delete;
0059       explicit EchoedLogStream(const std::string& jobName, std::stringstream& buffer)
0060           : m_edmLogger(jobName, __FILE__, __LINE__), m_buffer(&buffer) {}
0061       virtual ~EchoedLogStream() {}
0062       template <typename T>
0063       EchoedLogStream& operator<<(const T& t) {
0064         *m_buffer << t;
0065         m_edmLogger << t;
0066         return *this;
0067       }
0068       EchoedLogStream& operator<<(std::ostream& (*f)(std::ostream&)) {
0069         *m_buffer << f;
0070         m_edmLogger << f;
0071         return *this;
0072       }
0073       EchoedLogStream& operator<<(std::ios_base& (*f)(std::ios_base&)) {
0074         *m_buffer << f;
0075         m_edmLogger << f;
0076         return *this;
0077       }
0078 
0079     private:
0080       edm::LogDebug_ m_edmLogger;
0081       std::stringstream* m_buffer;
0082     };
0083 
0084     //
0085     class Logger {
0086     public:
0087       // default constructor is suppressed
0088       Logger() = delete;
0089 
0090       // constructor
0091       explicit Logger(const std::string& jobName);
0092 
0093       //
0094       virtual ~Logger();
0095 
0096       //
0097       void subscribeCoralMessages(const std::weak_ptr<MsgDispatcher>& dispatcher);
0098 
0099       //
0100       void setDbDestination(const std::string& connectionString);
0101 
0102       //
0103       void start();
0104 
0105       //
0106       void end(int retCode);
0107 
0108       //
0109       void saveOnDb();
0110 
0111       //
0112       void saveOnFile();
0113 
0114       //
0115       void save();
0116 
0117       //
0118       std::iostream& log(const std::string& tag);
0119 
0120       EchoedLogStream<edm::LogInfo> logInfo();
0121       EchoedLogStream<edm::LogDebug_> logDebug();
0122       EchoedLogStream<edm::LogError> logError();
0123       EchoedLogStream<edm::LogWarning> logWarning();
0124 
0125     private:
0126       void clearBuffer();
0127 
0128     private:
0129       std::string m_jobName;
0130       std::string m_connectionString;
0131       bool m_started;
0132       boost::posix_time::ptime m_startTime;
0133       boost::posix_time::ptime m_endTime;
0134       int m_retCode;
0135       std::stringstream m_log;
0136       std::weak_ptr<MsgDispatcher> m_dispatcher;
0137     };
0138 
0139   }  // namespace persistency
0140 }  // namespace cond
0141 #endif