中间件底层,websocket

Logger.cpp 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #include "stdafx.h"
  2. #include "Logger.h"
  3. #define FILE_SIZE 1024*1024 // 1m
  4. #include <boost\filesystem.hpp>
  5. Logger::Logger()
  6. {
  7. m_isStop = true;
  8. m_LogThread = nullptr;
  9. m_LogDir = "./log";
  10. m_LogFileName = "log";
  11. m_LogSize = FILE_SIZE * 20;
  12. }
  13. Logger::~Logger()
  14. {
  15. Stop();
  16. }
  17. bool Logger::Init(const std::string& logDir, const std::string& logFileName)
  18. {
  19. m_LogDir = logDir;
  20. m_LogFileName = logFileName;
  21. m_LogSize = FILE_SIZE * 20;
  22. if (!boost::filesystem::exists(m_LogDir)) {
  23. return boost::filesystem::create_directories(m_LogDir);
  24. }
  25. return true;
  26. }
  27. bool Logger::Start()
  28. {
  29. if (m_isStop) {
  30. m_isStop = false;
  31. if (m_LogThread == nullptr) {
  32. m_LogThread = std::make_shared<std::thread>(std::bind(&Logger::__dealLogItem, this));
  33. }
  34. }
  35. return true;
  36. }
  37. bool Logger::Stop()
  38. {
  39. try
  40. {
  41. if (!m_isStop) {
  42. m_isStop = true;
  43. m_LogCond.notify_one();
  44. if (m_LogThread != nullptr && m_LogThread->joinable()) {
  45. m_LogThread->join();
  46. }
  47. }
  48. }
  49. catch (const std::exception& e)
  50. {
  51. std::cout << e.what() << std::endl;
  52. return false;
  53. }
  54. return true;
  55. }
  56. std::string Logger::Log(const char * format, ...)
  57. {
  58. if (m_isStop) return "";
  59. boost::posix_time::ptime pt = boost::posix_time::microsec_clock::local_time();
  60. auto ptStr = boost::posix_time::to_iso_extended_string(pt);
  61. ptStr = std::regex_replace(ptStr, std::regex("T"), " ");
  62. // 解析日志信息参数
  63. char szMsgBuffer[2048];
  64. memset(szMsgBuffer, 0, sizeof(szMsgBuffer));
  65. va_list ap;
  66. va_start(ap, format);
  67. vsnprintf(szMsgBuffer, sizeof(szMsgBuffer), format, ap);
  68. va_end(ap);
  69. std::string str(szMsgBuffer);
  70. __pushLogItem(ptStr, 1, str);
  71. return str;
  72. }
  73. void Logger::__pushLogItem(const std::string & data, const int & level, const std::string & content)
  74. {
  75. std::shared_ptr<LogItem> pLogItem = std::make_shared<LogItem>(data);
  76. if (pLogItem) {
  77. pLogItem->SetItem(level, content);
  78. std::unique_lock<std::mutex>lock(m_LogMut);
  79. m_LogList.emplace_back(pLogItem);
  80. m_LogCond.notify_one();
  81. }
  82. }
  83. void Logger::__dealLogItem()
  84. {
  85. while (!m_isStop)
  86. {
  87. std::unique_lock<std::mutex>lock(m_LogMut);
  88. m_LogCond.wait(lock);
  89. lock.unlock();
  90. if (m_isStop) break;
  91. auto pOutFile = __openFile();
  92. for (; !m_isStop;) {
  93. std::unique_lock<std::mutex>lock1(m_LogMut);
  94. if (m_LogList.empty()) break;
  95. auto pLogItem = std::move(m_LogList.front()) ;
  96. m_LogList.pop_front();
  97. lock1.unlock();
  98. if (pLogItem) {
  99. std::cout << *pLogItem << std::endl;
  100. *pOutFile << *pLogItem << std::endl;
  101. pOutFile->flush();
  102. }
  103. }
  104. pOutFile->close();
  105. }
  106. std::cout << "线程结束" << std::endl;
  107. }
  108. std::shared_ptr<std::ofstream> Logger::__openFile()
  109. {
  110. boost::posix_time::ptime pt = boost::posix_time::microsec_clock::local_time();
  111. auto date = boost::posix_time::to_iso_extended_string(pt);
  112. date = date.substr(0,date.find("T"));
  113. static std::string lastDate = date;
  114. static int index = 1;
  115. std::streampos size = 0;
  116. std::string filePath = "log.txt";
  117. do {
  118. // 第二天,文件索引重置
  119. if (lastDate != date) {
  120. lastDate = date;
  121. index = 1;
  122. }
  123. // 文件路径拼接,创建
  124. filePath = "";
  125. filePath = filePath.append(m_LogDir).append("/").append(date).append("/");
  126. if (!boost::filesystem::exists(filePath)) {
  127. boost::filesystem::create_directories(filePath);
  128. }
  129. filePath = filePath.append(m_LogFileName).append("_").append(std::to_string(index)).append(".txt");
  130. std::ifstream infile(filePath, std::ifstream::in | std::ifstream::binary);
  131. infile.seekg(std::ios::beg, std::ios::end);
  132. size = infile.tellg(); // 字节
  133. infile.close();
  134. if (size > m_LogSize)
  135. ++index;
  136. } while (size > m_LogSize);
  137. std::shared_ptr<std::ofstream> pOutFile = std::make_shared<std::ofstream >();
  138. pOutFile->open(filePath, std::ios::app);
  139. return pOutFile;
  140. }
  141. //Logger Logger::pInstance;
  142. std::shared_ptr<Logger> Logger::pInstance = std::make_shared<Logger>();