Skip to content

Commit

Permalink
[logging] Switch tokenizer logging to own handler
Browse files Browse the repository at this point in the history
Pigweed's pw_tokenizer:global_handler_with_payload facade is deprecated.
Each pw_tokenizer should define its own handler function instead. This
gives each pw_tokenizer user more control over the handler function and
allows multiple subsystems to use pw_tokenizer in whichever way works
best.

This change updates the tokenized version of the ChipInternalLogImpl
macro to function as described at
https://pigweed.dev/pw_tokenizer/#tokenize-a-message-with-arguments-in-a-custom-macro.

This commit updates the Pigweed submodule to the latest version, which
brings in changes needed to use a custom pw_tokenizer macro in C++14.
  • Loading branch information
255 committed Mar 6, 2023
1 parent d44b6a6 commit f32d321
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 15 deletions.
2 changes: 1 addition & 1 deletion src/lib/support/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -216,7 +216,7 @@ static_library("support") {
}

if (chip_pw_tokenizer_logging) {
public_deps += [ "${dir_pw_tokenizer}:global_handler_with_payload" ]
public_deps += [ "${dir_pw_tokenizer}" ]
}

if (chip_config_memory_debug_dmalloc) {
Expand Down
32 changes: 22 additions & 10 deletions src/lib/support/logging/CHIPLogging.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,33 +38,45 @@

#include <atomic>

#if CHIP_PW_TOKENIZER_LOGGING
#include "pw_tokenizer/encode_args.h"
#endif

namespace chip {
namespace Logging {

#if _CHIP_USE_LOGGING

#if CHIP_PW_TOKENIZER_LOGGING

extern "C" void pw_tokenizer_HandleEncodedMessageWithPayload(uintptr_t levels, const uint8_t encoded_message[], size_t size_bytes)
void HandleTokenizedLog(uint32_t levels, pw_tokenizer_Token token, pw_tokenizer_ArgTypes types, ...)
{
uint8_t encoded_message[PW_TOKENIZER_CFG_ENCODING_BUFFER_SIZE_BYTES];

va_list args;
va_start(args, types);
// Use the C argument encoding API, since the C++ API requires C++17.
const size_t encoded_size = pw_tokenizer_EncodeArgs(types, args, encoded_message, sizeof(encoded_message));
va_end(args);

uint8_t log_category = levels >> 8 & 0xFF;
uint8_t log_module = levels & 0xFF;
char * buffer = (char *) chip::Platform::MemoryAlloc(2 * size_bytes + 1);
char * buffer = (char *) chip::Platform::MemoryAlloc(2 * encoded_size + 1);

if (buffer)
{
for (int i = 0; i < size_bytes; i++)
for (int i = 0; i < encoded_size; i++)
{
sprintf(buffer + 2 * i, "%02x", encoded_message[i]);
}
buffer[2 * size_bytes] = '\0';
chip::Logging::Log(log_module, log_category, "%s", buffer);
buffer[2 * encoded_size] = '\0';
Log(log_module, log_category, "%s", buffer);
chip::Platform::MemoryFree(buffer);
}
}

#endif

namespace chip {
namespace Logging {

#if _CHIP_USE_LOGGING

namespace {

std::atomic<LogRedirectCallback_t> sLogRedirectCallback{ nullptr };
Expand Down
10 changes: 7 additions & 3 deletions src/lib/support/logging/CHIPLogging.h
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@
#endif

#if CHIP_PW_TOKENIZER_LOGGING
#include "pw_tokenizer/tokenize_to_global_handler_with_payload.h"
#include "pw_tokenizer/tokenize.h"
#endif

/**
Expand Down Expand Up @@ -423,13 +423,17 @@ DLL_LOCAL void LogV(uint8_t module, uint8_t category, const char * msg, va_list
#endif // CHIP_SYSTEM_CONFIG_PLATFORM_LOG

#if CHIP_PW_TOKENIZER_LOGGING

DLL_LOCAL void HandleTokenizedLog(uint32_t levels, pw_tokenizer_Token token, pw_tokenizer_ArgTypes, ...);

#define ChipInternalLogImpl(MOD, CAT, MSG, ...) \
do \
{ \
if (chip::Logging::IsCategoryEnabled(CAT)) \
{ \
PW_TOKENIZE_TO_GLOBAL_HANDLER_WITH_PAYLOAD((pw_tokenizer_Payload)((CAT << 8) | chip::Logging::kLogModule_##MOD), MSG, \
__VA_ARGS__); \
PW_TOKENIZE_FORMAT_STRING(PW_TOKENIZER_DEFAULT_DOMAIN, UINT32_MAX, MSG, __VA_ARGS__); \
::chip::Logging::HandleTokenizedLog((uint32_t)((CAT << 8) | chip::Logging::kLogModule_##MOD), _pw_tokenizer_token, \
PW_TOKENIZER_ARG_TYPES(__VA_ARGS__) PW_COMMA_ARGS(__VA_ARGS__)); \
} \
} while (0)
#else // CHIP_PW_TOKENIZER_LOGGING
Expand Down
2 changes: 1 addition & 1 deletion third_party/pigweed/repo
Submodule repo updated 505 files

0 comments on commit f32d321

Please sign in to comment.