Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #ifndef CLOGGER_H_
- #define CLOGGER_H_
- #include <iostream>
- #include <deque>
- #include <string>
- #include <mutex>
- #include <condition_variable>
- #include <pthread.h>
- #include <ostream>
- #include <fstream>
- #include <sstream>
- #include <ctime>
- #include <iomanip>
- #include <sys/time.h>
- #include "CTeebuf.h"
- using namespace std;
- struct CLoggerProxy;
- struct CLoggerHoldLog_t { };
- static CLoggerHoldLog_t CLoggerHold;
- /*!
- * \brief Singleton class used for logging
- */
- class CLogger
- {
- public:
- /*!
- * \brief Describes the log level of called \ref CLogger object.
- */
- enum class ElogLevel { eNone = 0, eError, eWarning, eInfo, eDebug };
- /*!
- * Structure describing a single log item:
- */
- struct logline_t
- {
- stringstream logString; /*!< String line to be saved to a file (and printed to cout). */
- ElogLevel logLevel; /*!< The \ref ElogLevel of this line. */
- timeval currentTime; /*!< time stamp of current log line */
- logline_t& operator =(const logline_t& a)
- {
- logString.str(a.logString.str());
- logLevel = a.logLevel;
- currentTime = a.currentTime;
- return *this;
- }
- };
- static CLogger* internalInstance(ElogLevel lLevel = ElogLevel::eDebug);
- static CLoggerProxy instance(ElogLevel lLevel = ElogLevel::eDebug);
- bool startLog(string fileName, bool verbose);
- logline_t pop_front();
- void push_back(logline_t* s);
- void setLogLevel(ElogLevel ll);
- void finaliseLine();
- void stopLog();
- /*!
- * Takes the data from the stream and adds it to the current string.
- * @param t stream item
- * @return \ref CLogger object address
- */
- template<typename T>
- CLogger& operator<<(const T& t)
- {
- if ((mp_logLine->logLevel <= m_userDefinedLogLevel) && m_logStarted)
- {
- /*ostringstream stream;
- stream << t;
- mp_logLine->logString += (stream.str() + " "); // add space at the end*/
- mp_logLine->logString << t << " ";
- }
- return *this;
- }
- CLogger& operator<<(CLoggerHoldLog_t)
- {
- m_holdLog = true;
- return *this;
- }
- protected:
- virtual void threadLoop();
- private:
- CLogger() {}; // Private so that it can not be called
- CLogger(CLogger const&) {}; // copy constructor is private
- CLogger& operator= (CLogger const&) {}; // assignment operator is private
- /*!< Global static pointer used to ensure a single instance of the class */
- static CLogger* mp_instance;
- bool m_logStarted;
- logline_t* mp_logLine;
- ElogLevel m_userDefinedLogLevel;
- ofstream m_logFileStream;
- bool m_verbose;
- bool m_finishLog;
- bool m_holdLog;
- timeval m_initialTime;
- static void * threadHelper(void* handler)
- {
- ((CLogger*)handler)->threadLoop();
- return NULL;
- }
- deque<logline_t*> m_data;
- mutex m_mutex;
- condition_variable m_cv;
- pthread_t m_thread;
- };
- /*!
- * RAII class used for its destructor, to add a log item to the queue
- */
- struct CLoggerProxy
- {
- CLogger &log;
- CLoggerProxy(CLogger &logger) : log(logger) {}
- ~CLoggerProxy() { log.finaliseLine(); }
- template <class T>
- CLoggerProxy& operator<< (const T &t) const
- {
- log << t;
- return *this;
- }
- };
- #endif /* CLOGGER_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement