源代码:
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;
}
运行结果: