forked from project-chip/connectedhomeip
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathLogging.cpp
208 lines (162 loc) · 5.4 KB
/
Logging.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
/* See Project CHIP LICENSE file for licensing information. */
#include <platform/logging/LogV.h>
#include <inttypes.h>
#include <lib/core/CHIPConfig.h>
#include <lib/support/EnforceFormat.h>
#include <lib/support/logging/Constants.h>
#include <platform/CHIPDeviceConfig.h>
#include <src/lib/support/CodeUtils.h>
#include "fsl_debug_console.h"
#include <cstring>
#ifdef PW_RPC_ENABLED
#include <examples/platform/nxp/PigweedLogger.h>
#endif
#define K32W_LOG_MODULE_NAME chip
#define EOL_CHARS "\r\n" /* End of Line Characters */
#define EOL_CHARS_LEN 2 /* Length of EOL */
/* maximum value for uint32_t is 4294967295 - 10 bytes + 2 bytes for the brackets */
static constexpr uint8_t timestamp_max_len_bytes = 12;
/* one byte for the category + 2 bytes for the brackets */
static constexpr uint8_t category_max_len_bytes = 3;
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
#include <openthread/platform/logging.h>
#include <utils/uart.h>
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
static bool isLogInitialized;
extern "C" uint32_t otPlatAlarmMilliGetNow(void);
extern "C" void otPlatUartSendBlocking(const uint8_t * aBuf, uint32_t len);
namespace chip {
namespace Logging {
namespace Platform {
void GetMessageString(char * buf, uint8_t bufLen, const char * module, uint8_t category)
{
int writtenLen = 0;
const char * categoryString;
/* bufLen must accommodate the length of the timestamp + length of the category +
* length of the module + 2 bytes for the module's brackets +
* 1 byte for the terminating character.
*/
assert(bufLen >= (timestamp_max_len_bytes + category_max_len_bytes + (strlen(module) + 2) + 1));
writtenLen = snprintf(buf, bufLen, "[%ld]", otPlatAlarmMilliGetNow());
bufLen -= writtenLen;
buf += writtenLen;
if (category != kLogCategory_None)
{
switch (category)
{
case kLogCategory_Error:
categoryString = "E";
break;
case kLogCategory_Progress:
categoryString = "P";
break;
case kLogCategory_Detail:
categoryString = "D";
break;
default:
categoryString = "U";
}
writtenLen = snprintf(buf, bufLen, "[%s]", categoryString);
bufLen -= writtenLen;
buf += writtenLen;
}
writtenLen = snprintf(buf, bufLen, "[%s]", module);
}
} // namespace Platform
} // namespace Logging
} // namespace chip
void FillPrefix(char * buf, uint8_t bufLen, const char * module, uint8_t category)
{
chip::Logging::Platform::GetMessageString(buf, bufLen, module, category);
}
namespace chip {
namespace DeviceLayer {
/**
* Called whenever a log message is emitted by CHIP or LwIP.
*
* This function is intended be overridden by the application to, e.g.,
* schedule output of queued log entries.
*/
void __attribute__((weak)) OnLogOutput(void) {}
} // namespace DeviceLayer
} // namespace chip
void ENFORCE_FORMAT(1, 0) GenericLog(const char * format, va_list arg, const char * module, uint8_t category)
{
#if K32W_LOG_ENABLED
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE - 1] = { 0 };
size_t prefixLen, writtenLen;
if (!isLogInitialized)
{
isLogInitialized = true;
#ifndef PW_RPC_ENABLED
otPlatUartEnable();
#endif
}
/* Prefix is composed of [Time Reference][Debug String][Module Name String] */
FillPrefix(formattedMsg, CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE - 1, module, category);
prefixLen = strlen(formattedMsg);
// Append the log message.
writtenLen = vsnprintf(formattedMsg + prefixLen, CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE - prefixLen - EOL_CHARS_LEN, format, arg);
VerifyOrDie(writtenLen > 0);
memcpy(formattedMsg + prefixLen + writtenLen, EOL_CHARS, EOL_CHARS_LEN);
#ifndef PW_RPC_ENABLED
otPlatUartSendBlocking((const uint8_t *) formattedMsg, strlen(formattedMsg));
#else
PigweedLogger::PutString((const char *) formattedMsg, strlen(formattedMsg));
#endif
// Let the application know that a log message has been emitted.
chip::DeviceLayer::OnLogOutput();
#endif // K32W_LOG_ENABLED
}
namespace chip {
namespace Logging {
namespace Platform {
/**
* CHIP log output function.
*/
void LogV(const char * module, uint8_t category, const char * msg, va_list v)
{
(void) module;
(void) category;
#if K32W_LOG_ENABLED
GenericLog(msg, v, module, category);
// Let the application know that a log message has been emitted.
DeviceLayer::OnLogOutput();
#endif // K32W_LOG_ENABLED
}
} // namespace Platform
} // namespace Logging
} // namespace chip
#undef K32W_LOG_MODULE_NAME
#define K32W_LOG_MODULE_NAME lwip
#if CHIP_SYSTEM_CONFIG_USE_LWIP
/**
* LwIP log output function.
*/
extern "C" void ENFORCE_FORMAT(1, 2) LwIPLog(const char * msg, ...)
{
#if K32W_LOG_ENABLED
va_list v;
const char * module = "LWIP";
va_start(v, msg);
GenericLog(msg, v, module, chip::Logging::kLogCategory_None);
va_end(v);
#endif
}
#endif // CHIP_SYSTEM_CONFIG_USE_LWIP
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
#undef K32W_LOG_MODULE_NAME
#define K32W_LOG_MODULE_NAME thread
extern "C" void ENFORCE_FORMAT(3, 4) otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char * aFormat, ...)
{
#if K32W_LOG_ENABLED
va_list v;
const char * module = "OT";
(void) aLogLevel;
(void) aLogRegion;
va_start(v, aFormat);
GenericLog(aFormat, v, module, chip::Logging::kLogCategory_None);
va_end(v);
#endif
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD