Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / qpg6100 / Logging.cpp
1 /* See Project CHIP LICENSE file for licensing information. */
2 #include <platform/logging/LogV.h>
3
4 #include "qvCHIP.h"
5
6 #include <core/CHIPConfig.h>
7 #include <platform/CHIPDeviceConfig.h>
8 #include <support/CHIPPlatformMemory.h>
9 #include <support/logging/Constants.h>
10
11 #include <ctype.h>
12 #include <string.h>
13
14 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD
15 #include <openthread/platform/logging.h>
16 #include <openthread/platform/memory.h>
17 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD
18
19 constexpr uint8_t kPrintfModuleLwip       = 0x01;
20 constexpr uint8_t kPrintfModuleOpenThread = 0x02;
21 constexpr uint8_t kPrintfModuleLogging    = 0x03;
22
23 namespace chip {
24 namespace DeviceLayer {
25
26 /**
27  * Called whenever a log message is emitted by chip or LwIP.
28  *
29  * This function is intended be overridden by the application to, e.g.,
30  * schedule output of queued log entries.
31  */
32 void __attribute__((weak)) OnLogOutput(void) {}
33
34 } // namespace DeviceLayer
35 } // namespace chip
36
37 namespace chip {
38 namespace Logging {
39 namespace Platform {
40
41 /**
42  * CHIP log output function.
43  */
44
45 void LogV(const char * module, uint8_t category, const char * msg, va_list v)
46 {
47     char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
48     size_t prefixLen;
49
50     prefixLen = 0;
51
52     // No build-time switches in Qorvo logging module.
53     // Add small prefix to show logging category for now.
54     formattedMsg[prefixLen++] = '[';
55     switch (category)
56     {
57     case kLogCategory_Error:
58         formattedMsg[prefixLen++] = 'E';
59         break;
60     case kLogCategory_Detail:
61         formattedMsg[prefixLen++] = 'D';
62         break;
63     case kLogCategory_Progress:
64     default:
65         formattedMsg[prefixLen++] = 'P';
66         break;
67     }
68     snprintf(formattedMsg + prefixLen, sizeof(formattedMsg) - prefixLen, "][%s] ", module);
69     formattedMsg[sizeof(formattedMsg) - 2] = 0; // -2 to allow at least one char for the vsnprintf
70     prefixLen                              = strlen(formattedMsg);
71
72     vsnprintf(formattedMsg + prefixLen, sizeof(formattedMsg) - prefixLen, msg, v);
73
74     qvCHIP_Printf(kPrintfModuleLogging, formattedMsg);
75
76     // Let the application know that a log message has been emitted.
77     chip::DeviceLayer::OnLogOutput();
78 }
79
80 } // namespace Platform
81 } // namespace Logging
82 } // namespace chip
83
84 /**
85  * LwIP log output function.
86  */
87 extern "C" void LwIPLog(const char * msg, ...)
88 {
89     char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
90
91     va_list v;
92
93     va_start(v, msg);
94     size_t len = vsnprintf(formattedMsg, sizeof(formattedMsg), msg, v);
95     va_end(v);
96
97     while (len > 0 && isspace(formattedMsg[len - 1]))
98     {
99         len--;
100         formattedMsg[len] = 0;
101     }
102
103     qvCHIP_Printf(kPrintfModuleLwip, formattedMsg);
104
105     // Let the application know that a log message has been emitted.
106     chip::DeviceLayer::OnLogOutput();
107 }
108
109 #if CHIP_DEVICE_CONFIG_ENABLE_THREAD
110 extern "C" void otPlatLog(otLogLevel aLogLevel, otLogRegion aLogRegion, const char * aFormat, ...)
111 {
112     char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
113
114     va_list v;
115
116     va_start(v, aFormat);
117     vsnprintf(formattedMsg, sizeof(formattedMsg), aFormat, v);
118     va_end(v);
119
120     qvCHIP_Printf(kPrintfModuleOpenThread, formattedMsg);
121
122     // Let the application know that a log message has been emitted.
123     chip::DeviceLayer::OnLogOutput();
124 }
125
126 // TODO: have qpg6100 openthread platform implementation defines the APIs.
127 // It is not perfect to have the openthread platform calloc/free
128 // APIs defined here. If a dedicated source file (e.g. Memory.cpp)
129 // that includes only the two functions is used, the target file
130 // Memory.o will be thrown away whening linking the libary because
131 // there is no one referring the symbols (We are not linking
132 // the 'platform' library against openthread).
133 extern "C" void * otPlatCAlloc(size_t aNum, size_t aSize)
134 {
135     return CHIPPlatformMemoryCalloc(aNum, aSize);
136 }
137
138 extern "C" void otPlatFree(void * aPtr)
139 {
140     CHIPPlatformMemoryFree(aPtr);
141 }
142 #endif // CHIP_DEVICE_CONFIG_ENABLE_THREAD