-
Notifications
You must be signed in to change notification settings - Fork 1.8k
Expand file tree
/
Copy pathlog.c
More file actions
238 lines (198 loc) · 6.04 KB
/
log.c
File metadata and controls
238 lines (198 loc) · 6.04 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
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
/*
* This file is part of INAV.
*
* INAV is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* INAV is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with INAV. If not, see <http://www.gnu.org/licenses/>.
*/
#include <stdbool.h>
#include <stdint.h>
#include <stdarg.h>
#include <ctype.h>
#if defined(SEMIHOSTING) || defined(SITL_BUILD)
#include <stdio.h>
#endif
#include "build/version.h"
#include "build/debug.h"
#include "drivers/serial.h"
#include "drivers/time.h"
#include "common/log.h"
#include "common/printf.h"
#include "common/utils.h"
#include "config/feature.h"
#include "config/parameter_group_ids.h"
#include "io/serial.h"
#include "fc/config.h"
#include "fc/settings.h"
#include "msp/msp.h"
#include "msp/msp_serial.h"
#include "msp/msp_protocol.h"
#if defined(USE_LOG)
#define LOG_PREFIX "[%6d.%03d] "
#define LOG_PREFIX_FORMATTED_SIZE 13
static serialPort_t * logPort = NULL;
static mspPort_t * mspLogPort = NULL;
PG_REGISTER(logConfig_t, logConfig, PG_LOG_CONFIG, 0);
PG_RESET_TEMPLATE(logConfig_t, logConfig,
.level = SETTING_LOG_LEVEL_DEFAULT,
.topics = SETTING_LOG_TOPICS_DEFAULT
);
#if defined(USE_BOOTLOG)
char bootlog_buffer[USE_BOOTLOG];
char *bootlog_head = bootlog_buffer;
#endif
void logInit(void)
{
const serialPortConfig_t *portConfig = findSerialPortConfig(FUNCTION_LOG);
if (!portConfig) {
return;
}
bool portIsSharedWithMSP = false;
if (determinePortSharing(portConfig, FUNCTION_LOG) == PORTSHARING_SHARED) {
// We support sharing a LOG port only with MSP
if (portConfig->functionMask != (FUNCTION_LOG | FUNCTION_MSP)) {
return;
}
portIsSharedWithMSP = true;
}
// If the port is shared with MSP, reuse the port
if (portIsSharedWithMSP) {
const serialPort_t *logAndMspPort = findSharedSerialPort(FUNCTION_LOG, FUNCTION_MSP);
if (!logAndMspPort) {
return;
}
mspLogPort = mspSerialPortFind(logAndMspPort);
if (!mspLogPort) {
return;
}
} else {
logPort = openSerialPort(portConfig->identifier, FUNCTION_LOG, NULL, NULL, baudRates[BAUD_921600], MODE_TX, SERIAL_NOT_INVERTED);
if (!logPort) {
return;
}
}
// Initialization done
LOG_INFO(SYSTEM, "%s/%s %s %s / %s (%s)",
FC_FIRMWARE_NAME,
targetName,
FC_VERSION_STRING,
buildDate,
buildTime,
shortGitRevision
);
}
static void logPutcp(void *p, char ch)
{
*(*((char **) p))++ = ch;
}
static void logPrint(const char *buf, size_t size)
{
#if defined(SEMIHOSTING)
static bool semihostingInitialized = false;
extern void initialise_monitor_handles(void);
if (!semihostingInitialized) {
initialise_monitor_handles();
semihostingInitialized = true;
}
for (size_t ii = 0; ii < size; ii++) {
fputc(buf[ii], stdout);
}
#endif
SD(printf("%s\n", buf));
if (logPort) {
// Send data via UART (if configured & connected - a safeguard against zombie VCP)
if (serialIsConnected(logPort)) {
serialPrint(logPort, buf);
}
} else if (mspLogPort) {
mspSerialPushPort(MSP_DEBUGMSG, (uint8_t*)buf, size, mspLogPort, MSP_V2_NATIVE);
}
#ifdef USE_BOOTLOG
if ( (bootlog_head + size + 2) < (bootlog_buffer + USE_BOOTLOG) ) {
for (unsigned int ii = 0; ii < size; ii++) {
*bootlog_head = buf[ii];
bootlog_head++;
}
bootlog_head[0] = '\r';
bootlog_head[1] = '\n';
bootlog_head = bootlog_head + 2;
}
#endif
}
static size_t logFormatPrefix(char *buf, const timeMs_t timeMs)
{
// Write timestamp
return tfp_sprintf(buf, LOG_PREFIX, (int)(timeMs / 1000), (int)(timeMs % 1000));
}
static bool logHasOutput(void)
{
#if defined(SEMIHOSTING)
return true;
#else
return logPort || mspLogPort;
#endif
}
static bool logIsEnabled(logTopic_e topic, unsigned level)
{
return logHasOutput() && (level <= logConfig()->level || (logConfig()->topics & (1 << topic)));
}
void _logf(logTopic_e topic, unsigned level, const char *fmt, ...)
{
char buf[128];
char *bufPtr;
int charCount;
STATIC_ASSERT(MSP_PORT_OUTBUF_SIZE >= sizeof(buf), MSP_PORT_OUTBUF_SIZE_not_big_enough_for_log);
if (!logIsEnabled(topic, level)) {
return;
}
charCount = logFormatPrefix(buf, millis());
bufPtr = &buf[charCount];
// Write message
va_list va;
va_start(va, fmt);
charCount += tfp_format(&bufPtr, logPutcp, fmt, va);
logPutcp(&bufPtr, '\n');
logPutcp(&bufPtr, 0);
charCount += 2;
va_end(va);
logPrint(buf, charCount);
}
void _logBufferHex(logTopic_e topic, unsigned level, const void *buffer, size_t size)
{
// Print lines of up to maxBytes bytes. We need 5 characters per byte
// 0xAB[space|\n]
const size_t charsPerByte = 5;
const size_t maxBytes = 8;
char buf[LOG_PREFIX_FORMATTED_SIZE + charsPerByte * maxBytes + 1]; // +1 for the null terminator
size_t bufPos = LOG_PREFIX_FORMATTED_SIZE;
const uint8_t *inputPtr = buffer;
if (!logIsEnabled(topic, level)) {
return;
}
logFormatPrefix(buf, millis());
for (size_t ii = 0; ii < size; ii++) {
tfp_sprintf(buf + bufPos, "0x%02x ", inputPtr[ii]);
bufPos += charsPerByte;
if (bufPos == sizeof(buf)-1) {
buf[bufPos-1] = '\n';
buf[bufPos] = '\0';
logPrint(buf, bufPos + 1);
bufPos = LOG_PREFIX_FORMATTED_SIZE;
}
}
if (bufPos > LOG_PREFIX_FORMATTED_SIZE) {
buf[bufPos-1] = '\n';
buf[bufPos] = '\0';
logPrint(buf, bufPos + 1);
}
}
#endif