修訂 | f904d12b0357c0e675604c36814860309c854e17 (tree) |
---|---|
時間 | 2013-01-29 00:20:59 |
作者 | arakaki <alucky4416@user...> |
Commiter | arakaki |
feature: add function save log to file.
@@ -67,10 +67,11 @@ void DAQThread::run() | ||
67 | 67 | while(!stopped) { |
68 | 68 | if (1) { |
69 | 69 | timestamp = QDateTime::currentDateTime(); |
70 | - GetTempHumidTrue(deviceStr, &tmpr, &humid); | |
70 | + | |
71 | + GetTempHumidTrue(deviceStr, &tmpr, &humid); // Get Data from USBRH | |
71 | 72 | |
72 | 73 | //qDebug() << "NowTime=" << timestamp << ", Tempr=" << tmpr << ", humid=" << humid; |
73 | - emit GetData(timestamp, tmpr, humid); | |
74 | + emit GetData(timestamp, tmpr, humid); // Send Data to MainWindow | |
74 | 75 | |
75 | 76 | if (logging) { |
76 | 77 | // Send Data to LogTh; // toMSecsSinceEpoch() is UTC ms uint64 |
@@ -1,5 +1,10 @@ | ||
1 | 1 | #include "logthread.h" |
2 | 2 | |
3 | +#include <QFile> | |
4 | +#include <QDir> | |
5 | +#include <QTextStream> | |
6 | + | |
7 | +#include <QDateTime> | |
3 | 8 | #include <QDebug> |
4 | 9 | |
5 | 10 | LogThread::LogThread(QObject *parent) : |
@@ -57,6 +62,9 @@ void LogThread::run() | ||
57 | 62 | |
58 | 63 | qDebug() << "logTh is started."; |
59 | 64 | EventData event; |
65 | + QFile csvfile; | |
66 | + QTextStream out; | |
67 | + QDateTime timestamp; | |
60 | 68 | while(!stopped) { |
61 | 69 | if (EvtQue->isEmpty()) { |
62 | 70 | msleep(100); |
@@ -65,13 +73,29 @@ void LogThread::run() | ||
65 | 73 | if (!logging) { // idle |
66 | 74 | if (event.id == Ev_Log_Start) { |
67 | 75 | // then file open |
68 | - logging = true; | |
69 | - qDebug() << "logTh log started."; | |
70 | - emit LogStarted(); | |
76 | + logfilepath = logsavefolderpath + QDir::separator() + "USBRHLog_" + QDateTime::currentDateTime().toString("yyyyMMdd") + ".csv"; | |
77 | + csvfile.setFileName(logfilepath); | |
78 | + if (csvfile.open(QIODevice::WriteOnly | QIODevice::Append)) { | |
79 | + logging = true; | |
80 | + qDebug() << "logTh log started. logfilename is " << logfilepath; | |
81 | + out.flush(); | |
82 | + out.setDevice(&csvfile); | |
83 | + out.setRealNumberNotation(QTextStream::FixedNotation); | |
84 | + out.setRealNumberPrecision(2); | |
85 | + if (csvfile.size() == 0) { | |
86 | + out << "timestamp, tempr(C), humid(%)" << endl; // append header | |
87 | + } | |
88 | + emit LogStarted(); | |
89 | + } else { | |
90 | + emit LogEnded(1); | |
91 | + qDebug() << "open error"; | |
92 | + } | |
71 | 93 | } |
72 | 94 | } else { // logging |
73 | 95 | if (event.id == Ev_Log_Stop) { |
74 | 96 | // then file close |
97 | + out.flush(); | |
98 | + if (csvfile.isOpen()) csvfile.close(); | |
75 | 99 | logsavefolderpath = ""; |
76 | 100 | logfilepath = ""; |
77 | 101 | logging = false; |
@@ -79,11 +103,23 @@ void LogThread::run() | ||
79 | 103 | emit LogEnded(0); |
80 | 104 | } else if (event.id == Ev_Log_Data) { |
81 | 105 | // if data; // write data to file. |
82 | - qDebug() << "tmpr = " << event.tmpr << ", humid = " << event.humid; | |
106 | + timestamp.setMSecsSinceEpoch(event.timestamp_ms); | |
107 | + qDebug() << "timestamp: " << timestamp.toString("yyyy/MM/dd hh:mm:ss") << "," << "tmpr = " << event.tmpr << ", humid = " << event.humid; | |
108 | + out << timestamp.toString("yyyy/MM/dd hh:mm:ss") << "," << event.tmpr << "," << event.humid << endl; | |
83 | 109 | } |
84 | 110 | } |
85 | 111 | } |
86 | 112 | } |
87 | - qDebug() << "logTh is stopped."; | |
88 | 113 | |
114 | + if (logging) { | |
115 | + logging = false; | |
116 | + logsavefolderpath = ""; | |
117 | + emit LogEnded(0); | |
118 | + } | |
119 | + if (csvfile.isOpen()) { | |
120 | + out.flush(); | |
121 | + csvfile.close(); | |
122 | + } | |
123 | + | |
124 | + qDebug() << "logTh is stopped."; | |
89 | 125 | } |