Fix for x86_64 build fail
[platform/upstream/connectedhomeip.git] / src / platform / Zephyr / Logging.cpp
1 /* See Project CHIP LICENSE file for licensing information. */
2
3 #include <platform/logging/LogV.h>
4
5 #include <core/CHIPConfig.h>
6 #include <support/logging/Constants.h>
7
8 #include <kernel.h>
9 #include <logging/log.h>
10 #include <sys/cbprintf.h>
11
12 LOG_MODULE_REGISTER(chip, LOG_LEVEL_DBG);
13
14 namespace chip {
15 namespace DeviceLayer {
16
17 /**
18  * Called whenever a log message is emitted by chip.
19  *
20  * This function is intended be overridden by the application to, e.g.,
21  * schedule output of queued log entries.
22  */
23 void __attribute__((weak)) OnLogOutput(void) {}
24
25 } // namespace DeviceLayer
26
27 namespace Logging {
28 namespace Platform {
29
30 /**
31  * CHIP log output function.
32  */
33
34 void LogV(const char * module, uint8_t category, const char * msg, va_list v)
35 {
36     char formattedMsg[CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE];
37
38     snprintfcb(formattedMsg, sizeof(formattedMsg), "%u [%s]", k_uptime_get_32(), module);
39
40     // -2 to ensure at least one byte available for vsnprintf below.
41     formattedMsg[sizeof(formattedMsg) - 2] = 0;
42
43     size_t prefixLen = strlen(formattedMsg);
44
45     // Append the log message.
46     vsnprintfcb(formattedMsg + prefixLen, sizeof(formattedMsg) - prefixLen, msg, v);
47
48     // Invoke the Zephyr logging library to log the message.
49     //
50     // Unfortunately the Zephyr logging macros end up assigning uint16_t
51     // variables to uint16_t:10 fields, which triggers integer conversion
52     // warnings.  And treating the Zephyr headers as system headers does not
53     // help, apparently.  Just turn off that warning around this switch.
54 #pragma GCC diagnostic push
55 #pragma GCC diagnostic ignored "-Wconversion"
56     switch (category)
57     {
58     case kLogCategory_Error:
59         LOG_ERR("%s", log_strdup(formattedMsg));
60         break;
61     case kLogCategory_Progress:
62     default:
63         LOG_INF("%s", log_strdup(formattedMsg));
64         break;
65     case kLogCategory_Detail:
66         LOG_DBG("%s", log_strdup(formattedMsg));
67         break;
68     }
69 #pragma GCC diagnostic pop
70
71     // Let the application know that a log message has been emitted.
72     DeviceLayer::OnLogOutput();
73 }
74
75 } // namespace Platform
76 } // namespace Logging
77 } // namespace chip