| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- #include "stdafx.h"
- #include "Logger.h"
- #define FILE_SIZE 1024*1024 // 1m
- #include <boost\filesystem.hpp>
- Logger::Logger()
- {
- m_isStop = true;
- m_LogThread = nullptr;
- m_LogDir = "./log";
- m_LogFileName = "log";
- m_LogSize = FILE_SIZE * 20;
- }
- Logger::~Logger()
- {
- Stop();
- }
- bool Logger::Init(const std::string& logDir, const std::string& logFileName)
- {
- m_LogDir = logDir;
- m_LogFileName = logFileName;
- m_LogSize = FILE_SIZE * 20;
- if (!boost::filesystem::exists(m_LogDir)) {
- return boost::filesystem::create_directories(m_LogDir);
- }
- return true;
- }
- bool Logger::Start()
- {
- if (m_isStop) {
- m_isStop = false;
- if (m_LogThread == nullptr) {
- m_LogThread = std::make_shared<std::thread>(std::bind(&Logger::__dealLogItem, this));
- }
- }
- return true;
- }
- bool Logger::Stop()
- {
- try
- {
- if (!m_isStop) {
- m_isStop = true;
- m_LogCond.notify_one();
- if (m_LogThread != nullptr && m_LogThread->joinable()) {
- m_LogThread->join();
- }
- }
- }
- catch (const std::exception& e)
- {
- std::cout << e.what() << std::endl;
- return false;
- }
- return true;
- }
- std::string Logger::Log(const char * format, ...)
- {
- if (m_isStop) return "";
- boost::posix_time::ptime pt = boost::posix_time::microsec_clock::local_time();
- auto ptStr = boost::posix_time::to_iso_extended_string(pt);
- ptStr = std::regex_replace(ptStr, std::regex("T"), " ");
- // 解析日志信息参数
- char szMsgBuffer[2048];
- memset(szMsgBuffer, 0, sizeof(szMsgBuffer));
- va_list ap;
- va_start(ap, format);
- vsnprintf(szMsgBuffer, sizeof(szMsgBuffer), format, ap);
- va_end(ap);
- std::string str(szMsgBuffer);
- __pushLogItem(ptStr, 1, str);
- return str;
- }
- void Logger::__pushLogItem(const std::string & data, const int & level, const std::string & content)
- {
- std::shared_ptr<LogItem> pLogItem = std::make_shared<LogItem>(data);
- if (pLogItem) {
- pLogItem->SetItem(level, content);
- std::unique_lock<std::mutex>lock(m_LogMut);
- m_LogList.emplace_back(pLogItem);
- m_LogCond.notify_one();
- }
- }
- void Logger::__dealLogItem()
- {
- while (!m_isStop)
- {
- std::unique_lock<std::mutex>lock(m_LogMut);
- m_LogCond.wait(lock);
- lock.unlock();
- if (m_isStop) break;
- auto pOutFile = __openFile();
- for (; !m_isStop;) {
- std::unique_lock<std::mutex>lock1(m_LogMut);
- if (m_LogList.empty()) break;
- auto pLogItem = std::move(m_LogList.front()) ;
- m_LogList.pop_front();
- lock1.unlock();
- if (pLogItem) {
- std::cout << *pLogItem << std::endl;
- *pOutFile << *pLogItem << std::endl;
- pOutFile->flush();
- }
- }
- pOutFile->close();
- }
- std::cout << "线程结束" << std::endl;
- }
- std::shared_ptr<std::ofstream> Logger::__openFile()
- {
- boost::posix_time::ptime pt = boost::posix_time::microsec_clock::local_time();
- auto date = boost::posix_time::to_iso_extended_string(pt);
- date = date.substr(0,date.find("T"));
- static std::string lastDate = date;
- static int index = 1;
- std::streampos size = 0;
- std::string filePath = "log.txt";
- do {
- // 第二天,文件索引重置
- if (lastDate != date) {
- lastDate = date;
- index = 1;
- }
- // 文件路径拼接,创建
- filePath = "";
- filePath = filePath.append(m_LogDir).append("/").append(date).append("/");
-
- if (!boost::filesystem::exists(filePath)) {
- boost::filesystem::create_directories(filePath);
- }
- filePath = filePath.append(m_LogFileName).append("_").append(std::to_string(index)).append(".txt");
- std::ifstream infile(filePath, std::ifstream::in | std::ifstream::binary);
- infile.seekg(std::ios::beg, std::ios::end);
- size = infile.tellg(); // 字节
- infile.close();
- if (size > m_LogSize)
- ++index;
- } while (size > m_LogSize);
-
- std::shared_ptr<std::ofstream> pOutFile = std::make_shared<std::ofstream >();
- pOutFile->open(filePath, std::ios::app);
- return pOutFile;
- }
- //Logger Logger::pInstance;
- std::shared_ptr<Logger> Logger::pInstance = std::make_shared<Logger>();
|