C++ 写一个多线程,可以配置输出等级的日志类
时间: 2023-05-31 15:06:57 浏览: 173
以下是一个基本的多线程日志类,可以配置输出等级:
```c
#include <iostream>
#include <fstream>
#include <ctime>
#include <mutex>
#include <thread>
#include <queue>
#include <condition_variable>
using namespace std;
enum class LogLevel {
DEBUG,
INFO,
WARNING,
ERROR
};
class Logger {
public:
Logger(LogLevel level = LogLevel::DEBUG) : m_logLevel(level), m_stop(false) {
m_thread = thread(&Logger::worker, this);
}
~Logger() {
m_stop = true;
m_cv.notify_one();
m_thread.join();
}
void log(LogLevel level, const string& message) {
unique_lock<mutex> lock(m_mutex);
if (level < m_logLevel) {
return;
}
m_queue.push(make_pair(level, message));
m_cv.notify_one();
}
void setLogLevel(LogLevel level) {
m_logLevel = level;
}
private:
void worker() {
ofstream file("log.txt");
if (!file.is_open()) {
cerr << "Failed to open log file!" << endl;
return;
}
while (!m_stop) {
unique_lock<mutex> lock(m_mutex);
m_cv.wait(lock, [this] { return !m_queue.empty() || m_stop; });
while (!m_queue.empty()) {
auto log = m_queue.front();
m_queue.pop();
lock.unlock();
file << getTimestamp() << " [" << getLevelString(log.first) << "] " << log.second << endl;
lock.lock();
}
}
file.close();
}
string getTimestamp() {
time_t now = time(nullptr);
char buffer[20];
strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", localtime(&now));
return buffer;
}
string getLevelString(LogLevel level) {
switch (level) {
case LogLevel::DEBUG:
return "DEBUG";
case LogLevel::INFO:
return "INFO";
case LogLevel::WARNING:
return "WARNING";
case LogLevel::ERROR:
return "ERROR";
default:
return "";
}
}
LogLevel m_logLevel;
queue<pair<LogLevel, string>> m_queue;
mutex m_mutex;
condition_variable m_cv;
thread m_thread;
bool m_stop;
};
int main() {
Logger logger(LogLevel::INFO);
logger.log(LogLevel::DEBUG, "Debug message"); // shouldn't be printed
logger.log(LogLevel::INFO, "Info message"); // should be printed
logger.log(LogLevel::WARNING, "Warning message"); // should be printed
logger.log(LogLevel::ERROR, "Error message"); // should be printed
logger.setLogLevel(LogLevel::WARNING);
logger.log(LogLevel::DEBUG, "Debug message"); // shouldn't be printed
logger.log(LogLevel::INFO, "Info message"); // shouldn't be printed
logger.log(LogLevel::WARNING, "Warning message"); // should be printed
logger.log(LogLevel::ERROR, "Error message"); // should be printed
return 0;
}
```
该日志类使用一个队列来缓存日志信息,当队列中有日志时,工作线程会将其写入文件。日志的输出等级可以通过设置 `LogLevel` 来控制,低于该等级的日志将被忽略。同时,在程序结束前,需要调用 `Logger` 的析构函数来结束工作线程。
阅读全文