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 : #include "BinaryLogging.h"
19 :
20 : #include <cstdio>
21 :
22 : namespace chip {
23 : namespace Logging {
24 :
25 : #if _CHIP_USE_LOGGING
26 :
27 0 : void LogByteSpan(uint8_t module, uint8_t category, const ByteSpan & span)
28 : {
29 : // Maximum number of characters needed to print 8 byte buffer including formatting (0x)
30 : // 8 bytes * (2 nibbles per byte + 4 character for ", 0x") + null termination.
31 : // Rounding up to 50 bytes.
32 : char output[50];
33 0 : size_t offset = 0;
34 0 : for (unsigned int i = 0; i < span.size(); i++)
35 : {
36 0 : if (i % 8 == 0 && offset != 0)
37 : {
38 0 : Log(module, category, "%s", output);
39 0 : offset = 0;
40 : }
41 0 : int result = snprintf(&output[offset], sizeof(output) - offset, "0x%02x, ", (unsigned char) span.data()[i]);
42 0 : if (result > 0)
43 : {
44 0 : offset += static_cast<size_t>(result);
45 : }
46 : else
47 : {
48 0 : Log(module, Logging::kLogCategory_Error, "Failed to print ByteSpan buffer");
49 0 : return;
50 : }
51 : }
52 0 : if (offset != 0)
53 : {
54 0 : Log(module, category, "%s", output);
55 : }
56 : }
57 :
58 : #endif // _CHIP_USE_LOGGING
59 :
60 : } // namespace Logging
61 : } // namespace chip
|