| 1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162 |
- #include "logthread.h"
- #include <QDebug>
- 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<list.count();i++){
- if((QString::compare(list.at(i).fileName(),".")!=0)&&(QString::compare(list.at(i).fileName(),"..")!=0)){
- qDebug()<<list.at(i).fileName();
- if((t-list.at(i).lastModified().toTime_t())>(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);
- }
- }
|