我一直在研究c ++中一个相当基本的日志记录类。现在,我需要将类移到主文件之外,但是我不知道将什么放入头文件中。我大部分的困惑在于我的日志记录级别的枚举。这是我的代码:
#include <iostream>
class Log {
public:
enum Level {
LevelError, LevelWarning, LevelInfo
};
private:
Level LogLevel = LevelInfo;
public:
void set_level(Level level) {
LogLevel = level;
}
void Error(const char* message) {
if (LogLevel >= LevelError) {
std::cout << "[ERROR]: " << message << std::endl;
}
}
void Warn(const char* message) {
if (LogLevel >= LevelWarning) {
std::cout << "[WARNING]: " << message << std::endl;
}
}
void Info(const char* message) {
if (LogLevel >= LevelInfo) {
std::cout << "[INFO]: " << message << std::endl;
}
}
};
非常感谢PS反馈我如何改进这门糟糕的课。
实际上,随您所愿,这似乎还不错。您可以将整个内容原样放入Log.h
文件,或将其拆分为Log.h/Log.cpp
,其中.h包含类定义,.cpp文件包含四个类方法。