-
Notifications
You must be signed in to change notification settings - Fork 10
/
consolelog.cpp
306 lines (265 loc) · 8.6 KB
/
consolelog.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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
#include "consolelog.h"
consoleLog::consoleLog(QWidget *parent) : QWidget(parent)
{
this->logFileName = "";
this->enableLogToFile = false;
this->createUI();
this->makeConnections();
insertText(QString("[ConsoleLog]: Warning: Not logging text to a file."));
this->logSystemConfig();
}
consoleLog::consoleLog(QString logFileName, bool enableFlightMode, QWidget *parent) : QWidget(parent)
{
this->flightMode = enableFlightMode;
this->logFileName = createFilenameFromDirectory(logFileName);
this->enableLogToFile = true;
this->createUI();
this->makeConnections();
openFile(this->logFileName);
this->logSystemConfig();
}
consoleLog::~consoleLog()
{
this->destroyUI();
this->closeFile();
}
void consoleLog::createUI()
{
logView.setMinimumHeight(200);
logView.setMinimumWidth(230);
logView.setReadOnly(true);
QFont font("monospace");
font.setStyleHint(QFont::Monospace);
logView.setFont(font);
layout.addWidget(&logView);
layout.addItem(&hLayout);
annotateText.setMinimumWidth(200);
annotateText.setFont(font);
annotateBtn.setMaximumWidth(75);
annotateBtn.setText("Annotate");
annotateBtn.setToolTip("Press to annotate the log");
annotateBtn.setDefault(true);
annotateBtn.setAutoDefault(true);
hLayout.addWidget(&annotateText);
hLayout.addWidget(&annotateBtn);
this->setLayout(&layout);
this->setWindowTitle("FlightView Console Log");
this->resize(1156, 512);
logView.setFocusPolicy(Qt::NoFocus);
annotateBtn.setFocusPolicy(Qt::NoFocus);
logView.scroll(0,200);
annotateText.setFocus();
}
void consoleLog::makeConnections()
{
connect(&annotateBtn, SIGNAL(pressed()), this, SLOT(onAnnotateBtnPushed()));
connect(&clearBtn, SIGNAL(pressed()), this, SLOT(onClearBtnPushed()));
connect(&annotateText, SIGNAL(returnPressed()), this, SLOT(onAnnotateBtnPushed()));
}
void consoleLog::destroyUI()
{
}
void consoleLog::writeToFile(QString textOut)
{
if(fileIsOpen)
{
textOut.append("\n");
outfile.write(textOut.toLocal8Bit().data(), textOut.length());
if ( (outfile.rdstate() & std::ifstream::failbit ) != 0 )
{
handleError("ERROR: Could not write text to file!");
}
outfile.flush();
} else {
handleError("Log file is not open, text not being logged.");
}
return;
}
void consoleLog::openFile(QString filename)
{
if(fileIsOpen)
{
// indicate error because file is already open
handleError("ConsoleLog was already opened, tried to open again.");
return;
} else {
if(filename.isEmpty())
{
handleError("ConsoleLog filename is empty. Cannot log to file.");
return;
}
this->logFileName = filename;
outfile.open(filename.toLocal8Bit().data(), std::ios_base::app); // append
if ( (outfile.rdstate() & std::ifstream::failbit ) != 0 )
{
handleError(QString("ConsoleLog file [%1] could not be opened for appending.").arg(filename));
fileIsOpen = false;
return;
} else {
fileIsOpen = true;
insertText(QString("[ConsoleLog]: Opened file [%1] for logging.").arg(filename));
}
}
return;
}
void consoleLog::closeFile()
{
if(fileIsOpen)
{
outfile.flush();
outfile.close();
} else {
handleError(QString("ConsoleLog file [%1] could not be closed"));
}
}
void consoleLog::onClearBtnPushed()
{
logView.clear();
}
void consoleLog::onAnnotateBtnPushed()
{
if(annotateText.text().isEmpty())
return;
insertText("[Operator Annotation]: " + annotateText.text());
if(annotateText.text() == QString(msgInitSequence))
insertText(msgReplySequence);
annotateText.clear();
}
void consoleLog::insertText(QString text)
{
insertTextNoTagging(createTimeStamp() + text);
}
void consoleLog::insertTextNoTagging(QString text)
{
logView.appendPlainText(text);
if(enableLogToFile)
writeToFile(text);
}
QString consoleLog::createTimeStamp()
{
QDateTime now = QDateTime::currentDateTimeUtc();
QString dateString = now.toString("[yyyy-MM-dd hh:mm:ss]: ");
return dateString;
}
QString consoleLog::createFilenameFromDirectory(QString directoryName)
{
QString filename;
if(directoryName.isEmpty())
{
handleError("ERROR: Directory name supplied for logging is empty. Using /tmp.");
directoryName = QString("/tmp/");
}
if(!directoryName.endsWith("/"))
directoryName.append("/");
makeDirectory(directoryName);
QDateTime now = QDateTime::currentDateTimeUtc();
QString dateString;
QString namePrefix = "AV3";
if(flightMode) {
dateString.append(namePrefix);
dateString.append(now.toString("yyyyMMdd"));
dateString.append("t"); // t = "timezone" in datetime, so we must append it this way.
dateString.append(now.toString("hhmmss"));
} else {
dateString = now.toString("yyyy-MM-dd_hhmmss");
}
filename = directoryName + dateString + "-FlightView.log";
return filename;
}
void consoleLog::makeDirectory(QString directoryName)
{
QDir dir(directoryName);
if (!dir.exists())
{
// create directory
handleNote(QString("Creating directory [%1]").arg(directoryName));
dir.mkpath(".");
}
// Check our work:
if(!dir.exists())
{
handleError(QString("Could not create directory [%1]").arg(directoryName));
}
return;
}
void consoleLog::logSystemConfig()
{
struct utsname info;
int rtnval = 0;
rtnval = uname(&info);
if(rtnval)
return;
handleOwnText(QString("Sysname: %1").arg(info.sysname));
handleOwnText(QString("Hostname: %1").arg(info.nodename));
handleOwnText(QString("Kernel release: %1").arg(info.release));
handleOwnText(QString("Kernel version: %1").arg(info.version));
handleOwnText(QString("Machine Type: %1").arg(info.machine));
#ifdef _GNU_SOURCE
handleOwnText(QString("Domainname: %1").arg(info.domainname));
#endif
// Distribution name:
FILE *fp;
char lsbInfo[1024] = {'\0'};
QString infoStr;
fp = popen("/usr/bin/lsb_release -d", "r");
if(fp==NULL)
{
handleOwnText("Could not determine lsb_release");
return;
}
if(fgets(lsbInfo, sizeof(lsbInfo), fp))
{
infoStr = QString(lsbInfo);
infoStr.replace(QString("\t"), QString(" ")).replace("\n", "");
handleOwnText(QString("Linux LSB %1").arg(infoStr));
} else {
handleOwnText("Could not determine lsb_release");
}
pclose(fp);
handleOwnText(QString("Compiled against Qt version: %1").arg(QT_VERSION_STR));
if(!QString(GIT_CURRENT_SHA1_SHORT).isEmpty()) {
handleOwnText(QString("Git short SHA1: %1").arg(GIT_CURRENT_SHA1_SHORT));
handleOwnText(QString("Git long SHA1: %1").arg(GIT_CURRENT_SHA1));
handleOwnText(QString("Link to commit: https://github.com/nasa-jpl/LiveViewLegacy/tree/%1").arg(GIT_CURRENT_SHA1_SHORT));
handleOwnText(QString("Git branch: %1").arg(GIT_BRANCH));
}
if(!QString(SRC_DIR).isEmpty()) {
handleOwnText(QString("Source directory was: %1").arg(SRC_DIR));
}
#ifdef QT_DEBUG
handleOwnText(QString("Compiled as a DEBUG version"));
#else
handleOwnText(QString("Compiled as a RELEASE version"));
#endif
}
void consoleLog::handleOwnText(QString message)
{
insertText(QString("[ConsoleLog]: %1").arg(message));
}
void consoleLog::handleError(QString errorText)
{
// This function is for errors *within* the consoleLog class.
// These errors are not logged to the file since they are likely
// caused by not beign able to log errors.
errorText.prepend("[ConsoleLog ERROR]: ");
logView.appendPlainText(errorText);
std::cout << errorText.toLocal8Bit().data() << std::endl;
}
void consoleLog::handleWarning(QString warningText)
{
// This function is for warnings *within* the consoleLog class.
// These warnings are not logged to the file since they are likely
// caused by not beign able to log or setting up the log.
warningText.prepend("[ConsoleLog WARNING]: ");
logView.appendPlainText(warningText);
std::cout << warningText.toLocal8Bit().data() << std::endl;
}
void consoleLog::handleNote(QString noteText)
{
// This function is for notes *within* the consoleLog class.
// These notes are not logged to the file since they are likely
// caused by not beign able to log or setting up the log.
noteText.prepend("[ConsoleLog NOTE]: ");
logView.appendPlainText(noteText);
std::cout << noteText.toLocal8Bit().data() << std::endl;
}