bremenpl

Untitled

Jun 26th, 2015
30
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * CLogger.h
  3.  *
  4.  *  Created on: 25 cze 2015
  5.  *      Author: lukasz
  6.  */
  7.  
  8. #ifndef CLOGGER_H_
  9. #define CLOGGER_H_
  10.  
  11. #include <iostream>
  12. #include <deque>
  13. #include <string>
  14. #include <mutex>
  15. #include <condition_variable>
  16. #include <pthread.h>
  17. #include <ostream>
  18. #include <fstream>
  19. #include <sstream>
  20. #include <ctime>
  21.  
  22. using namespace std;
  23.  
  24. struct CLoggerProxy;
  25.  
  26. class CLogger
  27. {
  28. public:
  29.     enum class ElogLevel { eNone = 0, eError, eWarning, eInfo, eDebug };
  30.  
  31.     typedef struct
  32.     {
  33.         string logString;
  34.         ElogLevel logLevel;
  35.     } logline_t;
  36.  
  37.     static CLogger* internalInstance(ElogLevel ll = ElogLevel::eError);
  38.     static CLoggerProxy instance(ElogLevel ll = ElogLevel::eError);
  39.  
  40.     bool startLog(string fileName, bool verbose);
  41.     logline_t pop_front();
  42.     void push_back(logline_t s);
  43.     void setLogLevel(ElogLevel ll);
  44.     void finaliseLine();
  45.     void stopLog();
  46.  
  47.     template<typename T>
  48.     CLogger& operator<<(const T& t)
  49.     {
  50.         if (((int)m_logLine.logLevel <= (int)m_userDefinedLogLevel) && m_logStarted)
  51.         {
  52.             ostringstream stream;
  53.             stream << t;
  54.             m_logLine.logString += stream.str();
  55.  
  56.             /*if (stream.str().find("\n") != string::npos)
  57.             {
  58.                 push_back(m_logLine);
  59.                 m_logLine.logString.clear();
  60.             }*/
  61.         }
  62.  
  63.         return *this;
  64.     }
  65.  
  66. protected:
  67.     virtual void threadLoop();
  68.  
  69. private:
  70.     CLogger() {};                               // Private so that it can  not be called
  71.     CLogger(CLogger const&) {};                 // copy constructor is private
  72.     CLogger& operator= (CLogger const&) {};     // assignment operator is private
  73.  
  74.     static CLogger* mp_instance;
  75.     bool m_logStarted;
  76.     logline_t m_logLine;
  77.     ElogLevel m_userDefinedLogLevel;
  78.     ofstream m_logFileStream;
  79.     bool m_verbose;
  80.     bool m_finishLog;
  81.  
  82.     static void * threadHelper(void* handler)
  83.     {
  84.         ((CLogger*)handler)->threadLoop();
  85.         return NULL;
  86.     }
  87.  
  88.     deque<logline_t> m_data;
  89.     mutex m_mutex;
  90.     condition_variable m_cv;
  91.     pthread_t   m_thread;
  92. };
  93.  
  94. struct CLoggerProxy
  95. {
  96.     CLogger &logger;
  97.  
  98.     CLoggerProxy(CLogger &logger) : logger(logger) {}
  99.  
  100.     ~CLoggerProxy() { logger.finaliseLine(); }
  101.  
  102.     template <class T>
  103.     CLoggerProxy& operator<< (const T &t) const
  104.     {
  105.         logger << t;
  106.         return *this;
  107.     }
  108. };
  109.  
  110. #endif /* CLOGGER_H_ */
RAW Paste Data