logthread.cpp 1.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #include "logthread.h"
  2. LogThread::LogThread(QObject *parent) : QThread(parent)
  3. {
  4. QDir dir;
  5. if(!dir.exists("log"))
  6. dir.mkdir("log");
  7. logList.clear();
  8. }
  9. LogThread::~LogThread()
  10. {
  11. if(keepWorking)
  12. stop();
  13. this->deleteLater();
  14. }
  15. void LogThread::appendLog(QString log)
  16. {
  17. logList.append(log);
  18. }
  19. void LogThread::stop()
  20. {
  21. keepWorking = false;
  22. }
  23. void LogThread::run()
  24. {
  25. keepWorking = true;
  26. while (keepWorking) {
  27. if(logList.length()>0){
  28. QDate today = QDate::currentDate();
  29. QFile file(QString("log/landwelllog-%1%2%3.txt").arg(today.year(),4,10,QChar('0')).arg(today.month(),2,10,QChar('0')).arg(today.day(),2,10,QChar('0')));
  30. if(file.open(QIODevice::Text|QIODevice::Append|QIODevice::WriteOnly)){
  31. while (logList.length()>0) {
  32. file.write(logList.first().toUtf8()+"\n");
  33. logList.removeFirst();
  34. usleep(10000);
  35. }
  36. file.close();
  37. }
  38. }
  39. usleep(100000);
  40. }
  41. }