-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlogger.cpp
177 lines (166 loc) · 5.9 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
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
#include "logger.h"
#include <QJsonObject>
#include <QJsonDocument>
#include <QFile>
#include <QDir>
#include <QDataStream>
#include <QStandardPaths>
#include <QApplication>
Logger::Logger()
{
#ifdef Q_OS_WIN32
appHomeDir = qApp->applicationDirPath() + QDir::separator();
#endif
#ifdef Q_OS_ANDROID
appHomeDir = QStandardPaths::standardLocations(QStandardPaths::DataLocation)[1] + QDir::separator();
#endif
dir.setPath(appHomeDir + "Logs");
if (!dir.exists())
QDir().mkdir(appHomeDir + "Logs");
}
void Logger::startLog()
{
createNewBinFileNamePermission = true;
createNewTxtFileNamePermission = true;
createNewJsonFileNamePermission = true;
writeLogsPermission = true;
}
void Logger::stopLog()
{
writeLogsPermission = false;
// newBinFile.close();
// newLogFile.close();
// newJsonFile.close();
}
void Logger::incomingBinData(const QByteArray data)
{
if (bin && writeLogsPermission)
{
if (!newBinFile.isOpen())
{
if (createNewBinFileNamePermission)
{
binFileName = (dir.path() + "\\" + currentProfileName + '_' + returnTimestamp().toString("dd.MM.yy_hh-mm-ss") + ".bin");
createNewBinFileNamePermission = false;
}
newBinFile.setFileName(binFileName);
if (!newBinFile.exists())
{
newBinFile.open(QIODevice::WriteOnly);
emit toTextLog(QString(tr("Start write log file ")) + newBinFile.fileName(), true);
}
else newBinFile.open(QIODevice::Append);
}
if (newBinFile.isOpen())
{
QDataStream binStream(&newBinFile);
binStream.writeRawData(data.constData(), data.size());
newBinFile.close();
}
else emit showStatusMessage(tr("Error write bin"));
}
else if (!bin && !createNewBinFileNamePermission)
{
emit toTextLog(QString(tr("Stop write bin file")), true);
newBinFile.close();
createNewBinFileNamePermission = true;
}
}
void Logger::incomingTxtData(const QString string)
{
if (txt && writeLogsPermission)//если стоит галка в настройках и есть разрешение на писание логов
{
if (!newLogFile.isOpen())
{
if (createNewTxtFileNamePermission)//обновляем имя файла, если стоит флаг
{
logFileName = (dir.path() + "\\" + currentProfileName + '_' + returnTimestamp().toString("dd.MM.yy_hh-mm-ss") + ".log");
newLogFile.setFileName(logFileName);
createNewTxtFileNamePermission = false;
}
if (!newLogFile.exists() && !logFileName.isEmpty()) //если файла нет - создаём
{
newLogFile.open(QIODevice::WriteOnly|QIODevice::Text);
if (newLogFile.isOpen())
emit toTextLog(QString(tr("Start write log file ")) + newLogFile.fileName(), true);
else emit toTextLog(tr("Error open log file"), true);
}
else newLogFile.open(QIODevice::Append|QIODevice::Text); //если есть - открываем на дописывание
if (newLogFile.isOpen())//если файл открыт - пишем
{
QTextStream logStream(&newLogFile);
while (!txtLogQueue.isEmpty())
logStream << txtLogQueue.dequeue() << '\n';
logStream << string << '\n';
newLogFile.close();//закрываем после записи
}
else emit toTextLog(tr("Error open log file"), true);
}
else txtLogQueue.enqueue(string);
}
if (!txt && !createNewTxtFileNamePermission)//если сняли галку в настройках при активном соединении, закрываем файл
{
emit toTextLog(QString(tr("Stop write log file")), true);
newLogFile.close();
createNewTxtFileNamePermission = true;
txtLogQueue.clear();
}
}
void Logger::incomingJsonData(const QVariantMap jsonMap)
{
if (json && writeLogsPermission)
{
if (!newJsonFile.isOpen())
{
if (createNewJsonFileNamePermission)
{
jsonFileName = (dir.path() + "\\" + currentProfileName + '_' + returnTimestamp().toString("dd.MM.yy_hh-mm-ss") + ".json");
createNewJsonFileNamePermission = false;
}
newJsonFile.setFileName(jsonFileName);
if (!newJsonFile.exists())
{
newJsonFile.open(QIODevice::WriteOnly|QIODevice::Text);
emit toTextLog((QString(tr("Start write json file ")) + newJsonFile.fileName()), true);
}
else newJsonFile.open(QIODevice::Append|QIODevice::Text);
}
if (newJsonFile.isOpen())
{
QJsonObject jsonObj = QJsonObject::fromVariantMap(jsonMap);
newJsonFile.write(QJsonDocument(jsonObj).toJson(QJsonDocument::Compact));
QTextStream newLineStream(&newJsonFile);
newLineStream << '\n';
newJsonFile.close();
}
else emit showStatusMessage(tr("Error write json"));
}
else if (!json && !createNewJsonFileNamePermission)
{
emit toTextLog((QString(tr("Stop write json file"))), true);
newJsonFile.close();
createNewJsonFileNamePermission = true;
}
}
QDateTime Logger::returnTimestamp()
{
quint64 timestamp = QDateTime::currentMSecsSinceEpoch();
QDateTime dt3 = QDateTime::fromMSecsSinceEpoch(timestamp);
return dt3;
}
void Logger::setBin(bool b)
{
bin = b;
}
void Logger::setTxt(bool b)
{
txt = b;
}
void Logger::setJson(bool b)
{
json = b;
}
void Logger::setProfileName(QString name)
{
currentProfileName = name;
}