Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * CLogger.cpp
- *
- * Created on: 25 cze 2015
- * Author: lukasz
- */
- #include "CLogger.h"
- using namespace std;
- // Global static pointer used to ensure a single instance of the class.
- CLogger* CLogger::mp_instance = NULL;
- /** This function is called to create an instance of the class.
- Calling the constructor publicly is not allowed. The constructor
- is private and is only called by this Instance function.
- */
- CLogger* CLogger::internalInstance(ElogLevel ll)
- {
- //cout << "instance run with logLevel = " << (int)ll << endl;
- // Only allow one instance of class to be generated.
- if (!mp_instance)
- mp_instance = new CLogger;
- mp_instance->m_logLine.logLevel = ll;
- return mp_instance;
- }
- CLoggerProxy CLogger::instance(ElogLevel ll)
- {
- return CLoggerProxy(*internalInstance(ll));
- }
- bool CLogger::startLog(string fileName, bool verbose)
- {
- if(remove(fileName.c_str()) != 0)
- perror( "Error deleting file" );
- m_logFileStream.open(fileName.c_str(), ios::out | ios::app);
- if (!m_logFileStream.is_open())
- {
- cout << "Could not open log file " << fileName << endl;
- return false;
- }
- m_finishLog = false;
- m_verbose = verbose;
- m_logStarted = true;
- return (pthread_create(&(m_thread), NULL, threadHelper, this) == 0);
- }
- void CLogger::push_back(logline_t s)
- {
- unique_lock<mutex> ul(m_mutex);
- m_data.emplace_back(move(s));
- m_cv.notify_all();
- }
- CLogger::logline_t CLogger::pop_front()
- {
- unique_lock<mutex> ul(m_mutex);
- m_cv.wait(ul, [this]() { return !m_data.empty(); });
- logline_t retVal;
- retVal.logString = move(m_data.front().logString);
- retVal.logLevel = move(m_data.front().logLevel);
- m_data.pop_front();
- return retVal;
- }
- void CLogger::setLogLevel(ElogLevel ll)
- {
- m_userDefinedLogLevel = ll;
- }
- void CLogger::stopLog()
- {
- m_finishLog = true;
- logline_t temp;
- temp.logLevel = ElogLevel::eDebug;
- temp.logString = "CLogger Stop \n";
- push_back(temp);
- pthread_join(m_thread, NULL);
- }
- void CLogger::finaliseLine()
- {
- push_back(m_logLine);
- m_logLine.logString.clear();
- }
- void CLogger::threadLoop()
- {
- logline_t logline;
- const string logLevelsStrings[] = {"eNone", "eError", "eWarning", "eInfo", "eDebug" };
- do
- {
- logline = pop_front();
- uint32_t pos;
- if((pos = logline.logString.find('\n')) != string::npos)
- logline.logString.erase(pos);
- time_t curTime = time(0);
- struct tm* now = localtime(&curTime);
- m_logFileStream << "[" << now->tm_hour << ":" << now->tm_min << ":" << now->tm_sec << "]"
- << "[" << logLevelsStrings[(int)logline.logLevel] << "] "
- << logline.logString << endl;
- if(m_verbose)
- {
- cout << "[" << now->tm_hour << ":" << now->tm_min << ":" << now->tm_sec << "]"
- << "[" << logLevelsStrings[(int)logline.logLevel] << "] "
- << logline.logString << endl;
- }
- }
- while(!(m_finishLog && !m_data.size()));
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement