| 12345678910111213141516171819202122232425262728293031323334353637383940414243444546 |
- #pragma once
- #include <string>
- #include <fstream>
- #include <mutex>
- #include <memory>
- enum class LogLevel {
- LOG_LEVEL_INFO,
- LOG_LEVEL_WARNING,
- LOG_LEVEL_ERROR,
- LOG_LEVEL_DEBUG
- };
- class Logger {
- private:
- static std::unique_ptr<Logger> instance_;
- static std::mutex mutex_;
-
- std::ofstream log_file_;
- std::mutex file_mutex_;
- std::string log_file_path_;
-
- Logger(const std::string& log_file_path);
-
- public:
- static Logger* GetInstance(const std::string& log_file_path = "");
-
- void Log(LogLevel level, const std::string& message);
- void LogInfo(const std::string& message);
- void LogWarning(const std::string& message);
- void LogError(const std::string& message);
- void LogDebug(const std::string& message);
-
- ~Logger();
-
- // 禁用拷贝构造和赋值
- Logger(const Logger&) = delete;
- Logger& operator=(const Logger&) = delete;
- };
- // 便利宏
- #define LOG_INFO(msg) Logger::GetInstance()->LogInfo(msg)
- #define LOG_WARNING(msg) Logger::GetInstance()->LogWarning(msg)
- #define LOG_ERROR(msg) Logger::GetInstance()->LogError(msg)
- #define LOG_DEBUG(msg) Logger::GetInstance()->LogDebug(msg)
|