|
|
|
#include "loghandler.h"
|
|
|
|
|
|
|
|
#include <QCoreApplication>
|
|
|
|
|
|
|
|
QMutex g_mutex;
|
|
|
|
//QTextStream *g_in = nullptr;
|
|
|
|
QFile g_file;
|
|
|
|
|
|
|
|
// 消息处理函数
|
|
|
|
void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
|
|
|
|
{
|
|
|
|
g_mutex.lock();
|
|
|
|
QString level;
|
|
|
|
|
|
|
|
switch (type)
|
|
|
|
{
|
|
|
|
case QtDebugMsg:
|
|
|
|
level = "DEBUG";
|
|
|
|
break;
|
|
|
|
case QtInfoMsg:
|
|
|
|
level = "INFO ";
|
|
|
|
break;
|
|
|
|
case QtWarningMsg:
|
|
|
|
level = "WARN ";
|
|
|
|
break;
|
|
|
|
case QtCriticalMsg:
|
|
|
|
level = "ERROR";
|
|
|
|
break;
|
|
|
|
case QtFatalMsg:
|
|
|
|
level = "FATAL";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 输出到标准输出: Windows 下 std::cout 使用 GB2312,而 msg 使用 UTF-8,但是程序的 Local 也还是使用 UTF-8
|
|
|
|
#if defined(Q_OS_WIN)
|
|
|
|
QByteArray localMsg = QTextCodec::codecForName("GB2312")->fromUnicode(msg); //msg.toLocal8Bit();
|
|
|
|
#else
|
|
|
|
QByteArray localMsg = msg.toLocal8Bit();
|
|
|
|
#endif
|
|
|
|
|
|
|
|
// 输出到日志文件, 格式: 时间 - [Level] (文件名:行数, 函数): 消息
|
|
|
|
QString fileName = context.file;
|
|
|
|
int index = fileName.lastIndexOf(QDir::separator());
|
|
|
|
fileName = fileName.mid(index + 1);
|
|
|
|
QString msgOut = QString("%1 - [%2] (%3:%4, %5): %6")
|
|
|
|
.arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss")).arg(level)
|
|
|
|
.arg(fileName).arg(context.line).arg(context.function).arg(msg);
|
|
|
|
std::cout << msgOut.toLocal8Bit().constData() << std::endl;
|
|
|
|
QTextStream in(&g_file);
|
|
|
|
in.setCodec("UTF-8");
|
|
|
|
in << msgOut << "\n";
|
|
|
|
// g_file.flush();
|
|
|
|
g_mutex.unlock();
|
|
|
|
}
|
|
|
|
|
|
|
|
// 给Qt安装消息处理函数
|
|
|
|
void LogHandler::installMessageHandler()
|
|
|
|
{
|
|
|
|
//获取日期
|
|
|
|
auto _currentDate = QDateTime::currentDateTime().toString("yyyy-MM-dd");
|
|
|
|
|
|
|
|
//打开/创建文件
|
|
|
|
auto _appDirPath = QCoreApplication::applicationDirPath();
|
|
|
|
auto _logDirPath = _appDirPath + "/logs/";
|
|
|
|
QDir _logDir(_logDirPath);
|
|
|
|
if(!_logDir.exists())
|
|
|
|
_logDir.mkpath(_logDirPath);
|
|
|
|
|
|
|
|
QString _logPath;
|
|
|
|
for(int i = 1; i < 1000; ++i)
|
|
|
|
{
|
|
|
|
_logPath = _logDirPath + _currentDate + QString("_%1.log").arg(i);
|
|
|
|
QFileInfo _fileInfo(_logPath);
|
|
|
|
if(!_fileInfo.exists())
|
|
|
|
{
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
g_file.setFileName(_logPath);
|
|
|
|
if(!g_file.open(QIODevice::WriteOnly | QIODevice::Text | QIODevice::Append))
|
|
|
|
{
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
// g_in = new QTextStream(&g_file);
|
|
|
|
qInstallMessageHandler(messageHandler); // 给 Qt 安装自定义消息处理函数
|
|
|
|
}
|
|
|
|
|
|
|
|
// 取消安装消息处理函数并释放资源
|
|
|
|
void LogHandler::uninstallMessageHandler()
|
|
|
|
{
|
|
|
|
g_mutex.lock();
|
|
|
|
if(g_file.isOpen())
|
|
|
|
{
|
|
|
|
g_file.flush();
|
|
|
|
g_file.close();
|
|
|
|
}
|
|
|
|
g_mutex.unlock();
|
|
|
|
qInstallMessageHandler(nullptr);
|
|
|
|
}
|