1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374 |
- #include "logthread.h"
- logThread::logThread(QObject *parent) :
- QThread(parent)
- {
- keep = false;
- dataList.clear();
- wrongList.clear();
- printf("logthread init\n");
- QDir dir;
- if(!dir.exists("/usky/datacollector/log"))
- system("mkdir -p /usky/datacollector/log");
- file = new QFile("/usky/datacollector/log/ytDeviceTypeInfluxdb-"+QDate::currentDate().toString("yyyyMMdd")+".log");
- day = QDate::currentDate().day();
- }
- void logThread::appendData(QString data)
- {
- dataList.append(data);
- }
- void logThread::appendWrongData(QString data)
- {
- wrongList.append(data);
- }
- void logThread::stop()
- {
- keep = false;
- }
- void logThread::run()
- {
- printf("log thread start\n");
- if(file->open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text)){
- file->write(QString("ytDeviceTypeInfluxdbWriter start at %1.\r\n").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz")).toUtf8());
- file->close();
- }
- keep = true;
- while(keep){
- if(dataList.length()>0){
- if(day!=QDate::currentDate().day()){
- day = QDate::currentDate().day();
- file = new QFile("/usky/datacollector/log/ytDeviceTypeInfluxdb-"+QDate::currentDate().toString("yyyyMMdd")+".log");
- uint l_time = QDateTime::currentDateTime().toTime_t()-7*86400;
- QDir dir("/usky/datacollector/log");
- QFileInfoList file_list = dir.entryInfoList(QDir::Files);
- for(int i=0;i<file_list.size();i++){
- QFileInfo f_info = file_list.at(i);
- if(f_info.lastModified().toTime_t()<l_time){
- QFile(f_info.canonicalFilePath()).remove();
- }
- }
- }
- if(file->open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text)){
- while(dataList.length()>0){
- file->write(dataList.first().toUtf8());
- file->write("\r\n");
- dataList.removeFirst();
- usleep(1000);
- }
- file->close();
- }
- }
- usleep(50000);
- }
- }
|