3 * Copyright (c) 2020-2021 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.
21 #include "debug-printing.h"
23 #include <platform/CHIPDeviceConfig.h>
24 #include <support/CodeUtils.h>
25 #include <support/logging/CHIPLogging.h>
27 bool emberAfPrintReceivedMessages = true;
29 using namespace chip::Logging;
31 void emberAfPrint(int category, const char * format, ...)
33 if (format != nullptr)
36 va_start(args, format);
37 chip::Logging::LogV(chip::Logging::kLogModule_Zcl, chip::Logging::kLogCategory_Progress, format, args);
42 void emberAfPrintln(int category, const char * format, ...)
44 if (format != nullptr)
47 va_start(args, format);
48 chip::Logging::LogV(chip::Logging::kLogModule_Zcl, chip::Logging::kLogCategory_Progress, format, args);
53 // TODO: add unit tests.
55 void emberAfPrintBuffer(int category, const uint8_t * buffer, uint16_t length, bool withSpace)
57 if (buffer != nullptr && length > 0)
59 constexpr uint16_t kBufferSize = CHIP_CONFIG_LOG_MESSAGE_MAX_SIZE;
60 const char * perByteFormatStr = withSpace ? "%02X " : "%02X";
61 const uint8_t perByteCharCount = withSpace ? 3 : 2;
62 const uint16_t bytesPerBuffer = static_cast<uint16_t>((kBufferSize - 1) / perByteCharCount);
63 char result[kBufferSize];
66 while (index < length)
68 const uint16_t remainingBytes = static_cast<uint16_t>(length - index);
69 const uint16_t segmentLength = chip::min(bytesPerBuffer, remainingBytes);
70 const uint16_t segmentEnd = static_cast<uint16_t>(index + segmentLength);
71 const uint32_t outStringEnd = segmentLength * perByteCharCount;
72 for (uint32_t dst_idx = 0; dst_idx < outStringEnd && index < segmentEnd; dst_idx += perByteCharCount, index++)
74 snprintf(result + dst_idx, outStringEnd - dst_idx + 1, perByteFormatStr, buffer[index]);
76 result[outStringEnd] = 0;
77 emberAfPrint(category, "%s", result);
82 emberAfPrint(EMBER_AF_PRINT_CORE, "NULL");
86 void emberAfPrintString(int category, const uint8_t * string)
88 emberAfPrint(category, "%s", string);