Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include "CLogger.h"
- using namespace std;
- 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.
- * @param lLevel Log level for current object
- */
- CLogger* CLogger::internalInstance(ElogLevel lLevel)
- {
- // Only allow one instance of class to be generated.
- if (!mp_instance)
- mp_instance = new CLogger;
- mp_instance->mp_logLine = new logline_t;
- gettimeofday(&mp_instance->mp_logLine->currentTime, NULL);
- mp_instance->mp_logLine->logLevel = lLevel;
- return mp_instance;
- }
- /*!
- * This method is called in order to use the methods
- * within the objects.
- * @param lLevel Log level for current object
- */
- CLoggerProxy CLogger::instance(ElogLevel lLevel)
- {
- return CLoggerProxy(*internalInstance(lLevel));
- }
- /*!
- * \brief Starts the logging system.
- *
- * This method creates and opens the log file,
- * then opens it and creates the threadloop for messages deque.
- * @param fileName desired log file path,
- * @param verbose when set true, logging will also be printed to standard output.
- */
- 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;
- gettimeofday(&m_initialTime, NULL);
- return (pthread_create(&(m_thread), NULL, threadHelper, this) == 0);
- }
- /*!
- * \brief puts a \ref logline_t object at the end of the queue
- * @param s object to be added to queue
- */
- void CLogger::push_back(logline_t* s)
- {
- unique_lock<mutex> ul(m_mutex);
- m_data.emplace_back(move(s));
- m_cv.notify_all();
- }
- /*!
- * \brief takes a \ref logline_t object from the beggining of the queue
- * @return first \ref logline_t object
- */
- CLogger::logline_t CLogger::pop_front()
- {
- unique_lock<mutex> ul(m_mutex);
- m_cv.wait(ul, [this]() { return !m_data.empty(); });
- logline_t retVal = move(*m_data.front());
- delete m_data.front();
- m_data.pop_front();
- return retVal;
- }
- /*!
- * \brief Sets the log level for the whole \ref CLogger object.
- * If \ref m_logLine is equal or higher than set level, log
- * is going to be printed.
- * @param lLevel desired user define log level.
- */
- void CLogger::setLogLevel(ElogLevel lLevel)
- {
- m_userDefinedLogLevel = lLevel;
- }
- /*!
- * \brief Stops the logging system.
- * Last final logline is being added and then the logging thread
- * is being closed.
- */
- void CLogger::stopLog()
- {
- m_finishLog = true;
- instance(ElogLevel::eNone).log << "CLogger Stop";
- pthread_join(m_thread, NULL);
- }
- /*!
- * This function should be run in the \ref CLoggerProxy destructor.
- * is pushes the gathered stream to the queue.
- */
- void CLogger::finaliseLine()
- {
- if ((mp_logLine->logString.gcount() > 0) && !m_holdLog)
- push_back(mp_logLine);
- else if ((mp_logLine->logString.gcount() > 0) && m_holdLog)
- m_holdLog = false;
- else
- delete mp_instance->mp_logLine;
- }
- /*!
- * The loop running in a separate thread. It take items of the
- * log deque object (if there are any) and saves them to a file.
- */
- void CLogger::threadLoop()
- {
- logline_t logline;
- const string logLevelsStrings[] = {"eNone", "eError", "eWarning", "eInfo", "eDebug" };
- COteestream tee;
- tee.add(m_logFileStream);
- if (m_verbose)
- tee.add(cout);
- int secs = 0;
- int h = 0;
- int m = 0;
- int s = 0;
- do
- {
- logline = pop_front(); // waits here for new lines
- secs = logline.currentTime.tv_sec - m_initialTime.tv_sec;
- h = secs / 3600;
- m = ( secs % 3600 ) / 60;
- s = ( secs % 3600 ) % 60;
- tee << "["
- << setw(2) << setfill('0') << h
- << ":"
- << setw(2) << setfill('0') << m
- << ":"
- << setw(2) << setfill('0') << s
- << "."
- << setw(6) << setfill('0') << logline.currentTime.tv_usec
- << "]"
- << "["
- << logLevelsStrings[(int)logline.logLevel]
- << "] "
- << logline.logString << "\n" << flush;
- }
- while(!(m_finishLog && m_data.empty()));
- m_logFileStream.close();
- }
Advertisement
Add Comment
Please, Sign In to add comment