logthread.cpp 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. #include "logthread.h"
  2. LogThread::LogThread(QObject *parent) :
  3. QThread(parent)
  4. {
  5. keep = false;
  6. dataList.clear();
  7. wrongList.clear();
  8. printf("logthread init\n");
  9. QDir dir("/usky/dmp/log");
  10. if(!dir.exists())
  11. system("mkdir -p /usky/dmp/log");
  12. file = new QFile("/usky/dmp/log/data-"+QDate::currentDate().toString("yyyyMMdd")+".log");
  13. day = QDate::currentDate().day();
  14. }
  15. void LogThread::appendData(QString data)
  16. {
  17. dataList.append(data);
  18. }
  19. void LogThread::stop()
  20. {
  21. keep = false;
  22. }
  23. void LogThread::run()
  24. {
  25. printf("dmp log thread start\n");
  26. if(file->open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text)){
  27. file->write(QString("ytDMPDataServer start at %1.\r\n").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz")).toUtf8());
  28. file->close();
  29. }
  30. keep = true;
  31. while(keep){
  32. if(dataList.length()>0){
  33. if(day!=QDate::currentDate().day()){
  34. day = QDate::currentDate().day();
  35. file = new QFile("/usky/dmp/log/data-"+QDate::currentDate().toString("yyyyMMdd")+".log");
  36. }
  37. if(file->open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text)){
  38. while(dataList.length()>0){
  39. file->write(dataList.first().toUtf8());
  40. file->write("\r\n");
  41. dataList.removeFirst();
  42. usleep(1000);
  43. }
  44. file->close();
  45. }
  46. }
  47. usleep(50000);
  48. }
  49. }