在 C++ 中,您可以使用条件语句和宏定义来实现 loge 日志分级。以下是一个简单的示例:
enum class LogLevel {
INFO,
WARNING,
ERROR,
LOGE
};
#include <iostream>
#include <string>
template<typename T>
void log(LogLevel level, const T& message) {
if (level >= LOGE) {
std::cout << "LOGE: " << message << std::endl;
}
}
#define INFO(msg) log(LogLevel::INFO, msg)
#define WARNING(msg) log(LogLevel::WARNING, msg)
#define ERROR(msg) log(LogLevel::ERROR, msg)
#define LOGE(msg) log(LogLevel::LOGE, msg)
int main() {
INFO("This is an info message.");
WARNING("This is a warning message.");
ERROR("This is an error message.");
LOGE("This is a loge message.");
return 0;
}
这个示例中,log
函数模板根据日志级别决定是否输出信息。通过使用宏定义,您可以方便地为不同级别的日志添加前缀。