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