-
Notifications
You must be signed in to change notification settings - Fork 3
/
ubiqlogsaver.cpp
executable file
·174 lines (143 loc) · 4.47 KB
/
ubiqlogsaver.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
/*
* Copyright (c) 2011 Nokia Corporation
*
* This file is part of the Push Snowboarding Project, More info at:
* www.pushsnowboading.com
*
* Author: Clovis Scotti <scotti@ieee.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
*/
#include "ubiqlogsaver.h"
UbiqLogSaver::UbiqLogSaver(uint a_start_time)
{
start_time = a_start_time;
CreateRunDir();
QString rawLogFilename = (dirName + FSC_FSYS_SLASH) + "RawLog.xml";
qDebug() << "RawLog Filename: " << rawLogFilename;
dataFile = new QFile(rawLogFilename);
dataFile->open(QFile::WriteOnly | QFile::Text);
xml.setDevice(dataFile);
xml.setAutoFormatting(true);
xml.writeStartDocument();
xml.writeStartElement("N8SensorsLog");
runEnd = false;
this->start();
tickcount = 0;
}
UbiqLogSaver::~UbiqLogSaver()
{
this->run_end();//will return immediately if everything went ok since runEnd will be true
}
void UbiqLogSaver::run()
{
while(true)
{
bool leaving = false;
runControlLock.lock();
if(!runEnd)
runControlSignal.wait(&runControlLock);
else
leaving = true;
runControlLock.unlock();
dumpBuffers();
if(leaving){
break;
}
}
exit(0);
}
void UbiqLogSaver::dumpBuffers()
{
QMutexLocker onlyOneAtATime(&dumpBuffersLock);
Q_UNUSED(onlyOneAtATime);
ticksLock.lock();
while(ticksBuffer.count() > 0){
NPushLogTick * tick = ticksBuffer.dequeue();
ticksLock.unlock();
tick->dump_to_xml(xml);
dataFile->flush();
ticksLock.lock();
tickcount++;
// qDebug() << "saving tick = " << tickcount;
}
ticksLock.unlock();
reportsLock.lock();
while(reportBuffer.count() > 0){
qDebug() << "report being dumped";
NPushGenReport * repr = reportBuffer.dequeue();
reportsLock.unlock();
repr->save_to_dir(dirName);
reportsLock.lock();
}
reportsLock.unlock();
}
void UbiqLogSaver::run_end()
{
if(runEnd)
return;
qDebug() << "before signaling exit";
runControlLock.lock();
runEnd = true;
runControlLock.unlock();
qDebug() << "waking up saver";
runControlSignal.wakeAll();
this->wait();//Adding a timeout here? not!?
qDebug() << "saver exited";
xml.writeEndElement();//N8SensorsLog
xml.writeEndDocument();
dataFile->close();
qDebug() << "File closed, dumped " << tickcount << " ticks";
}
void UbiqLogSaver::CreateRunDir()
{
dirName = QString(FSC_RUNS_FOLDERS_ROOT)+QString(FSC_RUNS_PREFIX);
#ifdef Q_WS_MAEMO_5
qDebug() << "In Maemo home = " << QDir::homePath();
dirName = QDir::homePath() + QString("/PushSnowboarding/") + QString(FSC_RUNS_PREFIX);
#endif
dirName += QString::number(start_time);
dirName += QDateTime::fromTime_t(start_time).toString("_hhmm");
QFSFileEngine fse;
qDebug() << "Trying to create: " << dirName;
if(!fse.mkdir(dirName, false)) {
qDebug() << "Could not create run directory";
QCoreApplication::exit();
}
}
void UbiqLogSaver::log_tick_in(NPushLogTick* tick)
{
ticksLock.lock();
ticksBuffer.enqueue(tick);
ticksLock.unlock();
if(ticksBuffer.count() > maxBufferSize) {
dumpBuffers();
}
runControlSignal.wakeAll();
}
void UbiqLogSaver::report_in(NPushGenReport* rep)
{
reportsLock.lock();
reportBuffer.enqueue(rep);
reportsLock.unlock();
if(reportBuffer.count() > maxBufferSize) {
dumpBuffers();
}
runControlSignal.wakeAll();
}