-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Split apart text-only logging and binary logging.
Text-only logging does not depend on Span, but binary logging (LogByteSpan) does. And Span.h depends on text-only logging. We were manually breaking the cycle caused by having both logging types in CHIPLogging.h by not including Span.h from CHIPLogging.h and forward-declaring Span instead. This fixes things so that we have a TextOnlyLogging.h (which is used by Span.h, via CodeUtils.h, and does not use Span at all), and a BinaryLogging.h which uses Span and includes Span.h.
- Loading branch information
1 parent
52225dd
commit 17af39e
Showing
9 changed files
with
582 additions
and
485 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
/* | ||
* Copyright (c) 2020-2023 Project CHIP Authors | ||
* Copyright (c) 2013-2017 Nest Labs, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
#include "BinaryLogging.h" | ||
|
||
namespace chip { | ||
namespace Logging { | ||
|
||
#if _CHIP_USE_LOGGING | ||
|
||
void LogByteSpan(uint8_t module, uint8_t category, const ByteSpan & span) | ||
{ | ||
// Maximum number of characters needed to print 8 byte buffer including formatting (0x) | ||
// 8 bytes * (2 nibbles per byte + 4 character for ", 0x") + null termination. | ||
// Rounding up to 50 bytes. | ||
char output[50]; | ||
size_t offset = 0; | ||
for (unsigned int i = 0; i < span.size(); i++) | ||
{ | ||
if (i % 8 == 0 && offset != 0) | ||
{ | ||
Log(module, category, "%s", output); | ||
offset = 0; | ||
} | ||
int result = snprintf(&output[offset], sizeof(output) - offset, "0x%02x, ", (unsigned char) span.data()[i]); | ||
if (result > 0) | ||
{ | ||
offset += static_cast<size_t>(result); | ||
} | ||
else | ||
{ | ||
Log(module, Logging::kLogCategory_Error, "Failed to print ByteSpan buffer"); | ||
return; | ||
} | ||
} | ||
if (offset != 0) | ||
{ | ||
Log(module, category, "%s", output); | ||
} | ||
} | ||
|
||
#endif // _CHIP_USE_LOGGING | ||
|
||
} // namespace Logging | ||
} // namespace chip |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
/* | ||
* Copyright (c) 2020-2023 Project CHIP Authors | ||
* Copyright (c) 2013-2017 Nest Labs, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
/** | ||
* @file | ||
* This file defines macros, constants, and interfaces for logging binary | ||
* data (represented by ByteSpan). | ||
*/ | ||
|
||
#pragma once | ||
|
||
#include <lib/core/CHIPConfig.h> | ||
|
||
#include <lib/support/Span.h> | ||
#include <lib/support/logging/TextOnlyLogging.h> | ||
|
||
namespace chip { | ||
namespace Logging { | ||
|
||
#if CHIP_DETAIL_LOGGING | ||
|
||
/** | ||
* @def ChipLogByteSpan(MOD, DATA) | ||
* | ||
* @brief | ||
* Log a byte span for the specified module in the 'Detail' category. | ||
* | ||
*/ | ||
#define ChipLogByteSpan(MOD, DATA) ChipInternalLogByteSpan(MOD, DETAIL, DATA) | ||
|
||
#else // CHIP_DETAIL_LOGGING | ||
#define ChipLogByteSpan(MOD, DATA) ((void) 0) | ||
#endif // CHIP_DETAIL_LOGGING | ||
|
||
// _CHIP_USE_LOGGING is defined in TextOnlyLogging.h | ||
#if _CHIP_USE_LOGGING | ||
|
||
void LogByteSpan(uint8_t module, uint8_t category, const ByteSpan & span); | ||
|
||
#if CHIP_SYSTEM_CONFIG_PLATFORM_LOG | ||
#ifndef ChipPlatformLogByteSpan | ||
#error "CHIP_SYSTEM_CONFIG_PLATFORM_LOG is enabled but ChipPlatformLogByteSpan() is not defined" | ||
#endif | ||
#define ChipInternalLogByteSpan(...) ChipPlatformLogByteSpan(__VA_ARGS__) | ||
#else // CHIP_SYSTEM_CONFIG_PLATFORM_LOG | ||
#define ChipInternalLogByteSpan(MOD, CAT, DATA) \ | ||
if (CHIP_CONFIG_LOG_MODULE_##MOD && IsModuleCategoryEnabled(MOD, CAT)) \ | ||
{ \ | ||
ChipInternalLogByteSpanImpl(MOD, CHIP_LOG_CATEGORY_##CAT, DATA); \ | ||
} | ||
#endif // CHIP_SYSTEM_CONFIG_PLATFORM_LOG | ||
|
||
#define ChipInternalLogByteSpanImpl(MOD, CAT, DATA) \ | ||
do \ | ||
{ \ | ||
if (chip::Logging::IsCategoryEnabled(CAT)) \ | ||
{ \ | ||
chip::Logging::LogByteSpan(chip::Logging::kLogModule_##MOD, CAT, DATA); \ | ||
} \ | ||
} while (0) | ||
|
||
#endif // _CHIP_USE_LOGGING | ||
|
||
} // namespace Logging | ||
} // namespace chip |
Oops, something went wrong.