logthread.cpp 1.4 KB

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