3 * Copyright (c) 2020 Project CHIP Authors
4 * Copyright (c) 2013-2017 Nest Labs, Inc.
6 * Licensed under the Apache License, Version 2.0 (the "License");
7 * you may not use this file except in compliance with the License.
8 * You may obtain a copy of the License at
10 * http://www.apache.org/licenses/LICENSE-2.0
12 * Unless required by applicable law or agreed to in writing, software
13 * distributed under the License is distributed on an "AS IS" BASIS,
14 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 * See the License for the specific language governing permissions and
16 * limitations under the License.
21 * This file implements macros, constants, and interfaces for a
22 * platform-independent logging interface for the chip SDK.
26 #include "CHIPLogging.h"
28 #include <core/CHIPCore.h>
29 #include <support/CodeUtils.h>
30 #include <support/DLLUtil.h>
45 std::atomic<LogRedirectCallback_t> sLogRedirectCallback{ nullptr };
50 * Array of strings containing the names for each of the chip log
53 * NOTE: The names must be in the order defined in the LogModule
54 * enumeration. Each name must be a fixed number of characters
55 * long (chip::Logging::kMaxModuleNameLen) padded with nulls as
59 static const char ModuleNames[] = "-\0\0" // None
62 "ML\0" // MessageLayer
63 "SM\0" // SecurityManager
64 "EM\0" // ExchangeManager
70 "BDX" // BulkDataTransfer
71 "DMG" // DataManagement
72 "DC\0" // DeviceControl
73 "DD\0" // DeviceDescription
75 "FP\0" // FabricProvisioning
76 "NP\0" // NetworkProvisioning
77 "SD\0" // ServiceDirectory
78 "SP\0" // ServiceProvisioning
79 "SWU" // SoftwareUpdate
80 "TP\0" // TokenPairing
81 "TS\0" // TimeServices
83 "CSL" // chipSystemLayer
84 "EVL" // Event Logging
95 #define ModuleNamesCount ((sizeof(ModuleNames) - 1) / chip::Logging::kMaxModuleNameLen)
97 void GetModuleName(char * buf, uint8_t bufSize, uint8_t module)
99 const char * moduleNamePtr = ModuleNames + ((module < ModuleNamesCount) ? module * chip::Logging::kMaxModuleNameLen : 0);
101 snprintf(buf, bufSize, "%s", moduleNamePtr);
102 buf[chip::Logging::kMaxModuleNameLen] = 0;
105 void SetLogRedirectCallback(LogRedirectCallback_t callback)
107 sLogRedirectCallback.store(callback);
111 * Log, to the platform-specified mechanism, the specified log
112 * message, @a msg, for the specified module, @a module, in the
113 * provided category, @a category.
115 * @param[in] module A LogModule enumeration indicating the
116 * source of the chip package module that
117 * generated the log message. This must be
118 * translated within the function to a module
119 * name for inclusion in the log message.
120 * @param[in] category A LogCategory enumeration indicating the
121 * category of the log message. The category
122 * may be filtered in or out if
123 * CHIP_LOG_FILTERING was asserted.
124 * @param[in] msg A pointer to a NULL-terminated C string with
125 * C Standard Library-style format specifiers
126 * containing the log message to be formatted and
128 * @param[in] ... A variadic argument list whose elements should
129 * correspond to the format specifiers in @a msg.
132 DLL_EXPORT void Log(uint8_t module, uint8_t category, const char * msg, ...)
137 LogV(module, category, msg, v);
141 void LogV(uint8_t module, uint8_t category, const char * msg, va_list args)
143 if (!IsCategoryEnabled(category))
148 char moduleName[chip::Logging::kMaxModuleNameLen + 1];
149 GetModuleName(moduleName, sizeof(moduleName), module);
151 LogRedirectCallback_t redirect = sLogRedirectCallback.load();
153 if (redirect != nullptr)
155 redirect(moduleName, category, msg, args);
159 Platform::LogV(moduleName, category, msg, args);
163 #if CHIP_LOG_FILTERING
164 uint8_t gLogFilter = kLogCategory_Max;
165 DLL_EXPORT bool IsCategoryEnabled(uint8_t category)
167 return (category <= gLogFilter);
170 DLL_EXPORT uint8_t GetLogFilter()
175 DLL_EXPORT void SetLogFilter(uint8_t category)
177 gLogFilter = category;
180 #else // CHIP_LOG_FILTERING
182 DLL_EXPORT bool IsCategoryEnabled(uint8_t category)
188 DLL_EXPORT uint8_t GetLogFilter()
190 return kLogCategory_Max;
193 DLL_EXPORT void SetLogFilter(uint8_t category)
197 #endif // CHIP_LOG_FILTERING
199 #endif /* _CHIP_USE_LOGGING */
201 } // namespace Logging