Skip to content

Commit

Permalink
Split apart text-only logging and binary logging. (#29661)
Browse files Browse the repository at this point in the history
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
bzbarsky-apple authored and pull[bot] committed Nov 10, 2023
1 parent df6b00a commit 4555314
Show file tree
Hide file tree
Showing 10 changed files with 583 additions and 486 deletions.
2 changes: 1 addition & 1 deletion examples/common/pigweed/RpcService.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
#include "pw_stream/sys_io_stream.h"
#include "pw_sys_io/sys_io.h"

#include <lib/support/logging/CHIPLogging.h>
#include <lib/support/logging/TextOnlyLogging.h>

#include <array>

Expand Down
2 changes: 1 addition & 1 deletion examples/platform/ameba/PigweedLogger.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "FreeRTOS.h"
#include "pw_sys_io_ameba/init.h"
#include "semphr.h"
#include <lib/support/logging/CHIPLogging.h>
#include <lib/support/logging/TextOnlyLogging.h>
#include <pw_hdlc/encoder.h>
#include <pw_stream/sys_io_stream.h>

Expand Down
1 change: 1 addition & 0 deletions examples/platform/silabs/SilabsDeviceAttestationCreds.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <lib/core/CHIPError.h>
#include <lib/support/CodeUtils.h>
#include <lib/support/Span.h>
#include <lib/support/logging/CHIPLogging.h>
#include <platform/silabs/SilabsConfig.h>
#include <psa/crypto.h>

Expand Down
5 changes: 4 additions & 1 deletion src/lib/support/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -140,8 +140,11 @@ static_library("support") {
"Variant.h",
"ZclString.cpp",
"ZclString.h",
"logging/CHIPLogging.cpp",
"logging/BinaryLogging.cpp",
"logging/BinaryLogging.h",
"logging/CHIPLogging.h",
"logging/TextOnlyLogging.cpp",
"logging/TextOnlyLogging.h",
"verhoeff/Verhoeff.cpp",
"verhoeff/Verhoeff.h",
"verhoeff/Verhoeff10.cpp",
Expand Down
2 changes: 1 addition & 1 deletion src/lib/support/CodeUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
#include <lib/core/CHIPConfig.h>
#include <lib/core/CHIPError.h>
#include <lib/core/ErrorStr.h>
#include <lib/support/logging/CHIPLogging.h>
#include <lib/support/logging/TextOnlyLogging.h>

/**
* Base-level abnormal termination.
Expand Down
59 changes: 59 additions & 0 deletions src/lib/support/logging/BinaryLogging.cpp
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
79 changes: 79 additions & 0 deletions src/lib/support/logging/BinaryLogging.h
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

0 comments on commit 4555314

Please sign in to comment.