#pragma once #pragma warning(disable : 4996) //_CRT_SECURE_NO_WARNINGS #include "Logger.h" #include #include #include #include using namespace std; Logger::Logger(std::string loggerFile) { this->loggerFile = loggerFile; }; void Logger::start() { // ios::app - 追加 // ios::ate - 移动到文件末 // ios::out - 写(默认清空) // ios::trunc - 清空 this->fs.open(this->loggerFile, ios::out | ios::app); }; void Logger::PutLog(std::string module, std::string text) { if (debugFlag) { // Get current system time auto now = std::chrono::system_clock::now(); // Convert system time to time_t (seconds since Jan 1, 1970) std::time_t now_time = std::chrono::system_clock::to_time_t(now); // Convert time_t to struct tm (broken down time) std::tm* now_tm = std::localtime(&now_time); // Format timestamp string manually char timestamp[20]; std::strftime(timestamp, sizeof(timestamp), "%Y-%m-%d %H:%M:%S", now_tm); fs << "[" << timestamp << "]: " << "[" << module << "] " << text << endl; } }; void Logger::end() { fs.close(); };