logthread.cpp 1.5 KB

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