forked from feiyangqingyun/QWidgetDemo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsaveruntime.cpp
220 lines (184 loc) · 6.06 KB
/
saveruntime.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#include "saveruntime.h"
#include "qmutex.h"
#include "qapplication.h"
#include "qtimer.h"
#include "qfile.h"
#include "qtextstream.h"
#include "qstringlist.h"
#ifdef Q_OS_WIN
#define NEWLINE "\r\n"
#else
#define NEWLINE "\n"
#endif
QScopedPointer<SaveRunTime> SaveRunTime::self;
SaveRunTime *SaveRunTime::Instance()
{
if (self.isNull()) {
static QMutex mutex;
QMutexLocker locker(&mutex);
if (self.isNull()) {
self.reset(new SaveRunTime);
}
}
return self.data();
}
SaveRunTime::SaveRunTime(QObject *parent) : QObject(parent)
{
path = qApp->applicationDirPath();
QString str = qApp->applicationFilePath();
QStringList list = str.split("/");
name = list.at(list.count() - 1).split(".").at(0);
saveInterval = 1 * 60 * 1000;
startTime = QDateTime::currentDateTime();
//存储运行时间定时器
timerSave = new QTimer(this);
timerSave->setInterval(saveInterval);
connect(timerSave, SIGNAL(timeout()), this, SLOT(saveLog()));
}
void SaveRunTime::start()
{
//开始时间变量必须在这,在部分嵌入式系统上开机后的时间不准确比如是1970,而后会变成1999或者其他时间
//会在getDiffValue函数执行很久很久
startTime = QDateTime::currentDateTime();
timerSave->start();
initLog();
appendLog();
saveLog();
}
void SaveRunTime::stop()
{
timerSave->stop();
}
void SaveRunTime::getDiffValue(const QDateTime &startTime, const QDateTime &endTime, int &day, int &hour, int &minute)
{
qint64 sec = startTime.secsTo(endTime);
day = hour = minute = 0;
int seconds = 0;
while (sec > 0) {
seconds++;
if (seconds == 60) {
minute++;
seconds = 0;
}
if (minute == 60) {
hour++;
minute = 0;
}
if (hour == 24) {
day++;
hour = 0;
}
sec--;
}
}
void SaveRunTime::initLog()
{
//判断当前年份的记事本文件是否存在,不存在则新建并且写入标题
//存在则自动读取最后一行的id号 记事本文件格式内容
//幢号 开始时间 结束时间 已运行时间
//1 2016-01-01 12:33:33 2016-02-05 12:12:12 day: 0 hour: 0 minute: 0
logFile = QString("%1/%2_runtime_%3.txt").arg(path).arg(name).arg(QDate::currentDate().year());
QFile file(logFile);
if (file.size() == 0) {
if (file.open(QFile::WriteOnly | QFile::Text)) {
QString strID = QString("%1\t").arg("编号");
QString strStartTime = QString("%1\t\t").arg("开始时间");
QString strEndTime = QString("%1\t\t").arg("结束时间");
QString strRunTime = QString("%1").arg("已运行时间");
QString line = strID + strStartTime + strEndTime + strRunTime;
QTextStream stream(&file);
stream << line << NEWLINE;
file.close();
lastID = 0;
}
} else {
if (file.open(QFile::ReadOnly)) {
QString lastLine;
while (!file.atEnd()) {
lastLine = file.readLine();
}
file.close();
QStringList list = lastLine.split("\t");
lastID = list.at(0).toInt();
}
}
lastID++;
}
void SaveRunTime::appendLog()
{
logFile = QString("%1/%2_runtime_%3.txt").arg(path).arg(name).arg(QDate::currentDate().year());
QFile file(logFile);
//写入当前首次运行时间
if (file.open(QFile::WriteOnly | QFile::Append | QFile::Text)) {
QString strID = QString("%1\t").arg(lastID);
QString strStartTime = QString("%1\t").arg(startTime.toString("yyyy-MM-dd HH:mm:ss"));
QString strEndTime = QString("%1\t").arg(QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss"));
int day, hour, minute;
getDiffValue(startTime, QDateTime::currentDateTime(), day, hour, minute);
QString strRunTime = QString("%1 天 %2 时 %3 分").arg(day).arg(hour).arg(minute);
QString line = strID + strStartTime + strEndTime + strRunTime;
QTextStream stream(&file);
stream << line << NEWLINE;
file.close();
}
}
void SaveRunTime::saveLog()
{
//每次保存都是将之前的所有文本读取出来,然后替换最后一行即可
logFile = QString("%1/%2_runtime_%3.txt").arg(path).arg(name).arg(QDate::currentDate().year());
QFile file(logFile);
//如果日志文件不存在,则初始化一个日志文件
if (file.size() == 0) {
initLog();
appendLog();
return;
}
if (file.open(QFile::ReadWrite)) {
//一行行读取到链表
QStringList content;
while (!file.atEnd()) {
content.append(file.readLine());
}
//重新清空文件
file.resize(0);
//如果行数小于2则返回
if (content.count() < 2) {
file.close();
return;
}
QString lastLine = content.last();
QStringList list = lastLine.split("\t");
//计算已运行时间
int day, hour, minute;
getDiffValue(startTime, QDateTime::currentDateTime(), day, hour, minute);
QString strRunTime = QString("%1 天 %2 时 %3 分").arg(day).arg(hour).arg(minute);
//重新拼接最后一行
list[2] = QDateTime::currentDateTime().toString("yyyy-MM-dd HH:mm:ss");
list[3] = strRunTime;
lastLine = list.join("\t");
//重新替换最后一行并写入新的数据
content[content.count() - 1] = lastLine;
QTextStream stream(&file);
stream << content.join("") << NEWLINE;
file.close();
}
}
void SaveRunTime::setPath(const QString &path)
{
if (this->path != path) {
this->path = path;
}
}
void SaveRunTime::setName(const QString &name)
{
if (this->name != name) {
this->name = name;
}
}
void SaveRunTime::setSaveInterval(int saveInterval)
{
if (this->saveInterval != saveInterval) {
this->saveInterval = saveInterval;
timerSave->setInterval(saveInterval);
}
}