一个带有颜色的日志组件头文件|Log.hpp|可以直接使用

发布于:2024-05-17 ⋅ 阅读:(136) ⋅ 点赞:(0)

一个带有颜色的日志组件头文件


日志等级设置

一共设置了五个日志等级,分别是

  • INFO
  • DEBUG
  • WARNING
  • ERROR
  • FATAL

效果

#include "log.hpp"

void test_log() {
    LOG(INFO) << "here is the INFO text" << std::endl;
    LOG(DEBUG) << "here is the DEBUG text" << std::endl;
    LOG(WARNING) << "here is the WARNING text" << std::endl;
    LOG(ERROR) << "here is the ERROR text" << std::endl;
    LOG(FATAL) << "here is the FATAL text" << std::endl;
}

int main() {
    test_log();
    return 0;
}

在这里插入图片描述

Log.hpp实现代码

将Log.hpp头文件直接包含就可以使用了。

#ifndef __YUFC_LOG__
#define __YUFC_LOG__

#include <iostream>
#include <map>
#include <string>

enum STATUES // 日志等级
{
    INFO,
    DEBUG,
    WARNING,
    ERROR,
    FATAL
};

// 定义颜色代码
#define RESET "\033[0m"
#define RED "\033[1;31m" // 加粗红色
#define GREEN "\033[1;32m" // 加粗绿色
#define YELLOW "\033[1;33m" // 加粗黄色
#define BLUE "\033[1;34m" // 加粗蓝色
#define MAGENTA "\033[1;35m" // 加粗洋红

// 根据日志等级获取相应颜色
inline const char* GetColor(const std::string& level) {
    std::map<std::string, int> m = { { "INFO", 0 }, { "DEBUG", 1 }, { "WARNING", 2 }, { "ERROR", 3 }, { "FATAL", 4 } };
    switch (m[level]) {
    case INFO:
        return BLUE;
    case DEBUG:
        return GREEN;
    case WARNING:
        return YELLOW;
    case ERROR:
        return MAGENTA;
    case FATAL:
        return RED;
    default:
        return RESET;
    }
}

// LOG() << "message"
inline std::ostream& Log(const std::string& level, const std::string& file_name, int line) {
    // 添加日志等级
    std::string levelTag = std::string("[") + level + "]";
    std::string coloredLevelTag = std::string(GetColor(level)) + levelTag + RESET;
    std::string message = coloredLevelTag;
    // 添加报错文件名称
    message += "[";
    message += file_name;
    message += "]";
    // 添加当前文件的行数
    message += "[";
    message += std::to_string(line);
    message += "]";
    // cout 本质内部是包含缓冲区的
    std::cout << message << " "; // 不要endl进行刷新
    return std::cout;
}

// 这种就是开放式的日志
#define LOG(level) Log(#level, __FILE__, __LINE__)

#endif

网站公告

今日签到

点亮在社区的每一天
去签到