logthread.cpp 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. #include "logthread.h"
  2. LogThread::LogThread(QObject *parent) :
  3. QThread(parent)
  4. {
  5. keep = false;
  6. dataList.clear();
  7. wrongList.clear();
  8. QDir path("/usky/data-agbox/agboxdog/log");
  9. if(!path.exists()){
  10. system("mkdir -p /usky/data-agbox/agboxdog/log/");
  11. }
  12. file = new QFile("/usky/data-agbox/agboxdog/log/agboxdog-"+QDate::currentDate().toString("yyyyMMdd")+".log");
  13. wfile = new QFile("/opt/agBoxProcess/log/agboxdog.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::stop()
  25. {
  26. keep = false;
  27. }
  28. void LogThread::run()
  29. {
  30. printf("log thread start\n");
  31. if(file->open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text)){
  32. file->write(QString("agBoxDog start at %1.\r\n").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz")).toUtf8());
  33. file->close();
  34. }
  35. keep = true;
  36. while(keep){
  37. if(dataList.length()>0){
  38. if(day!=QDate::currentDate().day()){
  39. day = QDate::currentDate().day();
  40. file = new QFile("/usky/data-agbox/agboxdog/log/agboxdog-"+QDate::currentDate().toString("yyyyMMdd")+".log");
  41. }
  42. if(file->open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text)){
  43. while(dataList.length()>0){
  44. file->write(dataList.first().toUtf8());
  45. file->write("\r\n");
  46. dataList.removeFirst();
  47. usleep(1000);
  48. }
  49. file->close();
  50. }
  51. }
  52. if(wrongList.length()>0){
  53. if(wfile->open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text)){
  54. while(wrongList.length()>0){
  55. wfile->write(wrongList.first().toUtf8());
  56. wfile->write("\r\n");
  57. wrongList.removeFirst();
  58. usleep(1000);
  59. }
  60. wfile->close();
  61. }
  62. }
  63. usleep(50000);
  64. }
  65. }