feat(ble): update log compression for bluedroid

(cherry picked from commit 05783fafd7)

Co-authored-by: luoxu <luoxu@espressif.com>
This commit is contained in:
Luo Xu
2026-03-02 09:43:17 +08:00
parent 381196774c
commit 0df48ce990
5 changed files with 67 additions and 27 deletions
+11
View File
@@ -1017,6 +1017,17 @@ if(CONFIG_BLE_COMPRESSED_LOG_ENABLE)
if(LOG_COMPRESSION_TARGET)
set(srcs ${LOG_COMPRESS_SRCS})
set(include_dirs ${LOG_COMPRESS_INCLUDE_DIRS})
# LOG_COMPRESS_FILES_WITH_FLAGS format: "file_path|compile_flags"
if(LOG_COMPRESS_FILES_WITH_FLAGS)
foreach(file_entry ${LOG_COMPRESS_FILES_WITH_FLAGS})
# Split by '|' to get file path and flags
string(REPLACE "|" ";" file_parts "${file_entry}")
list(GET file_parts 0 file_path)
list(GET file_parts 1 compile_flags)
set_source_files_properties(${file_path}
PROPERTIES COMPILE_FLAGS "${compile_flags}")
endforeach()
endif()
else()
list(APPEND include_dirs ${LOG_COMPRESS_INCLUDE_DIRS})
endif()
@@ -175,16 +175,18 @@ if(LOG_COMPRESSED_MODULE)
)
endif()
# List of files with their compile flags: format "file_path|flag1;flag2;..."
set(FILES_WITH_COMPILE_FLAGS "")
function(add_flags_if_in_list file file_list compile_flags)
set(PROCESSED OFF PARENT_SCOPE)
foreach(item IN LISTS file_list)
if(item STREQUAL file)
set_source_files_properties("${LOG_COMPRESSED_SRCS_DIR}/${file}"
PROPERTIES
GENERATED TRUE
COMPILE_FLAGS "${compile_flags}"
OBJECT_DEPENDS "${CMAKE_CURRENT_BINARY_DIR}/ble_log_compression"
)
set(abs_file_path "${LOG_COMPRESSED_SRCS_DIR}/${file}")
# Append to global list with format: "file_path|compile_flags"
list(APPEND FILES_WITH_COMPILE_FLAGS "${abs_file_path}|${compile_flags}")
set(FILES_WITH_COMPILE_FLAGS "${FILES_WITH_COMPILE_FLAGS}" PARENT_SCOPE)
message("set pro ${LOG_COMPRESSED_SRCS_DIR}/${file} ${compile_flags}")
set(PROCESSED ON PARENT_SCOPE)
break()
endif()
@@ -240,6 +242,7 @@ if(LOG_COMPRESSED_MODULE)
set(LOG_COMPRESSION_TARGET ble_log_compression PARENT_SCOPE)
# set(LOG_COMPRESSION_TARGET "" PARENT_SCOPE)
set(LOG_COMPRESS_SRCS "${compressed_srcs_with_abs_path};${uncompressed_srcs}" PARENT_SCOPE)
set(LOG_COMPRESS_FILES_WITH_FLAGS "${FILES_WITH_COMPILE_FLAGS}" PARENT_SCOPE)
if(NOT BLE_COMPRESSED_LIB_LOG_BUILD)
list(APPEND include_dirs "common/ble_log/extension/log_compression/include")
endif()
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
# SPDX-FileCopyrightText: 2025-2026 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
# The current project needs to support environments before Python 3.9,
# and UP007 will prohibit the use of Tuple, Union, etc.
@@ -279,7 +279,7 @@ class LogCompressor:
for node in log_nodes:
try:
log_info = self._process_log_node(node, function_boundaries)
if log_info:
if log_info and log_info['hexify']:
logs.append(log_info)
except Exception as e:
LOGGER.error(f'Error processing log node: {e}\n{traceback.format_exc()}')
@@ -335,13 +335,23 @@ class LogCompressor:
# Parse format tokens
tokens = parse_format_string(f'"{log_fmt}"')
tokens_tuple_map: list[int] = []
need_args = 0
for idx, tk in enumerate(tokens):
if isinstance(tk, tuple):
tokens_tuple_map.append(idx)
need_args = need_args + 1
if tk[4] == '*': # dynamic width
need_args = need_args + 1
log_info['hexify'] = False
return log_info
if tk[5] == '*': # dynamic precision
need_args = need_args + 1
log_info['hexify'] = False
return log_info
arguments: list[Node] = valid_arg_childrn[1:]
if len(arguments) != len(tokens_tuple_map):
if len(arguments) != need_args:
raise SyntaxError(f'LogSyntaxError:{node.text.decode("utf-8")}')
# Process each argument
@@ -1,4 +1,4 @@
# SPDX-FileCopyrightText: 2025 Espressif Systems (Shanghai) CO LTD
# SPDX-FileCopyrightText: 2025-2026 Espressif Systems (Shanghai) CO LTD
# SPDX-License-Identifier: Apache-2.0
# ruff: noqa: UP007
"""
@@ -10,7 +10,7 @@ import struct
from typing import Union
def parse_format_string(format_str: str) -> list[Union[str, tuple[int, int, str, str, str, str, str]]]:
def parse_format_string(format_str: str) -> list[Union[str, tuple[int, int, str, str, str, str, str, str]]]:
"""
Parse a format string into tokens.
@@ -19,8 +19,9 @@ def parse_format_string(format_str: str) -> list[Union[str, tuple[int, int, str,
Returns:
List of tokens (strings or format spec tuples)
Tuple format: (start, end, full_spec, flags, width, precision, length, conv_char)
"""
tokens: list[Union[str, tuple[int, int, str, str, str, str, str]]] = []
tokens: list[Union[str, tuple[int, int, str, str, str, str, str, str]]] = []
i = 0
n = len(format_str)
@@ -41,36 +42,51 @@ def parse_format_string(format_str: str) -> list[Union[str, tuple[int, int, str,
flags += format_str[i]
i += 1
# Parse width
# Parse width (including dynamic width '*')
width = ''
while i < n and format_str[i].isdigit():
width += format_str[i]
if i < n and format_str[i] == '*':
width = '*'
i += 1
else:
while i < n and format_str[i].isdigit():
width += format_str[i]
i += 1
# Parse precision
# Parse precision (including dynamic precision '*')
precision = ''
if i < n and format_str[i] == '.':
i += 1
while i < n and format_str[i].isdigit():
precision += format_str[i]
if i < n and format_str[i] == '*':
precision = '*'
i += 1
else:
while i < n and format_str[i].isdigit():
precision += format_str[i]
i += 1
# Parse length modifier
# Parse length modifier (supports h, hh, l, ll, j, z, t)
length = ''
if i < n and format_str[i] in 'zhl':
if i < n and format_str[i] in 'jzt':
# j, z, t are single-character length modifiers
length += format_str[i]
i += 1
# Handle double length (e.g., ll)
if i < n and format_str[i] == 'l' and length == 'l':
length += 'l'
elif i < n and format_str[i] in 'hl':
length += format_str[i]
i += 1
# Handle double length (e.g., hh, ll)
if i < n and format_str[i] == length[0]:
length += format_str[i]
i += 1
if i < n and format_str[i] in 'diuoxXfcsplL':
# Parse conversion character
# Supports: d, i, u, o, x, X, f, F, e, E, g, G, a, A, c, s, p, n, L
if i < n and format_str[i] in 'diuoxXfFeEgGaAcspnL':
conv_char = format_str[i]
i += 1
full_spec = format_str[start:i]
tokens.append((start, i, full_spec, flags, width, length, conv_char))
tokens.append((start, i, full_spec, flags, width, precision, length, conv_char))
else:
# Invalid format spec, treat as literal text
tokens.append(format_str[start:i])
else:
# Regular text
@@ -200,7 +216,7 @@ def parse_compressed_arguments(byte_sequence: bytes, format_str: str) -> str:
for token in tokens:
if isinstance(token, tuple):
start, end, flags, width, precision, length_mod, conv_char = token
start, end, full_spec, flags, width, precision, length_mod, conv_char = token
if conv_char == '%':
output.append('%')
@@ -48,7 +48,7 @@ def gen_compressed_stmt(
stmt += '\\\n'
return ' ' + generate_bluedroid_log_prefix(func_name, stmt)
size_str = ', '.join([arg['size_type'] for arg in args])
args_str = ', '.join([arg['name'] for arg in args]).replace('\n', '')
args_str = ', '.join([arg['name'] for arg in args]).replace('\\\n', '').replace('\n', '')
stmt = f' ble_log_compressed_hex_print({module_id},{log_index}, {len(args)}, {size_str}, {args_str});'
for idx, buffer_arg in enumerate(buffer_args):
stmt += '\\\n'