C++ 日志输出

源代码:

log.h:

cpp 复制代码
#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:

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;
}

运行结果

相关推荐
晓纪同学27 分钟前
QT-简单视觉框架代码
开发语言·qt
威桑27 分钟前
Qt SizePolicy详解:minimum 与 minimumExpanding 的区别
开发语言·qt·扩张策略
飞飞-躺着更舒服30 分钟前
【QT】实现电子飞行显示器(简易版)
开发语言·qt
明月看潮生36 分钟前
青少年编程与数学 02-004 Go语言Web编程 16课题、并发编程
开发语言·青少年编程·并发编程·编程与数学·goweb
明月看潮生39 分钟前
青少年编程与数学 02-004 Go语言Web编程 17课题、静态文件
开发语言·青少年编程·编程与数学·goweb
Java Fans41 分钟前
C# 中串口读取问题及解决方案
开发语言·c#
盛派网络小助手1 小时前
微信 SDK 更新 Sample,NCF 文档和模板更新,更多更新日志,欢迎解锁
开发语言·人工智能·后端·架构·c#
Chinese Red Guest1 小时前
python
开发语言·python·pygame
一棵星2 小时前
Java模拟Mqtt客户端连接Mqtt Broker
java·开发语言
xiaoshiguang32 小时前
LeetCode:222.完全二叉树节点的数量
算法·leetcode