logthread.cpp 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  1. #include "logthread.h"
  2. #include "yt_unit_shm.h"
  3. extern YT_UNIT_SHM *ytShm;
  4. LogThread::LogThread(QObject *parent) :
  5. QThread(parent)
  6. {
  7. logList.clear();
  8. dbgList.clear();
  9. }
  10. void LogThread::appendLog(QString log)
  11. {
  12. logList.append(log);
  13. }
  14. void LogThread::appendDbgLog(QString log)
  15. {
  16. dbgList.append(log);
  17. }
  18. void LogThread::run()
  19. {
  20. ytShm->dogTimeList.dogTime[LOG_THREAD].IDX = LOG_THREAD;
  21. ytShm->dogTimeList.dogTime[LOG_THREAD].WAITSEC = 30;
  22. ytShm->dogTimeList.dogTime[LOG_THREAD].ENABLED = 0x01;
  23. appendLog(QString("[%1] log thread start\r\n")
  24. .arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss")));
  25. // system("echo hello\n");
  26. while(1)
  27. {
  28. ytShm->dogTimeList.dogTime[LOG_THREAD].LASTFEED = QDateTime::currentDateTime().toTime_t();
  29. if(logList.length()>0)
  30. {
  31. QFile logfile(QString("log_%1.txt").arg(QDate::currentDate().toString("yyyyMMdd")));
  32. if(logfile.open(QIODevice::Append|QIODevice::Text|QIODevice::ReadWrite))
  33. {
  34. while(logList.length()>0){
  35. logfile.write(logList.first().toUtf8());
  36. logList.removeFirst();
  37. usleep(10000);
  38. }
  39. logfile.close();
  40. }
  41. }
  42. if(dbgList.length()>0)
  43. {
  44. QFile dbgFile(QString("dbg_%1.txt").arg(QDate::currentDate().toString("yyyyMMdd")));
  45. if(dbgFile.open(QIODevice::Append|QIODevice::Text|QIODevice::ReadWrite))
  46. {
  47. while(dbgList.length()>0)
  48. {
  49. dbgFile.write(dbgList.first().toUtf8());
  50. dbgList.removeFirst();
  51. usleep(10000);
  52. }
  53. dbgFile.close();
  54. }
  55. }
  56. usleep(500000);
  57. }
  58. }