1.AppLog.cpp
#include "AppLog.h"
#include <QMutex>
#include <QFile>
#include <QDateTime>
#include <QTextStream>
#include "../FileAndDir/FileOperator.h"
void outputMessage(QtMsgType type, const QMessageLogContext &context, const QString &msg)
{
static QMutex mutex;
mutex.lock();
QString text;
bool isLog = true;
switch(type)
{
case QtDebugMsg:
text = QString("Debug:");
isLog = true;
break;
case QtWarningMsg:
text = QString("Warning:");
isLog = false;
break;
case QtCriticalMsg:
text = QString("Critical:");
break;
case QtFatalMsg:
text = QString("Fatal:");
}
QString message = "";
QString _logPath = AppLog::GetInstance()->getPath();
QString _logName = AppLog::GetInstance()->getLogName(type);
if (context.file != nullptr)
{
QString context_info;
if(isLog){
context_info = QString("File:(%1) - Line:(%2)").arg(context.file).arg(context.line);
}else{
context_info = QString("Line:(%1)").arg(context.line);
}
QString current_date_time = QDateTime::currentDateTime().toString("yyyy-MM-dd hh:mm:ss ddd");
QString current_date = QString("[%1]").arg(current_date_time);
message = QString("%1 %2 %3 %4").arg(current_date).arg(text).arg(context_info).arg(msg);
}
else
{
message = msg;
}
QFile file(_logPath + "/" + _logName +".txt");
if(file.exists())
{
file.open(QIODevice::WriteOnly | QIODevice::Append);
}else
{
FileOperator::GetInstance()->creatFile(_logPath,_logName,".txt");
file.open(QIODevice::WriteOnly | QIODevice::Append);
}
QTextStream text_stream(&file);
text_stream << message << "\r\n";
file.flush();
file.close();
mutex.unlock();
}
AppLog::AppLog()
{
logPath = "C:/QtLogData";
bool ret = FileOperator::GetInstance()->creatDir("C:/","QtLogData");
Q_UNUSED(ret);
qInstallMessageHandler(outputMessage);
}
void AppLog::qDebug_Info(int type, QString strInfo)
{
QMessageLogContext context;
context.file = nullptr;
outputMessage((QtMsgType)type, context, strInfo);
}
void AppLog::setLogPath(QString path)
{
logPath = path;
bool ret = FileOperator::GetInstance()->creatDir(logPath.left(logPath.lastIndexOf("/")),logPath.section('/', -1));
Q_UNUSED(ret);
}
void AppLog::setLogName(QString name)
{
logFileName = name;
}
QString AppLog::getLogName(QtMsgType type)
{
switch (type) {
case QtDebugMsg:
return "Debug";
case QtWarningMsg:
return "Warning";
case QtCriticalMsg:
return QString("Critical");
case QtFatalMsg:
return QString("Fatal");
case QtInfoMsg:
return QString("Log");
}
return "Log";
}
QString AppLog::getPath()
{
return logPath;
}
AppLog * AppLog::instance = nullptr;
AppLog *AppLog::GetInstance()
{
if(nullptr == instance)
{
instance = new AppLog();
}
return instance;
}
2.AppLog.h
/*
** 事件规划: qDebug_Info参数1
** 0 : 调试事件
** 1 : 警告事件
** 2 : 紧急(故障)事件
** 3 : 致命(严重)事件
** 4 : 系统(System)事件
*/
#include <QObject>
class AppLog : public QObject
{
Q_OBJECT
public:
AppLog();
Q_INVOKABLE void qDebug_Info(int type, QString strInfo);
Q_INVOKABLE void setLogPath(QString path);
Q_INVOKABLE void setLogName(QString name);
Q_INVOKABLE QString getLogName(QtMsgType type);
Q_INVOKABLE QString getPath();
static AppLog* GetInstance();
private:
QString logPath;
QString logFileName;
static AppLog * instance;
};
其中FileOperator类是文件和文件夹操作的一些方法.用于检查文件夹或者文件是否为空.
用于Qt和QML打印混合阻截,同时在日志记录本地时间.