-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathflightrecorderlog.cpp
342 lines (274 loc) · 10.6 KB
/
flightrecorderlog.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
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
#include "flightrecorderlog.h"
#include <log.pb.h>
#include <sockethelper.h>
#include <stl_ext.h>
#include <sys/time.h>
#include <unistd.h>
#include <cstdarg>
using namespace FlightRecorder;
namespace plog = protobuf::log;
static const char* logLevelString[] = {"INFO ", "DEBUG", "WARN ", "ERROR"};
LogLevel FlightRecorderLog::logLevel = LogLevel::INFO;
LogLevel FlightRecorderLog::textLogLevel = LogLevel::INFO;
std::list<FlightRecorderLog::BacklogEntryPtr>* FlightRecorderLog::backlog;
std::list<FlightRecorderLog::BacklogEntryPtr>* FlightRecorderLog::textBacklog;
Thread::ProducerConsumerLock* FlightRecorderLog::backlogMutex;
Thread::ProducerConsumerLock* FlightRecorderLog::textBacklogMutex;
LogSource FlightRecorderLog::src = LogSource::FIRMWARE;
void FlightRecorderLog::setTextLogLevel(LogLevel level) {
FlightRecorderLog::textLogLevel = level;
}
void FlightRecorderLog::setLogLevel(LogLevel level) {
FlightRecorderLog::logLevel = level;
}
void FlightRecorderLog::setLogSource(LogSource log_src) {
src = log_src;
}
FlightRecorderLog::FlightRecorderLog() {
#ifdef FLIGHTLOGCLIENT_IS_DUMMY
return;
#endif
static std::list<BacklogEntryPtr> _backlog;
static std::list<BacklogEntryPtr> _textBacklog;
static Thread::ProducerConsumerLock _backlogMutex;
static Thread::ProducerConsumerLock _textBacklogMutex;
backlog = &_backlog;
textBacklog = &_textBacklog;
backlogMutex = &_backlogMutex;
textBacklogMutex = &_textBacklogMutex;
backlogMutex->init([]() -> bool { return FlightRecorderLog::backlog->empty(); });
textBacklogMutex->init([]() -> bool { return FlightRecorderLog::textBacklog->empty(); });
launch_named_thread("NFR:NetWriter", false, &flightLogWriterThread).detach();
launch_named_thread("NFR:ConPrinter", false, &flightLogPrinterThread).detach();
}
void FlightRecorderLog::log(const BacklogEntryPtr& entry) {
#ifdef FLIGHTLOGCLIENT_IS_DUMMY
return;
#endif
backlogMutex->producerLock();
backlog->push_back(entry);
backlogMutex->producerUnlock();
}
void FlightRecorderLog::logText(const BacklogEntryPtr& entry) {
#ifdef FLIGHTLOGCLIENT_IS_DUMMY
return;
#endif
textBacklogMutex->producerLock();
textBacklog->push_back(entry);
textBacklogMutex->producerUnlock();
}
void FlightRecorderLog::flightLogPrinterThread() {
std::list<BacklogEntryPtr> entries;
while (true) {
textBacklogMutex->consumerLock();
while (!textBacklog->empty()) {
entries.push_back(textBacklog->front());
textBacklog->pop_front();
}
textBacklogMutex->consumerUnlock();
tm tms;
time_t tmpTime = time(nullptr);
memset(&tms, 0, sizeof(tms));
localtime_r(&tmpTime, &tms);
for (auto& entry : entries) {
plog::LogEntry logEntry;
logEntry.ParseFromArray(entry->ptr, entry->length);
if (logEntry.loglevel() == plog::ERROR) {
fprintf(stderr, "\e[1;91m%02d:%02d:%02d [%s][%s] %s\e[0m\n", tms.tm_hour, tms.tm_min, tms.tm_sec,
logLevelString[logEntry.loglevel()], logEntry.subsystem().c_str(), logEntry.logentry().c_str());
fflush(stderr);
} else {
printf("%02d:%02d:%02d [%s][%s] %s\n", tms.tm_hour, tms.tm_min, tms.tm_sec,
logLevelString[logEntry.loglevel()], logEntry.subsystem().c_str(), logEntry.logentry().c_str());
}
}
entries.clear();
}
}
void FlightRecorderLog::flightLogWriterThread() {
while (true) {
int sock;
struct sockaddr_in saddr;
uint16_t port = 40588;
if (src == LogSource::BIDGE)
port = 40589;
if (!openTcpSendSocket(sock, saddr, "127.0.0.1", port)) {
/* If we don't have a connection remove everything to have not a memory leak */
backlogMutex->consumerLock();
backlog->clear();
backlogMutex->consumerUnlock();
sleep(1);
continue;
}
std::list<BacklogEntryPtr> entries;
while (true) {
backlogMutex->consumerLock();
/* Copy everything may have been logged in between to our private list. */
backlog->swap(entries);
backlogMutex->consumerUnlock();
bool isFine = true;
/* Write all cached entries */
while (!entries.empty()) {
auto entry = entries.front();
entries.pop_front();
if (!socketWriteBytes(sock, sizeof(entry->length), &(entry->length))) {
close(sock);
isFine = false;
break;
}
if (!socketWriteBytes(sock, entry->length, entry->ptr)) {
close(sock);
isFine = false;
break;
}
}
// If we have a socket connection we don't want to keep everything.
entries.clear();
if (!isFine)
break;
}
}
}
LogPtr FlightRecorderLog::instance(const char* subsystem) {
static auto* instance = new FlightRecorderLog();
return LogPtr(new Log(subsystem, *instance));
}
void Log::info(google::protobuf::MessageLite& msg, const char* typeinfo) {
logProtobuf(LogLevel::INFO, msg, typeinfo);
}
void Log::debug(google::protobuf::MessageLite& msg, const char* typeinfo) {
logProtobuf(LogLevel::DEBUG, msg, typeinfo);
}
void Log::warn(google::protobuf::MessageLite& msg, const char* typeinfo) {
logProtobuf(LogLevel::WARN, msg, typeinfo);
}
void Log::err(google::protobuf::MessageLite& msg, const char* typeinfo) {
logProtobuf(LogLevel::ERROR, msg, typeinfo);
}
void Log::info(const void* ptr, uint32_t length, const char* typeinfo) {
logBinaryData(LogLevel::INFO, ptr, length, typeinfo);
}
void Log::debug(const void* ptr, uint32_t length, const char* typeinfo) {
logBinaryData(LogLevel::DEBUG, ptr, length, typeinfo);
}
void Log::warn(const void* ptr, uint32_t length, const char* typeinfo) {
logBinaryData(LogLevel::WARN, ptr, length, typeinfo);
}
void Log::err(const void* ptr, uint32_t length, const char* typeinfo) {
logBinaryData(LogLevel::ERROR, ptr, length, typeinfo);
}
void Log::infoMsg(const char* message, ...) {
va_list args;
char buffer[1024];
va_start(args, message);
vsnprintf(buffer, sizeof(buffer), message, args);
va_end(args);
logText(LogLevel::INFO, buffer);
}
void Log::debugMsg(const char* message, ...) {
va_list args;
char buffer[1024];
va_start(args, message);
vsnprintf(buffer, sizeof(buffer), message, args);
va_end(args);
logText(LogLevel::DEBUG, buffer);
}
void Log::warnMsg(const char* message, ...) {
va_list args;
char buffer[1024];
va_start(args, message);
vsnprintf(buffer, sizeof(buffer), message, args);
va_end(args);
logText(LogLevel::WARN, buffer);
}
void Log::errMsg(const char* message, ...) {
va_list args;
char buffer[1024];
va_start(args, message);
vsnprintf(buffer, sizeof(buffer), message, args);
va_end(args);
logText(LogLevel::ERROR, buffer);
}
void Log::logProtobuf(LogLevel loglevel, google::protobuf::MessageLite& msg, const char* typeinfo) {
if (loglevel < FlightRecorderLog::logLevel)
return;
size_t desiredSize = msg.ByteSizeLong();
char* buffer = (char*)malloc(desiredSize);
if (buffer == nullptr) {
fprintf(stderr, "Error malloc buffer for log %s %d\n", __FILE__, __LINE__);
return;
}
if (!msg.SerializeToArray(buffer, desiredSize)) {
fprintf(stderr, "Error serializing protobuf in Log %s %d\n", __FILE__, __LINE__);
free(buffer);
return;
}
plog::LogEntry entry;
entry.set_loglevel((plog::LogLevel)loglevel);
entry.set_logtype(plog::PROTOBUF);
entry.set_subsystem(subsystem);
entry.set_timestamp(time_us());
entry.set_logentry(buffer, desiredSize);
entry.set_size(desiredSize);
entry.set_src(recorder.src == LogSource::FIRMWARE ? protobuf::log::LogSource::FIRMWARE
: protobuf::log::LogSource::BRIDGE);
if (typeinfo != nullptr) {
entry.set_typeinfo(typeinfo);
}
size_t logEntrySize = entry.ByteSizeLong();
FlightRecorderLog::BacklogEntryPtr logEntry = std::make_shared<FlightRecorderLog::BacklogEntry>(logEntrySize);
if (!entry.SerializeToArray(logEntry->ptr, logEntrySize)) {
fprintf(stderr, "ERROR: Serializing log entry in %s %d\n", __FILE__, __LINE__);
free(buffer);
return;
}
recorder.log(logEntry);
entry.Clear();
free(buffer);
}
void Log::logBinaryData(LogLevel loglevel, const void* ptr, uint32_t length, const char* typeinfo) {
if (loglevel < FlightRecorderLog::logLevel)
return;
plog::LogEntry entry;
entry.set_loglevel((plog::LogLevel)loglevel);
entry.set_logtype(plog::BINARY);
entry.set_subsystem(subsystem);
entry.set_timestamp(time_us());
entry.set_logentry(ptr, length);
entry.set_size(length);
entry.set_src(recorder.src == LogSource::FIRMWARE ? protobuf::log::LogSource::FIRMWARE
: protobuf::log::LogSource::BRIDGE);
if (typeinfo != nullptr) {
entry.set_typeinfo(typeinfo);
}
size_t logEntrySize = entry.ByteSizeLong();
FlightRecorderLog::BacklogEntryPtr logEntry(new FlightRecorderLog::BacklogEntry(logEntrySize));
if (!entry.SerializeToArray(logEntry->ptr, logEntrySize)) {
fprintf(stderr, "ERROR: Serializing log entry in %s %d\n", __FILE__, __LINE__);
return;
}
entry.Clear();
recorder.log(logEntry);
}
void Log::logText(LogLevel loglevel, const char* message) {
if (loglevel < FlightRecorderLog::textLogLevel)
return;
plog::LogEntry entry;
entry.set_loglevel((plog::LogLevel)loglevel);
entry.set_logtype(plog::TEXT);
entry.set_subsystem(subsystem);
entry.set_timestamp(time_us());
entry.set_logentry(message, strlen(message));
entry.set_size(strlen(message));
entry.set_src(recorder.src == LogSource::FIRMWARE ? protobuf::log::LogSource::FIRMWARE
: protobuf::log::LogSource::BRIDGE);
size_t logEntrySize = entry.ByteSizeLong();
FlightRecorderLog::BacklogEntryPtr logEntry(new FlightRecorderLog::BacklogEntry(logEntrySize));
if (!entry.SerializeToArray(logEntry->ptr, logEntrySize)) {
fprintf(stderr, "ERROR: Serializing log entry in %s %d\n", __FILE__, __LINE__);
return;
}
recorder.log(logEntry);
recorder.logText(logEntry);
entry.Clear();
}