bremenpl

Untitled

Jun 26th, 2015
16
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. /*
  2.  * CLogger.cpp
  3.  *
  4.  *  Created on: 25 cze 2015
  5.  *      Author: lukasz
  6.  */
  7.  
  8. #include "CLogger.h"
  9.  
  10. using namespace std;
  11.  
  12. // Global static pointer used to ensure a single instance of the class.
  13. CLogger* CLogger::mp_instance = NULL;
  14.  
  15. /** This function is called to create an instance of the class.
  16.     Calling the constructor publicly is not allowed. The constructor
  17.     is private and is only called by this Instance function.
  18. */
  19. CLogger* CLogger::instance(ElogLevel ll)
  20. {
  21.     //cout << "instance run with logLevel = " << (int)ll << endl;
  22.  
  23.     // Only allow one instance of class to be generated.
  24.     if (!mp_instance)
  25.         mp_instance = new CLogger;
  26.  
  27.     mp_instance->m_logLine.logLevel = ll;
  28.     return mp_instance;
  29. }
  30.  
  31. bool CLogger::startLog(string fileName, bool verbose)
  32. {
  33.     if(remove(fileName.c_str()) != 0)
  34.         perror( "Error deleting file" );
  35.  
  36.     m_logFileStream.open(fileName.c_str(), ios::out | ios::app);
  37.     if (!m_logFileStream.is_open())
  38.     {
  39.         cout << "Could not open log file " << fileName << endl;
  40.         return false;
  41.     }
  42.  
  43.     m_finishLog = false;
  44.     m_verbose = verbose;
  45.     m_logStarted = true;
  46.     return (pthread_create(&(m_thread), NULL, threadHelper, this) == 0);
  47. }
  48.  
  49. void CLogger::push_back(logline_t s)
  50. {
  51.     unique_lock<mutex> ul(m_mutex);
  52.     m_data.emplace_back(move(s));
  53.     m_cv.notify_all();
  54. }
  55.  
  56. CLogger::logline_t CLogger::pop_front()
  57. {
  58.     unique_lock<mutex> ul(m_mutex);
  59.     m_cv.wait(ul, [this]() { return !m_data.empty(); });
  60.  
  61.     logline_t retVal;
  62.     retVal.logString = move(m_data.front().logString);
  63.     retVal.logLevel = move(m_data.front().logLevel);
  64.     m_data.pop_front();
  65.  
  66.     return retVal;
  67. }
  68.  
  69. void CLogger::setLogLevel(ElogLevel ll)
  70. {
  71.     m_userDefinedLogLevel = ll;
  72. }
  73.  
  74. void CLogger::stopLog()
  75. {
  76.     m_finishLog = true;
  77.  
  78.     logline_t temp;
  79.     temp.logLevel = ElogLevel::eDebug;
  80.     temp.logString = "CLogger Stop \n";
  81.     push_back(temp);
  82.  
  83.     pthread_join(m_thread, NULL);
  84. }
  85.  
  86. void CLogger::threadLoop()
  87. {
  88.     logline_t logline;
  89.     const string logLevelsStrings[] = {"eNone", "eError", "eWarning", "eInfo", "eDebug" };
  90.  
  91.     do
  92.     {
  93.         logline = pop_front();
  94.  
  95.         uint32_t pos;
  96.         if((pos = logline.logString.find('\n')) != string::npos)
  97.             logline.logString.erase(pos);
  98.  
  99.         time_t curTime = time(0);
  100.         struct tm* now = localtime(&curTime);
  101.  
  102.         m_logFileStream << "[" << now->tm_hour << ":" << now->tm_min << ":" << now->tm_sec << "]"
  103.                 << "[" << logLevelsStrings[(int)logline.logLevel] << "] "
  104.                 << logline.logString << endl;
  105.  
  106.         if(m_verbose)
  107.         {
  108.             cout << "[" << now->tm_hour << ":" << now->tm_min << ":" << now->tm_sec << "]"
  109.                             << "[" << logLevelsStrings[(int)logline.logLevel] << "] "
  110.                             << logline.logString << endl;
  111.         }
  112.     }
  113.     while(!(m_finishLog && !m_data.size()));
  114. }
RAW Paste Data