在 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
函數模板根據日志級別決定是否輸出信息。通過使用宏定義,您可以方便地為不同級別的日志添加前綴。