Advertisement
bremenpl

Untitled

Jun 26th, 2015
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.87 KB | None | 0 0
  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::internalInstance(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. CLoggerProxy CLogger::instance(ElogLevel ll)
  32. {
  33.     return CLoggerProxy(*internalInstance(ll));
  34. }
  35.  
  36. bool CLogger::startLog(string fileName, bool verbose)
  37. {
  38.     if(remove(fileName.c_str()) != 0)
  39.         perror( "Error deleting file" );
  40.  
  41.     m_logFileStream.open(fileName.c_str(), ios::out | ios::app);
  42.     if (!m_logFileStream.is_open())
  43.     {
  44.         cout << "Could not open log file " << fileName << endl;
  45.         return false;
  46.     }
  47.  
  48.     m_finishLog = false;
  49.     m_verbose = verbose;
  50.     m_logStarted = true;
  51.     return (pthread_create(&(m_thread), NULL, threadHelper, this) == 0);
  52. }
  53.  
  54. void CLogger::push_back(logline_t s)
  55. {
  56.     unique_lock<mutex> ul(m_mutex);
  57.     m_data.emplace_back(move(s));
  58.     m_cv.notify_all();
  59. }
  60.  
  61. CLogger::logline_t CLogger::pop_front()
  62. {
  63.     unique_lock<mutex> ul(m_mutex);
  64.     m_cv.wait(ul, [this]() { return !m_data.empty(); });
  65.  
  66.     logline_t retVal;
  67.     retVal.logString = move(m_data.front().logString);
  68.     retVal.logLevel = move(m_data.front().logLevel);
  69.     m_data.pop_front();
  70.  
  71.     return retVal;
  72. }
  73.  
  74. void CLogger::setLogLevel(ElogLevel ll)
  75. {
  76.     m_userDefinedLogLevel = ll;
  77. }
  78.  
  79. void CLogger::stopLog()
  80. {
  81.     m_finishLog = true;
  82.  
  83.     logline_t temp;
  84.     temp.logLevel = ElogLevel::eDebug;
  85.     temp.logString = "CLogger Stop \n";
  86.     push_back(temp);
  87.  
  88.     pthread_join(m_thread, NULL);
  89. }
  90.  
  91. void CLogger::finaliseLine()
  92. {
  93.     push_back(m_logLine);
  94.     m_logLine.logString.clear();
  95. }
  96.  
  97. void CLogger::threadLoop()
  98. {
  99.     logline_t logline;
  100.     const string logLevelsStrings[] = {"eNone", "eError", "eWarning", "eInfo", "eDebug" };
  101.  
  102.     do
  103.     {
  104.         logline = pop_front();
  105.  
  106.         uint32_t pos;
  107.         if((pos = logline.logString.find('\n')) != string::npos)
  108.             logline.logString.erase(pos);
  109.  
  110.         time_t curTime = time(0);
  111.         struct tm* now = localtime(&curTime);
  112.  
  113.         m_logFileStream << "[" << now->tm_hour << ":" << now->tm_min << ":" << now->tm_sec << "]"
  114.                 << "[" << logLevelsStrings[(int)logline.logLevel] << "] "
  115.                 << logline.logString << endl;
  116.  
  117.         if(m_verbose)
  118.         {
  119.             cout << "[" << now->tm_hour << ":" << now->tm_min << ":" << now->tm_sec << "]"
  120.                             << "[" << logLevelsStrings[(int)logline.logLevel] << "] "
  121.                             << logline.logString << endl;
  122.         }
  123.     }
  124.     while(!(m_finishLog && !m_data.size()));
  125. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement