#include "logthread.h" LogThread::LogThread(QObject *parent) : QThread(parent) { QDir dir; if(!dir.exists("log")) 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; while (keepWorking) { if(logList.length()>0){ QDate today = QDate::currentDate(); 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'))); 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); } }