-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.cpp
53 lines (48 loc) · 1.46 KB
/
logger.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
#include "logger.h"
#include <QDateTime>
#include <QDebug>
#include <fstream>
#include <QStandardPaths>
#include <QDir>
logger::logger(QObject *parent) : QObject(parent), logging(false)
{
}
void logger::startLogging()
{
QString writableLocation = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
QDir dir(writableLocation);
if (!dir.exists()) {
dir.mkpath(writableLocation);
}
QString filePath = writableLocation + "/logger.txt";
file.setFileName(filePath);
if (file.open(QIODevice::WriteOnly | QIODevice::Text)) {
stream.setDevice(&file);
logging = true;
stream << "Timestamp,AccelerometerX,AccelerometerY,AccelerometerZ,GyroscopeX,GyroscopeY,GyroscopeZ\n";
qDebug() << "Logging started, writing to" << filePath;
} else {
qDebug() << "Unable to open file for logging at" << filePath;
}
}
void logger::stopLogging()
{
logging = false;
file.close();
}
void logger::onAccelerometerData(qreal x, qreal y, qreal z)
{
if (logging) {
QString timestamp = QDateTime::currentDateTime().toString(Qt::ISODate);
stream << timestamp << "," << x << "," << y << "," << z << ",,,\n";
stream.flush();
}
}
void logger::onGyroscopeData(qreal x, qreal y, qreal z)
{
if (logging) {
QString timestamp = QDateTime::currentDateTime().toString(Qt::ISODate);
stream << timestamp << ",,,," << x << "," << y << "," << z << "\n";
stream.flush();
}
}