progmon.h 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. #ifndef PROGMON_H
  2. #define PROGMON_H
  3. #include <string>
  4. #include <vector>
  5. #include <thread>
  6. #include <QProcess>
  7. #include <QtXml>
  8. #include <QMutex>
  9. // Used For Program Monitor
  10. #include "cpumem.h"
  11. #include "ivlog.h"
  12. #include "ivfault.h"
  13. #include "QtDBus/QDBusMessage"
  14. #include "QtDBus/QDBusConnection"
  15. #include "progunit.h"
  16. #include "procmemstat.h"
  17. #ifdef IV_OS_WIN
  18. #include <windows.h>
  19. #endif
  20. class stdoutunit
  21. {
  22. public:
  23. QProcess * mpProc;
  24. QByteArray mba;
  25. public:
  26. stdoutunit(QProcess * pProc,QByteArray & ba)
  27. {
  28. mpProc = pProc;
  29. mba = ba;
  30. }
  31. };
  32. class ProgMon : public QThread
  33. {
  34. Q_OBJECT
  35. private slots:
  36. void onProcessStarted();
  37. void onProcessEnd();
  38. void onChRead();
  39. //add tjc
  40. void onProcessErrorStarted(QProcess::ProcessError error);
  41. //test
  42. void StopProcTest();
  43. void onReadStandardOutput();
  44. void onReadStandardError();
  45. public slots:
  46. bool onCheckExit(ProgUnit * pu);
  47. public:
  48. /* From A XML Load Program List */
  49. ProgMon(std::string path);
  50. ~ProgMon();
  51. /* Program List */
  52. std::vector<ProgUnit> mvectorprog;
  53. /* Start A Process */
  54. void StartProc(ProgUnit * pu);
  55. /* Stop A Process */
  56. void StopProc(ProgUnit * pu);
  57. /* fault stop process */
  58. void ForceStopProc(ProgUnit * pu);
  59. void restartProc(ProgUnit *pu);
  60. //判断启动状态
  61. bool checkStartState(ProgUnit *pu);
  62. void updatexml(std::string path);
  63. std::vector<ProgUnit> loadprogunit(std::string path);
  64. void StartProc(std::string strappname,std::string strargs="");
  65. void StopProc(std::string strappname,std::string strargs="");
  66. private:
  67. QMutex mMutex;
  68. ProgUnit * FindProcByName(std::string strappname,std::string strargs);
  69. signals:
  70. /* Signal when proc started */
  71. void SigProcStarted(ProgUnit * pu);
  72. /* Signal When proc stoped */
  73. void SigProcStoped(ProgUnit * pu);
  74. void checkExit(ProgUnit * pu);
  75. private:
  76. void run();
  77. bool mbquit = false;
  78. public:
  79. void setquit();
  80. private:
  81. void threadstdout();
  82. void threaderrout();
  83. std::vector<stdoutunit> mvectorstdout;
  84. QMutex mMutex_stdout;
  85. bool mbstdoutrun = true;
  86. std::vector<stdoutunit> mvectorerrout;
  87. QMutex mMutex_errout;
  88. bool mberroutrun = true;
  89. bool mbAllNoLog = true;
  90. std::thread * mpthread_stdout;
  91. std::thread * mpthread_errout;
  92. private:
  93. inline void LogError(QProcess * proc,QByteArray & ba);
  94. QFile mFileLog;
  95. bool mbFileLog = false;
  96. void InitLog();
  97. void WriteLog(const char * strlog);
  98. QFile mFileStdLog;
  99. bool mbFileStdLog = false;
  100. void WriteStdLog(const char * strlog);
  101. int get_path_availspace(const QString & path);
  102. private:
  103. qint64 mPhyMemTotal;
  104. qint64 mPhyMemAvail;
  105. qint64 mMemTotal;
  106. float mfCPULoad;
  107. std::vector<float> mvectorcpuload;
  108. void UpdateCPUMemStat();
  109. ProcMemStat mPMS;
  110. QString mstrsysInfo = "";
  111. QMutex mMutexSysinfo;
  112. public:
  113. QString GetSysInfo();
  114. };
  115. #endif // PROGMON_H