logthread.cpp 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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;
  10. if(!dir.exists("/usky/datacollector/log"))
  11. system("mkdir -p /usky/datacollector/log");
  12. file = new QFile("/usky/datacollector/log/ytDeviceTypeInfluxdb-"+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::appendWrongData(QString data)
  20. {
  21. wrongList.append(data);
  22. }
  23. void logThread::stop()
  24. {
  25. keep = false;
  26. }
  27. void logThread::run()
  28. {
  29. printf("log thread start\n");
  30. if(file->open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text)){
  31. file->write(QString("ytDeviceTypeInfluxdbWriter start at %1.\r\n").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz")).toUtf8());
  32. file->close();
  33. }
  34. keep = true;
  35. while(keep){
  36. if(dataList.length()>0){
  37. if(day!=QDate::currentDate().day()){
  38. day = QDate::currentDate().day();
  39. file = new QFile("/usky/datacollector/log/ytDeviceTypeInfluxdb-"+QDate::currentDate().toString("yyyyMMdd")+".log");
  40. uint l_time = QDateTime::currentDateTime().toTime_t()-7*86400;
  41. QDir dir("/usky/datacollector/log");
  42. QFileInfoList file_list = dir.entryInfoList(QDir::Files);
  43. for(int i=0;i<file_list.size();i++){
  44. QFileInfo f_info = file_list.at(i);
  45. if(f_info.lastModified().toTime_t()<l_time){
  46. QFile(f_info.canonicalFilePath()).remove();
  47. }
  48. }
  49. }
  50. if(file->open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text)){
  51. while(dataList.length()>0){
  52. file->write(dataList.first().toUtf8());
  53. file->write("\r\n");
  54. dataList.removeFirst();
  55. usleep(1000);
  56. }
  57. file->close();
  58. }
  59. }
  60. usleep(50000);
  61. }
  62. }