logthread.cpp 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "logthread.h"
  2. LogThread::LogThread(QObject *parent) :
  3. QThread(parent)
  4. {
  5. dataList.clear();
  6. QDir dir("/opt/ytCam/log");
  7. if(!dir.exists())
  8. dir.mkdir("/opt/ytCam/log");
  9. QDir dir2("/opt/ytCam/err/");
  10. if(!dir2.exists())
  11. dir2.mkdir("/opt/ytCam/err/");
  12. file = new QFile("/opt/ytCam/log/Cam01-"+QDate::currentDate().toString("yyyyMMdd")+".log");
  13. wfile = new QFile("/opt/ytCam/err/Cam01_message.log");
  14. day = QDate::currentDate().day();
  15. }
  16. void LogThread::appendData(QString data)
  17. {
  18. dataList.append(data);
  19. }
  20. void LogThread::appendWrongData(QString data)
  21. {
  22. wrongList.append(data);
  23. }
  24. void LogThread::run()
  25. {
  26. {
  27. file = new QFile("/opt/ytCam/log/Cam01-"+QDate::currentDate().toString("yyyyMMdd")+".log");
  28. if(file->open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text)){
  29. file->write(QString("ytDHCam start at %1.\r\n").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz")).toUtf8());
  30. file->close();
  31. }
  32. }
  33. while(1){
  34. if(dataList.length()>0){
  35. if(day!=QDate::currentDate().day()){
  36. day = QDate::currentDate().day();
  37. file = new QFile("/opy/ytCam/log/Cam01-"+QDate::currentDate().toString("yyyyMMdd")+".log");
  38. }
  39. if(file->open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text)){
  40. while(dataList.length()>0){
  41. file->write(dataList.first().toUtf8());
  42. file->write("\r\n");
  43. dataList.removeFirst();
  44. usleep(1000);
  45. }
  46. file->close();
  47. }
  48. }
  49. if(wrongList.length()>0){
  50. if(wfile->open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text)){
  51. while(wrongList.length()>0){
  52. wfile->write(wrongList.first().toUtf8());
  53. wfile->write("\r\n");
  54. wrongList.removeFirst();
  55. usleep(1000);
  56. }
  57. wfile->close();
  58. }
  59. }
  60. usleep(50000);
  61. }
  62. }