3 * Copyright (c) 2020 Project CHIP Authors
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
20 * Provides implementations for the CHIP logging functions
21 * on Nordic nRF Connect SDK platforms.
24 #include <platform/internal/CHIPDeviceLayerInternal.h>
25 #include <support/logging/CHIPLogging.h>
28 #include <logging/log.h>
32 using namespace ::chip;
33 using namespace ::chip::DeviceLayer;
34 using namespace ::chip::DeviceLayer::Internal;
36 LOG_MODULE_REGISTER(chip, LOG_LEVEL_DBG);
39 namespace DeviceLayer {
42 * Called whenever a log message is emitted by chip.
44 * This function is intended be overridden by the application to, e.g.,
45 * schedule output of queued log entries.
47 void __attribute__((weak)) OnLogOutput(void) {}
49 } // namespace DeviceLayer
54 * CHIP log output function.
57 void LogV(uint8_t module, uint8_t category, const char * msg, va_list v)
59 if (!IsCategoryEnabled(category))
63 char formattedMsg[CHIP_DEVICE_CONFIG_LOG_MESSAGE_MAX_SIZE];
66 // Max size for "[TAG] {UINT32}"
67 constexpr size_t maxPrefixLen = ChipLoggingModuleNameLen + 10 + 3;
68 static_assert(sizeof(formattedMsg) > maxPrefixLen);
70 prefixLen += snprintf(formattedMsg, sizeof(formattedMsg), "%u", k_uptime_get_32());
72 // Form the log prefix, e.g. "[DL] "
73 formattedMsg[prefixLen++] = '[';
74 GetModuleName(formattedMsg + prefixLen, module);
75 prefixLen = strlen(formattedMsg);
76 formattedMsg[prefixLen++] = ']';
77 formattedMsg[prefixLen++] = ' ';
79 // Append the log message.
80 vsnprintf(formattedMsg + prefixLen, sizeof(formattedMsg) - prefixLen, msg, v);
82 // Invoke the Zephyr logging library to log the message.
84 // Unfortunately the Zephyr logging macros end up assigning uint16_t
85 // variables to uint16_t:10 fields, which triggers integer conversion
86 // warnings. And treating the Zephyr headers as system headers does not
87 // help, apparently. Just turn off that warning around this switch.
88 #pragma GCC diagnostic push
89 #pragma GCC diagnostic ignored "-Wconversion"
92 case kLogCategory_Error:
93 LOG_ERR("%s", log_strdup(formattedMsg));
95 case kLogCategory_Progress:
96 case kLogCategory_Retain:
98 LOG_INF("%s", log_strdup(formattedMsg));
100 case kLogCategory_Detail:
101 LOG_DBG("%s", log_strdup(formattedMsg));
104 #pragma GCC diagnostic pop
107 // Let the application know that a log message has been emitted.
108 DeviceLayer::OnLogOutput();
111 } // namespace Logging