MODIFY:精简日志

This commit is contained in:
2023-12-30 00:57:03 +08:00
parent d4d18d81a0
commit bebf9d84ca
2 changed files with 31 additions and 220 deletions
+1 -35
View File
@@ -13,43 +13,11 @@
#include <QTextCodec>
#include "Singleton.h"
//#define log_d(format,...) qDebug(format,__VA_ARGS__)
//#define log_i(format,...) qInfo(format,__VA_ARGS__)
//#define log_w(format,...) qWarning(format,__VA_ARGS__)
//#define log_e(format,...) qCritical(format,__VA_ARGS__)
//#define log_f(format,...) qFatal(format,__VA_ARGS__)
const int g_logLimitSize = 5;
struct LogHandlerPrivate
{
LogHandlerPrivate();
~LogHandlerPrivate();
// qDebug()
// 打开日志文件 log.txt,如果日志文件不是当天创建的,则使用创建日期把其重命名为 yyyy-MM-dd.log,并重新创建一个 log.txt
void openAndBackupLogFile();
void checkLogFiles(); // 检测当前日志文件大小
void autoDeleteLog(); // 自动删除30天前的日志
// 消息处理函数
static void messageHandler(QtMsgType type, const QMessageLogContext &context, const QString &msg);
QDir logDir; // 日志文件夹
QTimer renameLogFileTimer; // 重命名日志文件使用的定时器
QTimer flushLogFileTimer; // 刷新输出到日志文件的定时器
QDate logFileCreatedDate; // 日志文件创建的时间
static QFile *logFile; // 日志文件
static QTextStream *logOut; // 输出日志的 QTextStream,使用静态对象就是为了减少函数调用的开销
static QMutex logMutex; // 同步使用的 mutex
};
class LogHandler
{
public:
void installMessageHandler(); // 给Qt安装消息处理函数
void uninstallMessageHandler(); // 取消安装消息处理函数并释放资源
static LogHandler& Get()
{
static LogHandler m_logHandler;
@@ -57,9 +25,7 @@ public:
}
private:
LogHandler();
LogHandlerPrivate *d;
LogHandler(){};
};
#endif // LOGHANDLER_H