Advertisement
bremenpl

CLogger.h

Jul 1st, 2015
252
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. #ifndef CLOGGER_H_
  2. #define CLOGGER_H_
  3.  
  4. #include <iostream>
  5. #include <deque>
  6. #include <string>
  7. #include <mutex>
  8. #include <condition_variable>
  9. #include <pthread.h>
  10. #include <ostream>
  11. #include <fstream>
  12. #include <sstream>
  13. #include <ctime>
  14. #include <iomanip>
  15. #include <sys/time.h>
  16.  
  17. #include "CTeebuf.h"
  18.  
  19. using namespace std;
  20.  
  21. struct CLoggerProxy;
  22.  
  23. struct CLoggerHoldLog_t { };
  24. static CLoggerHoldLog_t  CLoggerHold;
  25.  
  26. /*!
  27.  * \brief Singleton class used for logging
  28.  */
  29. class CLogger
  30. {
  31. public:
  32.     /*!
  33.      * \brief Describes the log level of called \ref CLogger object.
  34.      */
  35.     enum class ElogLevel { eNone = 0, eError, eWarning, eInfo, eDebug };
  36.  
  37.     /*!
  38.      * Structure describing a single log item:
  39.      */
  40.     struct logline_t
  41.     {
  42.         stringstream logString; /*!< String line to be saved to a file (and printed to cout). */
  43.         ElogLevel logLevel; /*!< The \ref ElogLevel of this line. */
  44.         timeval currentTime; /*!< time stamp of current log line */
  45.  
  46.         logline_t& operator =(const logline_t& a)
  47.         {
  48.             logString.str(a.logString.str());
  49.             logLevel = a.logLevel;
  50.             currentTime = a.currentTime;
  51.  
  52.             return *this;
  53.         }
  54.     };
  55.  
  56.     static CLogger* internalInstance(ElogLevel lLevel = ElogLevel::eDebug);
  57.     static CLoggerProxy instance(ElogLevel lLevel = ElogLevel::eDebug);
  58.  
  59.     bool startLog(string fileName, bool verbose);
  60.     logline_t pop_front();
  61.     void push_back(logline_t* s);
  62.     void setLogLevel(ElogLevel ll);
  63.     void finaliseLine();
  64.     void stopLog();
  65.  
  66.     /*!
  67.      * Takes the data from the stream and adds it to the current string.
  68.      * @param t stream item
  69.      * @return \ref CLogger object address
  70.      */
  71.     template<typename T>
  72.     CLogger& operator<<(const T& t)
  73.     {
  74.         if ((mp_logLine->logLevel <= m_userDefinedLogLevel) && m_logStarted)
  75.         {
  76.             /*ostringstream stream;
  77.             stream << t;
  78.             mp_logLine->logString += (stream.str() + " "); // add space at the end*/
  79.             mp_logLine->logString << t << " ";
  80.         }
  81.  
  82.         return *this;
  83.     }
  84.  
  85.     CLogger& operator<<(CLoggerHoldLog_t)
  86.     {
  87.         m_holdLog = true;
  88.         return *this;
  89.     }
  90.  
  91. protected:
  92.     virtual void threadLoop();
  93.  
  94. private:
  95.     CLogger() {};                               // Private so that it can  not be called
  96.     CLogger(CLogger const&) {};                 // copy constructor is private
  97.     CLogger& operator= (CLogger const&) {};     // assignment operator is private
  98.  
  99.     /*!< Global static pointer used to ensure a single instance of the class */
  100.     static CLogger* mp_instance;
  101.     bool m_logStarted;
  102.     logline_t* mp_logLine;
  103.     ElogLevel m_userDefinedLogLevel;
  104.     ofstream m_logFileStream;
  105.     bool m_verbose;
  106.     bool m_finishLog;
  107.     bool m_holdLog;
  108.     timeval m_initialTime;
  109.  
  110.     static void * threadHelper(void* handler)
  111.     {
  112.         ((CLogger*)handler)->threadLoop();
  113.         return NULL;
  114.     }
  115.  
  116.     deque<logline_t*> m_data;
  117.     mutex m_mutex;
  118.     condition_variable m_cv;
  119.     pthread_t   m_thread;
  120. };
  121.  
  122. /*!
  123.  * RAII class used for its destructor, to add a log item to the queue
  124.  */
  125. struct CLoggerProxy
  126. {
  127.     CLogger &log;
  128.  
  129.     CLoggerProxy(CLogger &logger) : log(logger) {}
  130.  
  131.     ~CLoggerProxy() { log.finaliseLine(); }
  132.  
  133.     template <class T>
  134.     CLoggerProxy& operator<< (const T &t) const
  135.     {
  136.         log << t;
  137.         return *this;
  138.     }
  139. };
  140.  
  141. #endif /* CLOGGER_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement