Files
AireOptoSifAndSpectral/othersoft/TemperatureControler/src/json/logtext.h
2025-03-10 10:03:01 +08:00

92 lines
2.7 KiB
C++
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

//
// Created by xin on 2021/8/17.
//
#ifndef TOWERASD_LOGTEXT_H
#define TOWERASD_LOGTEXT_H
#include <QFile>
#include <QTextStream>
#include <QDateTime>
#include "qmutex.h"
#include "QtMsgHandler"
//默认调试级别为warning即小于warning级别的都不会写入日志文件
//只有release版本的时候才会输出到日志debug版本正常输出到终端。
namespace QT_LOG
{//默认文件名为当前时间命名的log文件
static int m_LogLevel = 1;
static QString m_LogFile = QString("%1.log").arg(QDateTime::currentDateTime().toString("yyyyMMddhhmmss"));
QMutex m_LogMutex;
void customMessageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
if (type < m_LogLevel)//设置输出日志级别小于该级别将不会写入日志文件默认是warning级别即debug信息不会写入日志文件
{
return;
}
QString log_info;
switch (type)
{
case QtDebugMsg:
log_info = QString("%1:%2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"),msg);
break;
case QtWarningMsg:
log_info = QString("%1[Warning]:%2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"),msg);
break;
case QtCriticalMsg:
log_info = QString("%1[Critical]:%2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"),msg);
break;
case QtFatalMsg:
log_info = QString("%1[Fatal]:%2").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss"),msg);
abort();
}
//为了线程安全
m_LogMutex.lock();
QFile outFile(m_LogFile);
outFile.open(QIODevice::WriteOnly | QIODevice::Append | QIODevice::Text);
QTextStream ts(&outFile);
ts << log_info << endl;
outFile.close();
m_LogMutex.unlock();
}
//默认调试级别为warning及以上才会写入日志文件默认log文件名为程序启动时间命名的log文件
void logInit(QString logFile = "",int logLevel = 0)
{
#ifndef DEBUG //实现debug版本的时候输出到终端release版本的时候输出到日志文件
if ((logLevel < 0) || (logLevel > 3))
{
m_LogLevel = 1;
}
else
{
m_LogLevel = logLevel;
}
if (!logFile.isEmpty())
{
m_LogFile = logFile+"/"+m_LogFile;
}
qInstallMessageHandler(customMessageHandler);
//qInstallMsgHandler(customMessageHandler);
#endif
}
};
#endif //TOWERASD_LOGTEXT_H