Matter SDK Coverage Report
Current view: top level - lib/support/logging - TextOnlyLogging.cpp (source / functions) Coverage Total Hit
Test: SHA:4cbce7f768f16e614f5a8ccb8cd93c92cbeae70d Lines: 87.5 % 24 21
Test Date: 2025-04-26 07:09:35 Functions: 85.7 % 7 6

            Line data    Source code
       1              : /*
       2              :  *    Copyright (c) 2020-2023 Project CHIP Authors
       3              :  *    Copyright (c) 2013-2017 Nest Labs, Inc.
       4              :  *
       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
       8              :  *
       9              :  *        http://www.apache.org/licenses/LICENSE-2.0
      10              :  *
      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.
      16              :  */
      17              : 
      18              : /**
      19              :  *    @file
      20              :  *      This file implements macros, constants, and interfaces for a
      21              :  *      platform-independent logging interface for the chip SDK.
      22              :  *
      23              :  */
      24              : 
      25              : #include "TextOnlyLogging.h"
      26              : 
      27              : #include <lib/core/CHIPConfig.h>
      28              : #include <lib/support/CHIPMem.h>
      29              : 
      30              : #include <platform/logging/LogV.h>
      31              : 
      32              : #include <stdarg.h>
      33              : #include <stdio.h>
      34              : #include <string.h>
      35              : 
      36              : #include <atomic>
      37              : 
      38              : #if CHIP_PW_TOKENIZER_LOGGING
      39              : #include "pw_tokenizer/encode_args.h"
      40              : #endif
      41              : 
      42              : namespace chip {
      43              : namespace Logging {
      44              : 
      45              : #if _CHIP_USE_LOGGING
      46              : 
      47              : #if CHIP_PW_TOKENIZER_LOGGING
      48              : 
      49              : void HandleTokenizedLog(uint32_t levels, pw_tokenizer_Token token, pw_tokenizer_ArgTypes types, ...)
      50              : {
      51              :     uint8_t encoded_message[PW_TOKENIZER_CFG_ENCODING_BUFFER_SIZE_BYTES];
      52              : 
      53              :     memcpy(encoded_message, &token, sizeof(token));
      54              : 
      55              :     va_list args;
      56              :     va_start(args, types);
      57              :     // Use the C argument encoding API, since the C++ API requires C++17.
      58              :     const size_t encoded_size = sizeof(token) +
      59              :         pw_tokenizer_EncodeArgs(types, args, encoded_message + sizeof(token), sizeof(encoded_message) - sizeof(token));
      60              :     va_end(args);
      61              : 
      62              :     uint8_t log_category  = levels >> 8 & 0xFF;
      63              :     uint8_t log_module    = levels & 0xFF;
      64              :     char * logging_buffer = nullptr;
      65              : 
      66              :     // To reduce the number of alloc/free that is happening we will use a stack
      67              :     // buffer when buffer required to log is small.
      68              :     char stack_buffer[32];
      69              :     char * allocated_buffer     = nullptr;
      70              :     size_t required_buffer_size = 2 * encoded_size + 1;
      71              : 
      72              :     if (required_buffer_size > sizeof(stack_buffer))
      73              :     {
      74              :         allocated_buffer = (char *) chip::Platform::MemoryAlloc(required_buffer_size);
      75              :         if (allocated_buffer)
      76              :         {
      77              :             logging_buffer = allocated_buffer;
      78              :         }
      79              :     }
      80              :     else
      81              :     {
      82              :         logging_buffer = stack_buffer;
      83              :     }
      84              : 
      85              :     if (logging_buffer)
      86              :     {
      87              :         for (size_t i = 0; i < encoded_size; i++)
      88              :         {
      89              :             sprintf(logging_buffer + 2 * i, "%02x", encoded_message[i]);
      90              :         }
      91              :         logging_buffer[2 * encoded_size] = '\0';
      92              :         Log(log_module, log_category, "%s", logging_buffer);
      93              :     }
      94              :     if (allocated_buffer)
      95              :     {
      96              :         chip::Platform::MemoryFree(allocated_buffer);
      97              :     }
      98              : }
      99              : 
     100              : #endif
     101              : 
     102              : namespace {
     103              : 
     104              : std::atomic<LogRedirectCallback_t> sLogRedirectCallback{ nullptr };
     105              : 
     106              : /*
     107              :  * Array of strings containing the short names for each of the chip log modules.
     108              :  */
     109              : static const char ModuleNames[kLogModule_Max][kMaxModuleNameLen + 1] = {
     110              : #define _CHIP_LOGMODULE_NAME_INIT(MOD, NAME, ...) NAME,
     111              :     CHIP_LOGMODULES_ENUMERATE(_CHIP_LOGMODULE_NAME_INIT)
     112              : };
     113              : 
     114              : } // namespace
     115              : 
     116      1293887 : const char * GetModuleName(LogModule module)
     117              : {
     118      1293887 :     return ModuleNames[(module < kLogModule_Max) ? module : kLogModule_NotSpecified];
     119              : }
     120              : 
     121           10 : void SetLogRedirectCallback(LogRedirectCallback_t callback)
     122              : {
     123           10 :     sLogRedirectCallback.store(callback);
     124           10 : }
     125              : 
     126              : /**
     127              :  * Log, to the platform-specified mechanism, the specified log
     128              :  * message, @a msg, for the specified module, @a module, in the
     129              :  * provided category, @a category.
     130              :  *
     131              :  * @param[in] module    A LogModule enumeration indicating the
     132              :  *                      source of the chip package module that
     133              :  *                      generated the log message. This must be
     134              :  *                      translated within the function to a module
     135              :  *                      name for inclusion in the log message.
     136              :  * @param[in] category  A LogCategory enumeration indicating the
     137              :  *                      category of the log message. The category
     138              :  *                      may be filtered in or out if
     139              :  *                      CHIP_LOG_FILTERING was asserted.
     140              :  * @param[in] msg       A pointer to a NULL-terminated C string with
     141              :  *                      C Standard Library-style format specifiers
     142              :  *                      containing the log message to be formatted and
     143              :  *                      logged.
     144              :  * @param[in] ...       A variadic argument list whose elements should
     145              :  *                      correspond to the format specifiers in @a msg.
     146              :  *
     147              :  */
     148      1293887 : void Log(uint8_t module, uint8_t category, const char * msg, ...)
     149              : {
     150              : 
     151              :     va_list v;
     152      1293887 :     va_start(v, msg);
     153      1293887 :     LogV(module, category, msg, v);
     154      1293887 :     va_end(v);
     155      1293887 : }
     156              : 
     157      1293887 : void LogV(uint8_t module, uint8_t category, const char * msg, va_list args)
     158              : {
     159      1293887 :     const char * moduleName        = GetModuleName(static_cast<LogModule>(module));
     160      1293887 :     LogRedirectCallback_t redirect = sLogRedirectCallback.load();
     161      1293887 :     if (redirect != nullptr)
     162              :     {
     163           11 :         redirect(moduleName, category, msg, args);
     164              :     }
     165              :     else
     166              :     {
     167      1293876 :         Platform::LogV(moduleName, category, msg, args);
     168              :     }
     169      1293887 : }
     170              : 
     171              : #if CHIP_LOG_FILTERING
     172              : std::atomic<uint8_t> gLogFilter(kLogCategory_Max);
     173              : 
     174      1293882 : uint8_t GetLogFilter()
     175              : {
     176      1293882 :     return gLogFilter.load();
     177              : }
     178              : 
     179            0 : void SetLogFilter(uint8_t category)
     180              : {
     181            0 :     gLogFilter.store(category);
     182            0 : }
     183              : 
     184      1293882 : bool IsCategoryEnabled(uint8_t category)
     185              : {
     186      1293882 :     return (category <= GetLogFilter());
     187              : }
     188              : #endif // CHIP_LOG_FILTERING
     189              : 
     190              : #endif // _CHIP_USE_LOGGING
     191              : 
     192              : } // namespace Logging
     193              : } // namespace chip
        

Generated by: LCOV version 2.0-1