12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970 |
- #include "logthread.h"
- #include "smartlightshm.h"
- LogThread::LogThread(QObject *parent)
- : QThread (parent)
- {
- keep = false;
- dataList.clear();
- wrongList.clear();
- QDir dir(QString(LogPath));
- if(!dir.exists())
- dir.mkdir(QString(LogPath));
- file = new QFile(QString(LogPath)+"/smartlight-"+QDate::currentDate().toString("yyyyMMdd")+".log");
- wfile = new QFile(QString(LogPath)+"/error.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("ytSmartLight 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(QString(LogPath)+"/smartlight-"+QDate::currentDate().toString("yyyyMMdd")+".log");
- }
- 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();
- }
- }
- if(wrongList.length()>0){
- if(wfile->open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text)){
- while(wrongList.length()>0){
- wfile->write(wrongList.first().toUtf8());
- wfile->write("\r\n");
- wrongList.removeFirst();
- usleep(1000);
- }
- wfile->close();
- }
- }
- usleep(50000);
- }
- }
|