Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- * CLogger.h
- *
- * Created on: 25 cze 2015
- * Author: lukasz
- */
- #ifndef CLOGGER_H_
- #define CLOGGER_H_
- #include <iostream>
- #include <deque>
- #include <string>
- #include <mutex>
- #include <condition_variable>
- #include <pthread.h>
- #include <ostream>
- #include <fstream>
- #include <sstream>
- #include <ctime>
- using namespace std;
- class CLogger
- {
- public:
- enum class ElogLevel { eNone = 0, eError, eWarning, eInfo, eDebug };
- typedef struct
- {
- string logString;
- ElogLevel logLevel;
- } logline_t;
- static CLogger* instance(ElogLevel ll = ElogLevel::eError);
- bool startLog(string fileName, bool verbose);
- logline_t pop_front();
- void push_back(logline_t s);
- void setLogLevel(ElogLevel ll);
- void stopLog();
- template<typename T>
- CLogger& operator<<(const T& t)
- {
- if (((int)m_logLine.logLevel <= (int)m_userDefinedLogLevel) && m_logStarted)
- {
- ostringstream stream;
- stream << t;
- m_logLine.logString += stream.str();
- if (stream.str().find("\n") != string::npos)
- {
- push_back(m_logLine);
- m_logLine.logString.clear();
- }
- }
- return *this;
- }
- protected:
- virtual void threadLoop();
- private:
- CLogger() {}; // Private so that it can not be called
- CLogger(CLogger const&) {}; // copy constructor is private
- CLogger& operator= (CLogger const&) {}; // assignment operator is private
- static CLogger* mp_instance;
- bool m_logStarted;
- logline_t m_logLine;
- ElogLevel m_userDefinedLogLevel;
- ofstream m_logFileStream;
- bool m_verbose;
- bool m_finishLog;
- static void * threadHelper(void* handler)
- {
- ((CLogger*)handler)->threadLoop();
- return NULL;
- }
- deque<logline_t> m_data;
- mutex m_mutex;
- condition_variable m_cv;
- pthread_t m_thread;
- };
- #endif /* CLOGGER_H_ */
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement