-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathlogger.cpp
104 lines (91 loc) · 2.45 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
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
#include <QDir>
#include <QDateTime>
#include "logger.h"
/**
* Constructor. Sets the interface class pointer as
* well as log directory and log file extension.
*/
Logger::Logger(MEMSInterface* memsiface):
m_logExtension(".txt"), m_logDir("logs")
{
m_mems = memsiface;
}
/**
* Attempts to open a log file with the name specified.
* @return True on success, false otherwise
*/
bool Logger::openLog(QString fileName)
{
bool success = false;
m_lastAttemptedLog = m_logDir + QDir::separator() + fileName + m_logExtension;
// if the 'logs' directory exists, or if we're able to create it...
if (!m_logFile.isOpen() && (QDir(m_logDir).exists() || QDir().mkdir(m_logDir)))
{
// set the name of the log file and open it for writing
const bool alreadyExists = QFileInfo(m_lastAttemptedLog).exists();
m_logFile.setFileName(m_lastAttemptedLog);
if (m_logFile.open(QFile::WriteOnly | QFile::Append))
{
m_logFileStream.setDevice(&m_logFile);
if (!alreadyExists)
{
m_logFileStream << "#time,engineSpeed,waterTemp,intakeAirTemp," <<
"throttleVoltage,manifoldPressure,idleBypassPos,mainVoltage," <<
"idleswitch,closedloop,lambdaVoltage_mV" << Qt::endl;
}
success = true;
}
}
return success;
}
/**
* Close the log file.
*/
void Logger::closeLog()
{
m_logFile.close();
}
/**
* Converts degrees F to degrees C if necessary
*/
uint8_t Logger::convertTemp(uint8_t degrees)
{
if (m_tempUnits == Celsius)
{
return degrees;
}
else
{
return ((degrees * 1.8) + 32);
}
}
/**
* Commands the logger to query the interface for the currently
* buffered data, and write it to the file.
*/
void Logger::logData()
{
mems_data* data = m_mems->getData();
if (m_logFile.isOpen() && (m_logFileStream.status() == QTextStream::Ok))
{
m_logFileStream << QDateTime::currentDateTime().toString("hh:mm:ss.zzz") << "," <<
data->engine_rpm << "," <<
convertTemp(data->coolant_temp_c) << "," <<
convertTemp(data->intake_air_temp_c) << "," <<
data->throttle_pot_voltage << "," <<
data->map_kpa << "," <<
data->iac_position << "," <<
data->battery_voltage << "," <<
data->idle_switch << "," <<
data->closed_loop << "," <<
data->lambda_voltage_mv << Qt::endl;
}
}
/**
* Returns the full path to the last log that we attempted to open.
* @return Full path to last log file
*/
QString Logger::getLogPath()
{
return m_lastAttemptedLog;
}