-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdiagnosticmanager.cpp
More file actions
156 lines (127 loc) · 3.75 KB
/
Copy pathdiagnosticmanager.cpp
File metadata and controls
156 lines (127 loc) · 3.75 KB
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
#include "diagnosticmanager.h"
#include <QDateTime>
#include <QJsonDocument>
#include <QJsonObject>
#include <QMutexLocker>
#include <QLoggingCategory>
Q_LOGGING_CATEGORY(lcDiag, "carputer.diag")
std::atomic<DiagnosticManager *> DiagnosticManager::s_instance{nullptr};
DiagnosticManager::DiagnosticManager(QObject *parent)
: QObject(parent)
{
s_instance.store(this, std::memory_order_relaxed);
m_snapshotTimer.setInterval(2000);
connect(&m_snapshotTimer, &QTimer::timeout,
this, &DiagnosticManager::writeSnapshot);
}
DiagnosticManager::~DiagnosticManager()
{
qInstallMessageHandler(nullptr);
s_instance.store(nullptr, std::memory_order_release);
stopLogging();
}
DiagnosticManager *DiagnosticManager::instance()
{
return s_instance.load(std::memory_order_relaxed);
}
bool DiagnosticManager::isLogging() const
{
return m_logging;
}
QString DiagnosticManager::logFilePath() const
{
return m_logFilePath;
}
void DiagnosticManager::appendLogLine(const QString &line)
{
QMutexLocker lock(&m_mutex);
if (m_logFile.isOpen()) {
m_logFile.write((line + QLatin1Char('\n')).toUtf8());
m_logFile.flush();
}
}
void DiagnosticManager::startLogging()
{
if (m_logging)
return;
const QString ts = QDateTime::currentDateTime().toString(QStringLiteral("yyyyMMdd_HHmmss"));
const QString path = QStringLiteral("/tmp/carputer_diag_%1.log").arg(ts);
openLogFile(path);
if (!m_logFile.isOpen()) {
qCWarning(lcDiag) << "Could not open diagnostic log file:" << path;
return;
}
m_logging = true;
m_snapshotTimer.start();
qCInfo(lcDiag) << "Diagnostic logging started:" << path;
emit loggingChanged();
emit logFilePathChanged();
}
void DiagnosticManager::stopLogging()
{
if (!m_logging)
return;
m_snapshotTimer.stop();
{
QMutexLocker lock(&m_mutex);
if (m_logFile.isOpen()) {
m_logFile.write(
QStringLiteral("# --- Diagnostic logging stopped at %1 ---\n")
.arg(QDateTime::currentDateTime().toString(Qt::ISODate))
.toUtf8());
m_logFile.flush();
}
}
closeLogFile();
m_logging = false;
qCInfo(lcDiag) << "Diagnostic logging stopped";
emit loggingChanged();
}
QString DiagnosticManager::exportReport()
{
const bool wasLogging = m_logging;
if (!wasLogging)
startLogging();
writeSnapshot();
const QString path = m_logFilePath;
if (!wasLogging)
stopLogging();
return path;
}
void DiagnosticManager::writeSnapshot()
{
const QString json = buildSnapshotJson();
QMutexLocker lock(&m_mutex);
if (m_logFile.isOpen()) {
m_logFile.write((json + QLatin1Char('\n')).toUtf8());
m_logFile.flush();
}
}
void DiagnosticManager::openLogFile(const QString &path)
{
QMutexLocker lock(&m_mutex);
m_logFile.setFileName(path);
if (m_logFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
m_logFilePath = path;
m_logFile.write(
QStringLiteral("# Carputer Diagnostic Log\n"
"# Started: %1\n"
"# --- logs follow ---\n")
.arg(QDateTime::currentDateTime().toString(Qt::ISODate))
.toUtf8());
m_logFile.flush();
}
}
void DiagnosticManager::closeLogFile()
{
QMutexLocker lock(&m_mutex);
if (m_logFile.isOpen())
m_logFile.close();
}
QString DiagnosticManager::buildSnapshotJson() const
{
QJsonObject root;
root[QLatin1String("ts")] = QDateTime::currentDateTime().toString(Qt::ISODate);
root[QLatin1String("type")] = QLatin1String("snapshot");
return QString::fromUtf8(QJsonDocument(root).toJson(QJsonDocument::Compact));
}