DebugManager.h 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  1. 
  2. #ifndef DEBUGMANAGER_H
  3. #define DEBUGMANAGER_H
  4. #include "Common.h"
  5. #include <QObject>
  6. #include <QFile>
  7. #include <QMutex>
  8. #include <QElapsedTimer>
  9. #include <QDebug>
  10. #include <QDir>
  11. #include <QDateTime>
  12. #include <QThread>
  13. #include <QtConcurrentRun>
  14. /**
  15. * @brief 简易的日志管理类,作为单例
  16. * @details
  17. * 初始化时调用initManager重定向
  18. * @note
  19. * 改为手动调用initManager是为了便于流程控制
  20. * 此外,也可以手动调用freeManager
  21. */
  22. class DebugManager : public QObject
  23. {
  24. Q_OBJECT
  25. DebugManager();
  26. public:
  27. ~DebugManager();
  28. //获取单例实例
  29. static DebugManager* getInstance();
  30. //重定向qdebug输出
  31. void outputLog(QtMsgType type, const QMessageLogContext& context, const QString& msg);
  32. //初始化,如重定向等
  33. void initManager(const QString& path = QString());
  34. //释放
  35. void freeManager();
  36. QList<DEBUGMSG> m_AllDebugMsg;
  37. signals:
  38. //可以关联信号接收日志信息,如显示到ui中
  39. //注意,如果槽函数为lambda或者其他没有接收者的情况,需要保证槽函数中的变量有效性
  40. //因为static变量的生命周期更长,可能槽函数所在模块已经释放资源,最好connect加上接收者
  41. void newDebug(int msgType, const QString& log);
  42. private:
  43. //保留默认handle,用于输出到控制台
  44. QtMessageHandler defaultOutput = nullptr;
  45. //输出到文件
  46. QFile file;
  47. //输出路径
  48. QString filePath;
  49. //多线程操作时需要加锁
  50. QMutex logMutex;
  51. //计算操作间隔,分时生成文件
  52. QElapsedTimer elapsedTimer;
  53. };
  54. #endif // DBUGMANAGER_H