-
Notifications
You must be signed in to change notification settings - Fork 2.1k
/
Copy pathLogging.cpp
144 lines (114 loc) · 3.95 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
/* See Project CHIP LICENSE file for licensing information. */
#include <platform/logging/LogV.h>
#include "qvCHIP.h"
#include <core/CHIPConfig.h>
#include <platform/CHIPDeviceConfig.h>
#include <support/CHIPPlatformMemory.h>
#include <support/logging/Constants.h>
#include <ctype.h>
#include <string.h>
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
#include <openthread/platform/logging.h>
#include <openthread/platform/memory.h>
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
constexpr uint8_t kPrintfModuleLwip = 0x01;
constexpr uint8_t kPrintfModuleOpenThread = 0x02;
constexpr uint8_t kPrintfModuleLogging = 0x03;
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
namespace chip {
namespace Logging {
namespace Platform {
/**
* CHIP log output function.
*/
void LogV(const char * module, uint8_t category, const char * msg, va_list v)
{
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
size_t prefixLen;
prefixLen = 0;
// No build-time switches in Qorvo logging module.
// Add small prefix to show logging category for now.
formattedMsg[prefixLen++] = '[';
switch (category)
{
case kLogCategory_Error:
formattedMsg[prefixLen++] = 'E';
break;
case kLogCategory_Detail:
formattedMsg[prefixLen++] = 'D';
break;
case kLogCategory_Progress:
default:
formattedMsg[prefixLen++] = 'P';
break;
}
formattedMsg[prefixLen++] = ']';
formattedMsg[prefixLen++] = '[';
snprintf(formattedMsg + prefixLen, sizeof(formattedMsg) - prefixLen, "][%s] ", module);
formattedMsg[sizeof(formattedMsg) - 2] = 0; // -2 to allow at least one char for the vsnprintf
prefixLen = strlen(formattedMsg);
vsnprintf(formattedMsg + prefixLen, sizeof(formattedMsg) - prefixLen, msg, v);
qvCHIP_Printf(kPrintfModuleLogging, formattedMsg);
// Let the application know that a log message has been emitted.
chip::DeviceLayer::OnLogOutput();
}
} // namespace Platform
} // namespace Logging
} // namespace chip
/**
* LwIP log output function.
*/
extern "C" void LwIPLog(const char * msg, ...)
{
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
va_list v;
va_start(v, msg);
size_t len = vsnprintf(formattedMsg, sizeof(formattedMsg), msg, v);
va_end(v);
while (len > 0 && isspace(formattedMsg[len - 1]))
{
len--;
formattedMsg[len] = 0;
}
qvCHIP_Printf(kPrintfModuleLwip, formattedMsg);
// Let the application know that a log message has been emitted.
chip::DeviceLayer::OnLogOutput();
}
#if CHIP_DEVICE_CONFIG_ENABLE_THREAD
extern "C" void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char * aFormat, ...)
{
char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
va_list v;
va_start(v, aFormat);
vsnprintf(formattedMsg, sizeof(formattedMsg), aFormat, v);
va_end(v);
qvCHIP_Printf(kPrintfModuleOpenThread, formattedMsg);
// Let the application know that a log message has been emitted.
chip::DeviceLayer::OnLogOutput();
}
// TODO: have qpg6100 openthread platform implementation defines the APIs.
// It is not perfect to have the openthread platform calloc/free
// APIs defined here. If a dedicated source file (e.g. Memory.cpp)
// that includes only the two functions is used, the target file
// Memory.o will be thrown away whening linking the libary because
// there is no one referring the symbols (We are not linking
// the 'platform' library against openthread).
extern "C" void * otPlatCAlloc(size_t aNum, size_t aSize)
{
return CHIPPlatformMemoryCalloc(aNum, aSize);
}
extern "C" void otPlatFree(void * aPtr)
{
CHIPPlatformMemoryFree(aPtr);
}
#endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD