| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #pragma once
- #include <string>
- #include <list>
- #include <memory>
- #include <condition_variable>
- #include <thread>
- #include <fstream>
- #include <boost\date_time\posix_time\posix_time.hpp>
- #include <regex>
- #define LOG_DEBUG Logger::GetInstance()->Log
- class Logger
- {
- public:
- Logger();
- ~Logger();
- bool Init(const std::string& logDir, const std::string& logFileName);
- bool Start();
- bool Stop();
- std::string Log(const char* format, ...);
- //static Logger* GetInstance() { return &pInstance; }
- static std::shared_ptr<Logger> GetInstance() { return pInstance; }
- private:
- Logger(const Logger&) = default;
- Logger& operator=(const Logger&) = default;
- void __pushLogItem(const std::string& data, const int& level, const std::string& content);
- void __dealLogItem();
- std::shared_ptr<std::ofstream> __openFile();
- private:
- typedef struct tagLogItem {
- std::string Date;
- std::string Content;
- int Level;
- tagLogItem(const std::string& date) {
- Date = date;
- Content = "";
- Level = 0;
- }
- void SetItem(const int& Level, const std::string& Content) {
- this->Level = Level;
- this->Content = Content;
- }
- std::string string() {
- std::string str;
- str.append(this->Date).append(" [");
- str.append(this->Level == 0 ? "调试" : "信息").append("] ");
- str.append(this->Content).append("\n");
- return str;
- }
- friend std::ostream& operator<< (std::ostream& os, const tagLogItem&a) {
- return os << a.Date << " [" << (a.Level == 0 ? "调试" : "信息") << "] " << a.Content << std::endl;
- }
- }LogItem;
- std::list<std::shared_ptr<LogItem>> m_LogList;
- std::mutex m_LogMut;
- std::condition_variable m_LogCond;
- bool m_isStop;
- std::shared_ptr<std::thread> m_LogThread;
- int m_LogSize; // 文件大小,单位M
- std::string m_LogDir; // 日志文件夹
- std::string m_LogFileName;// 日志文件名
- //static Logger pInstance;
- static std::shared_ptr<Logger> pInstance;
- };
|