1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 |
-
- #ifndef DEBUGMANAGER_H
- #define DEBUGMANAGER_H
- #include "Common.h"
- #include <QObject>
- #include <QFile>
- #include <QMutex>
- #include <QElapsedTimer>
- #include <QDebug>
- #include <QDir>
- #include <QDateTime>
- #include <QThread>
- #include <QtConcurrentRun>
- /**
- * @brief 简易的日志管理类,作为单例
- * @details
- * 初始化时调用initManager重定向
- * @note
- * 改为手动调用initManager是为了便于流程控制
- * 此外,也可以手动调用freeManager
- */
- class DebugManager : public QObject
- {
- Q_OBJECT
- DebugManager();
- public:
- ~DebugManager();
- //获取单例实例
- static DebugManager* getInstance();
- //重定向qdebug输出
- void outputLog(QtMsgType type, const QMessageLogContext& context, const QString& msg);
- //初始化,如重定向等
- void initManager(const QString& path = QString());
- //释放
- void freeManager();
- QList<DEBUGMSG> m_AllDebugMsg;
- signals:
- //可以关联信号接收日志信息,如显示到ui中
- //注意,如果槽函数为lambda或者其他没有接收者的情况,需要保证槽函数中的变量有效性
- //因为static变量的生命周期更长,可能槽函数所在模块已经释放资源,最好connect加上接收者
- void newDebug(int msgType, const QString& log);
- private:
- //保留默认handle,用于输出到控制台
- QtMessageHandler defaultOutput = nullptr;
- //输出到文件
- QFile file;
- //输出路径
- QString filePath;
- //多线程操作时需要加锁
- QMutex logMutex;
- //计算操作间隔,分时生成文件
- QElapsedTimer elapsedTimer;
- };
- #endif // DBUGMANAGER_H
|