#include "logthread.h" #include LogThread::LogThread(QObject *parent) : QThread(parent) { QDir dir("log"); if(!dir.exists()) dir.mkdir("log"); logList.clear(); } LogThread::~LogThread() { if(keepWorking) stop(); this->deleteLater(); } void LogThread::appendLog(QString log) { logList.append(log); } void LogThread::stop() { keepWorking = false; } void LogThread::run() { keepWorking = true; int day = -1; while (keepWorking) { if(logList.length()>0){ QDate today = QDate::currentDate(); if(day!=today.day()){ uint t = QDateTime::currentDateTime().toTime_t(); QDir dir("log"); QFileInfoList list = dir.entryInfoList(); for(int i=0;i(7*86400)){ QFile::remove("log/"+list.at(i).fileName()); } } } day = today.day(); } QFile file(QString("log/log-%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'))); if(file.open(QIODevice::Text|QIODevice::Append|QIODevice::WriteOnly)){ while (logList.length()>0) { file.write(logList.first().toUtf8()+"\n"); logList.removeFirst(); usleep(10000); } file.close(); } } usleep(100000); } }