中间件底层,websocket

Logger.h 1.8KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  1. #pragma once
  2. #include <string>
  3. #include <list>
  4. #include <memory>
  5. #include <condition_variable>
  6. #include <thread>
  7. #include <fstream>
  8. #include <boost\date_time\posix_time\posix_time.hpp>
  9. #include <regex>
  10. #define LOG_DEBUG Logger::GetInstance()->Log
  11. class Logger
  12. {
  13. public:
  14. Logger();
  15. ~Logger();
  16. bool Init(const std::string& logDir, const std::string& logFileName);
  17. bool Start();
  18. bool Stop();
  19. std::string Log(const char* format, ...);
  20. //static Logger* GetInstance() { return &pInstance; }
  21. static std::shared_ptr<Logger> GetInstance() { return pInstance; }
  22. private:
  23. Logger(const Logger&) = default;
  24. Logger& operator=(const Logger&) = default;
  25. void __pushLogItem(const std::string& data, const int& level, const std::string& content);
  26. void __dealLogItem();
  27. std::shared_ptr<std::ofstream> __openFile();
  28. private:
  29. typedef struct tagLogItem {
  30. std::string Date;
  31. std::string Content;
  32. int Level;
  33. tagLogItem(const std::string& date) {
  34. Date = date;
  35. Content = "";
  36. Level = 0;
  37. }
  38. void SetItem(const int& Level, const std::string& Content) {
  39. this->Level = Level;
  40. this->Content = Content;
  41. }
  42. std::string string() {
  43. std::string str;
  44. str.append(this->Date).append(" [");
  45. str.append(this->Level == 0 ? "调试" : "信息").append("] ");
  46. str.append(this->Content).append("\n");
  47. return str;
  48. }
  49. friend std::ostream& operator<< (std::ostream& os, const tagLogItem&a) {
  50. return os << a.Date << " [" << (a.Level == 0 ? "调试" : "信息") << "] " << a.Content << std::endl;
  51. }
  52. }LogItem;
  53. std::list<std::shared_ptr<LogItem>> m_LogList;
  54. std::mutex m_LogMut;
  55. std::condition_variable m_LogCond;
  56. bool m_isStop;
  57. std::shared_ptr<std::thread> m_LogThread;
  58. int m_LogSize; // 文件大小,单位M
  59. std::string m_LogDir; // 日志文件夹
  60. std::string m_LogFileName;// 日志文件名
  61. //static Logger pInstance;
  62. static std::shared_ptr<Logger> pInstance;
  63. };