日志类
此类使用qInstallMessageHandler注册一个自定义的消息处理器替换掉默认QtMessageHandler。
通过 qInstallMessageHandler(QtMessageHandler handler)函数注册一个自定义的 MessageHandler,QtMessageHandler 的定义如下:
typedef void (*QtMessageHandler)(QtMsgType, const QMessageLogContext &, const QString &);
可以看到,QtMessageHandler 并不是一个类,而是一个函数指针,函数原型满足?void myMessageHandler(QtMsgType, const QMessageLogContext &, const QString &) 的函数都可以作为 QtMessageHandler 参数传递个?qInstallMessageHandler ?函数中。
话不多说,上代码。
Wlog.h
#ifndef WLOG_H
#define WLOG_H
//解决中文乱码 源代码编码必须是: UTF-8(BOM)
#if _MSC_VER >= 1600
#pragma execution_character_set("utf-8")
#endif
#include <QObject>
#include <QDebug>
#include <string>
#ifdef _WIN64
#define filename(x) strrchr(x,'\\')?strrchr(x,'\\')+1:x
#else
#define filename(x) strrchr(x,'/')?strrchr(x,'/')+1:x
#endif
#ifdef _WIN64
#define INFO_PRINT qDebug()<<"[INFO]"
#define SUCCESS_PRINT qDebug()<<"[SUCCESS]"
#define ERROR_PRINT qDebug()<<"[ERROR]"
#define FOCUS_PRINT qDebug()<<"[FOCUS]"
#define INFO_PRINT_LINE qDebug()<<"[INFO]"<<(filename(__FILE__))<<__LINE__
#define SUCCESS_PRINT_LINE qDebug()<<"[SUCCESS]"<<(filename(__FILE__))<<__LINE__
#define ERROR_PRINT_LINE qDebug()<<"[ERROR]"<<(filename(__FILE__))<<__LINE__
#define FOCUS_PRINT_LINE qDebug()<<"[FOCUS]"<<(filename(__FILE__))<<__LINE__
#else
#define INFO_PRINT qDebug()<<"\033[0m[INFO] "
#define SUCCESS_PRINT qDebug()<<"\033[1m\033[32m[SUCCESS]"
#define ERROR_PRINT qDebug()<<"\033[1m\033[31m[ERROR]"
#define FOCUS_PRINT qDebug()<<"\033[1m\033[33m[FOCUS]"
#endif
class Wlog : public QObject
{
Q_OBJECT
public:
static Wlog* getInstance();
public:
void init(QString name = QString());
QString getName();
public:
static void loggerOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg);
static void setColor(QString info);
private:
Wlog(QObject *parent = nullptr);
private:
QString m_name;
};
#endif // WLOG_H
Wlog.cpp
#include "Wlog.h"
#include <QMutex>
#include <QDateTime>
#include <fstream>
#include <iostream>
#include <QDir>
#ifdef _WIN64
#include <windows.h>
#endif
Wlog* Wlog::getInstance()
{
static Wlog instance;
return &instance;
}
Wlog::Wlog(QObject *parent)
: QObject(parent)
{
}
void Wlog::init(QString name)
{
m_name = name;
QDir dir;
QString dirName = "./log";
if (!dir.exists(dirName))
{
dir.mkpath(dirName);
}
qInstallMessageHandler(Wlog::loggerOutput);
}
QString Wlog::getName()
{
return m_name;
}
void Wlog::loggerOutput(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
static QMutex mutex;
mutex.lock();
#ifdef _WIN64
setColor(msg);
#endif
QString result = msg;
result = result.remove("\033[0m").remove("\033[32m").remove("\033[31m").remove("\033[21m");
QByteArray localMsg = result.toLocal8Bit();
QByteArray originMsg = msg.toLocal8Bit();
QString strMsg("");
switch (type)
{
case QtDebugMsg:
strMsg = QString("INFO");
break;
case QtWarningMsg:
strMsg = QString("WARN");
break;
case QtCriticalMsg:
strMsg = QString("ERROR");
break;
case QtFatalMsg:
strMsg = QString("ERROR");
break;
}
// 设置输出信息格式;
QString strDateTime = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss.zzz");
// 输出信息至文件中(读写、追加形式);
std::ofstream out("log/" + Wlog::getInstance()->getName().toStdString() + "_" + strMsg.toStdString() + "_" + QDateTime::currentDateTime().toString("yyyy-MM-dd").toStdString() + ".txt", std::ios::app);
out << strDateTime.toStdString() << " " << localMsg.constData() << std::endl;
out.flush();
out.close();
std::cout <<"["<< strDateTime.toStdString() <<"]"<< originMsg.constData() << std::endl;
#ifdef _WIN64
//恢复颜色
setColor("");
#endif
// 解锁;
mutex.unlock();
}
void Wlog::setColor(QString info)
{
#ifdef _WIN64
HANDLE hCon = GetStdHandle(STD_OUTPUT_HANDLE); //获取缓冲区句柄;
if (info.isEmpty())
{
SetConsoleTextAttribute(hCon, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
else if (info.contains("[INFO"))
{
SetConsoleTextAttribute(hCon, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN | FOREGROUND_BLUE);
}
else if (info.contains("[SUCCESS"))
{
SetConsoleTextAttribute(hCon, FOREGROUND_INTENSITY | FOREGROUND_GREEN);
}
else if (info.contains("[ERROR"))
{
SetConsoleTextAttribute(hCon, FOREGROUND_INTENSITY | FOREGROUND_RED);
}
else if (info.contains("[FOCUS"))
{
SetConsoleTextAttribute(hCon, FOREGROUND_INTENSITY | FOREGROUND_RED | FOREGROUND_GREEN);
}
#endif
}
此列中调用 qInstallMessageHandler(Wlog::loggerOutput)。
使用方法
#include <QCoreApplication>
#include "Wlog.h"
int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);
Wlog::getInstance()->init();
INFO_PRINT<<"info print";
ERROR_PRINT<<"error print";
SUCCESS_PRINT<<"success print";
return a.exec();
}
|