当前位置: 首页 > article >正文

C++ 日志输出

源代码:

log.h:

#pragma once

#ifndef LOG_H
#define LOG_H

#include <iostream>
#include <fstream>
#include <string>
#include <ctime>
#include <chrono>
#include <exception>
#include <sstream>
#include <iomanip>
#include <conio.h>
using namespace std;

#define RED 255, 0, 0
#define GREEN 0, 255, 0
#define BLUE 0, 0, 255
#define YELLOW 255, 255, 0

enum Level {
    Debug = 1,
    Info,
    Warning,
    Error,
    Fatal
};

string getColorString(int red, int green, int blue, char* message) {
    return "\033[38;2;" + to_string(red) + ";" + to_string(green) + ";" + to_string(blue) + "m" + message + "\033[0m";
}

string getCurrentTimeString() {
    auto now = chrono::system_clock::now();
    auto in_time_t = chrono::system_clock::to_time_t(now);

    stringstream ss;
    ss << put_time(localtime(&in_time_t), "%Y-%m-%d_%X");
    return ss.str();
}

string getCurrentTimeString_filename() {
    auto now = chrono::system_clock::now();
    auto in_time_t = chrono::system_clock::to_time_t(now);

    stringstream ss;
    ss << put_time(localtime(&in_time_t), "%Y-%m-%d_%H-%M-%S");
    return ss.str();
}


class Log {
public:
    Level level;
    string filename;

    Log() {
        level = Level::Info;
        filename = getCurrentTimeString_filename() + ".log";
    }

    Log(Level level) {
        this->level = level;
        filename = getCurrentTimeString_filename() + ".log";
    }

    Log(Level level, string filename) {
        this->level = level;
        this->filename = filename;
    }

    void setLevel(Level level) {
        this->level = level;
    }

    void setFilename(string filename) {
        this->filename = filename;
    }

    void debug(string message) {
        output(Level::Debug, message);
        write(Level::Debug, message);
    }

    void info(string message) {
        output(Level::Info, message);
        write(Level::Info, message);
    }

    void warning(string message) {
        output(Level::Warning, message);
        write(Level::Warning, message);
    }

    void error(string message) {
        output(Level::Error, message);
        write(Level::Error, message);
    }

    void fatal(string message) {
        output(Level::Fatal, message);
        write(Level::Fatal, message);
    }

    void pause() {
        cout << "Press any key to continue...";
        _getch();
    }

private:
    void write(Level level, string message) {
        fstream logFile;
        logFile.open(filename, ios::app);
        switch (level) {
        case Level::Debug: {
            string debugMessage = getCurrentTimeString() + " [DEBUG] " + message + "\n";
            logFile << debugMessage;
            break;
        }

        case Level::Info: {
            string infoMessage = getCurrentTimeString() + " [INFO] " + message + "\n";
            logFile << infoMessage;
            break;
        }

        case Level::Warning: {
            string warningMessage = getCurrentTimeString() + " [WARNING] " + message + "\n";
            logFile << warningMessage;
            break;
        }

        case Level::Error: {
            string errorMessage = getCurrentTimeString() + " [ERROR] " + message + "\n";
            logFile << errorMessage;
            break;
        }

        case Level::Fatal: {
            string fatalMessage = getCurrentTimeString() + " [FATAL] " + message + "\n";
            logFile << fatalMessage;
            break;
        }

        default:
            break;
        }

        logFile.close();
    }

    void output(Level level, string message) {
        switch (level) {
        case Level::Debug: {
            string debugMessage = getCurrentTimeString() + getColorString(RED, " [DEBUG] ") + message + "\n";
            cout << debugMessage;
            break;
        }

        case Level::Info: {
            string infoMessage = getCurrentTimeString() + getColorString(BLUE, " [INFO] ") + message + "\n";
            cout << infoMessage;
            break;
        }

        case Level::Warning: {
            string warningMessage = getCurrentTimeString() + getColorString(YELLOW, " [WARNING] ") + message + "\n";
            cout << warningMessage;
            break;
        }

        case Level::Error: {
            string errorMessage = getCurrentTimeString() + getColorString(RED, " [ERROR] ") + message + "\n";
            cout << errorMessage;
            break;
        }

        case Level::Fatal: {
            string fatalMessage = getCurrentTimeString() + getColorString(RED, " [FATAL] ") + message + "\n";
            cout << fatalMessage;
            break;
        }

        default:
            break;
        }
    }
};


#endif // LOG_H

main.cpp:

#include "log.h"

int main() {
    Log log;

    log.info("Hello, world!");
    log.debug("This is a debug message.");
    log.warning("This is a warning message.");

    try {
        throw runtime_error("An error occurred.");
    } catch (const exception& e) {
        log.error(e.what());
    }

    log.fatal("This is a fatal error message.");
    log.pause();

    return 0;
}

运行结果


http://www.kler.cn/a/449139.html

相关文章:

  • 基于Spring Boot的九州美食城商户一体化系统
  • C++中的字符串实现
  • 【GO环境安装】mac系统+GoLand使用
  • [react 3种方法] 获取ant组件ref用ts如何定义?
  • 脑肿瘤检测数据集,对9900张原始图片进行YOLO,COCO,VOC格式的标注
  • 问题解决:发现Excel中的部分内容有问题。是否让我们尽量尝试恢复? 如果您信任此工作簿的源,请单击“是”。
  • python中使用时间间隔操作及技巧
  • 【PPTist】组件结构设计、主题切换
  • Flink基本原理 + WebUI说明 + 常见问题分析
  • 某科技局国产服务器PVE虚拟化技术文档
  • 宠物用品电子商务系统|Java|SSM|VUE| 前后端分离
  • 【day13】深入面向对象编程
  • Windows电脑异地SSH远程连接苹果MacOS小主机Mac mini详细教程
  • scala中正则表达式的使用
  • 语音助手关键模块整理
  • 深入探讨 Go 中的高级表单验证与翻译:Gin 与 Validator 的实践之道20241223
  • Linux环境安装部署Prometheus
  • Craft CMS 模板注入导致 Rce漏洞复现(CVE-2024-56145)(附脚本)
  • 矩阵-向量乘法的行与列的解释(Row and Column Interpretations):中英双语
  • yolov目标检测的图片onnx输入尺寸及预处理
  • 杀死名为360安全的软件
  • 14,攻防世界Web_php_unserialize
  • 深入了解Linux —— make和makefile自动化构建工具
  • 以太坊账户详解
  • 构建一个rust生产应用读书笔记7-Mock编码浪子
  • 项目测试方案流程详解