1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768 |
- #include "logthread.h"
- LogThread::LogThread(QObject *parent) :
- QThread(parent)
- {
- dataList.clear();
- QDir dir;
- if(!dir.exists("log"))
- dir.mkdir("log");
- QDir dir2;
- if(!dir2.exists("err"))
- dir2.mkdir("err");
- file = new QFile("log/DashBoard01-"+QDate::currentDate().toString("yyyyMMdd")+".log");
- wfile = new QFile("err/DashBoard01_message.log");
- day = QDate::currentDate().day();
- }
- void LogThread::appendData(QString data)
- {
- dataList.append(data);
- }
- void LogThread::appendWrongData(QString data)
- {
- wrongList.append(data);
- }
- void LogThread::run()
- {
- {
- file = new QFile("log/DashBoard01-"+QDate::currentDate().toString("yyyyMMdd")+".log");
- if(file->open(QIODevice::ReadWrite|QIODevice::Append|QIODevice::Text)){
- file->write(QString("ytDashBoard start at %1.\r\n").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss.zzz")).toUtf8());
- file->close();
- }
- }
- while(1){
- if(dataList.length()>0){
- if(day!=QDate::currentDate().day()){
- day = QDate::currentDate().day();
- file = new QFile("log/DashBoard01-"+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);
- }
- }
|