学习了单例模式实现日志记录器,自己的理解以注释的形式记录一下
class Logger final
{
public:
enum class LogLevel {
Error,
Info,
Debug
};
static Logger& instance();
Logger(const Logger&) = delete;
Logger(Logger&&) = delete;
Logger& operator=(const Logger&) = delete;
Logger& operator=(Logger&&) = delete;
void setLogLevel(LogLevel level);
void log(std::string_view message, LogLevel logLevel);
void log(const std::vector<std::string>& messages,
LogLevel logLevel);
private:
Logger();
~Logger();
std::string_view getLogLevelString(LogLevel level) const;
static const char* const kLogFileName;
std::ofstream mOutputStream;
LogLevel mLogLevel = LogLevel::Error;
};
const char* const Logger::kLogFileName = "log.out";
Logger& Logger::instance()
{
static Logger instance;
return instance;
}
Logger::instance().setLogLevel(Logger::LogLevel::Debug);
Logger::instance().log("test message", Logger::LogLevel::Error);
|