Skip to content

Commit db956c1

Browse files
committed
diag: Support to use externally wrapped logging APIs
1 parent 56eec5c commit db956c1

File tree

4 files changed

+79
-18
lines changed

4 files changed

+79
-18
lines changed

components/esp_diagnostics/CMakeLists.txt

+3-1
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,9 @@ idf_component_register(SRCS "${srcs}"
3434
INCLUDE_DIRS "include"
3535
PRIV_REQUIRES ${priv_req})
3636

37-
set(WRAP_FUNCTIONS esp_log_write esp_log_writev)
37+
if(NOT CONFIG_DIAG_USE_EXTERNAL_LOG_WRAP)
38+
set(WRAP_FUNCTIONS esp_log_write esp_log_writev)
39+
endif()
3840

3941
if(CONFIG_LIB_BUILDER_COMPILE)
4042
list(APPEND WRAP_FUNCTIONS log_printf)

components/esp_diagnostics/Kconfig

+8
Original file line numberDiff line numberDiff line change
@@ -96,4 +96,12 @@ menu "Diagnostics"
9696
default n
9797
help
9898
Enable more advanced network variables
99+
100+
config DIAG_USE_EXTERNAL_LOG_WRAP
101+
bool "Use external log wrapper"
102+
default n
103+
help
104+
Diagnostics component wraps the esp_log_write and esp_log_writev APIs using `--wrap` gcc option.
105+
There can be scenario where another component also wants to wrap the logging functions.
106+
In that case, enable this option and use the data ingestion APIs esp_diag_log_write and esp_diag_log_writev.
99107
endmenu

components/esp_diagnostics/include/esp_diagnostics.h

+33
Original file line numberDiff line numberDiff line change
@@ -298,6 +298,39 @@ uint32_t esp_diag_meta_crc_get(void);
298298
*/
299299
uint32_t esp_diag_data_size_get_crc(void);
300300

301+
302+
/**
303+
* @brief Convenience API for ingesting log data into diagnostics when esp_log_writev() is externally wrapped.
304+
* This API should be called from __wrap_esp_log_writev(). \see CONFIG_DIAG_USE_EXTERNAL_LOG_WRAP.
305+
*
306+
* @param[in] level Log level
307+
* @param[in] tag Tag of the log
308+
* @param[in] format Format of the log
309+
* @param[in] v Variable argument list
310+
*
311+
* @note The Diagnostics component wraps the esp_log_write() and esp_log_writev() APIs using the `--wrap` GCC option
312+
* to collect logs. If another component intends to wrap the logging APIs, enable the configuration option
313+
* CONFIG_DIAG_USE_EXTERNAL_LOG_WRAP. This will prevent the Diagnostics component from wrapping the logging APIs.
314+
* To enable log diagnostics in such case, call the esp_diag_log_writev() and esp_diag_log_write() APIs within
315+
* their respective externally wrapped APIs.
316+
*
317+
* @note Avoid calling this API explicitly unless there is an use case as the one described above.
318+
*/
319+
void esp_diag_log_writev(esp_log_level_t level, const char *tag, const char *format, va_list v);
320+
321+
/**
322+
* @brief Convenience API for ingesting log data into diagnostics when esp_log_write() is externally wrapped.
323+
* This API should be called from __wrap_esp_log_write(). \see CONFIG_DIAG_USE_EXTERNAL_LOG_WRAP.
324+
*
325+
* @param[in] level Log level
326+
* @param[in] tag Tag of the log
327+
* @param[in] format Format of the log
328+
* @param[in] v variable argument list
329+
*
330+
* @note Please see notes from \see esp_diag_log_writev()
331+
*/
332+
void esp_diag_log_write(esp_log_level_t level, const char *tag, const char *format, va_list v);
333+
301334
#ifdef __cplusplus
302335
}
303336
#endif

components/esp_diagnostics/src/esp_diagnostics_log_hook.c

+35-17
Original file line numberDiff line numberDiff line change
@@ -427,6 +427,38 @@ void __wrap_log_printf(const char *format, ...)
427427
va_end(list);
428428
}
429429
#endif
430+
431+
void esp_diag_log_writev(esp_log_level_t level,
432+
const char *tag,
433+
const char *format,
434+
va_list args)
435+
{
436+
#ifndef CONFIG_DIAG_LOG_DROP_WIFI_LOGS
437+
/* Only collect logs with "wifi" tag */
438+
if (strcmp(tag, "wifi") == 0) {
439+
uint32_t pc = 0;
440+
pc = esp_cpu_process_stack_pc((uint32_t)__builtin_return_address(0));
441+
esp_diag_log(level, pc, tag, format, args);
442+
}
443+
#endif /* !CONFIG_DIAG_LOG_DROP_WIFI_LOGS */
444+
}
445+
446+
void esp_diag_log_write(esp_log_level_t level,
447+
const char *tag,
448+
const char *format,
449+
va_list list)
450+
{
451+
#ifndef BOOTLOADER_BUILD
452+
/* Logs with "wifi" tag, will be collected in esp_log_writev() */
453+
if (strcmp(tag, "wifi") != 0) {
454+
uint32_t pc = 0;
455+
pc = esp_cpu_process_stack_pc((uint32_t)__builtin_return_address(0));
456+
esp_diag_log(level, pc, tag, format, list);
457+
}
458+
#endif
459+
}
460+
461+
#if !CONFIG_DIAG_USE_EXTERNAL_LOG_WRAP
430462
/* Wrapping esp_log_write() and esp_log_writev() reduces the
431463
* changes required in esp_log module to support diagnostics
432464
*/
@@ -440,15 +472,7 @@ void __wrap_esp_log_writev(esp_log_level_t level,
440472
const char *format,
441473
va_list args)
442474
{
443-
#ifndef CONFIG_DIAG_LOG_DROP_WIFI_LOGS
444-
/* Only collect logs with "wifi" tag */
445-
if (strcmp(tag, "wifi") == 0) {
446-
uint32_t pc = 0;
447-
pc = esp_cpu_process_stack_pc((uint32_t)__builtin_return_address(0));
448-
esp_diag_log(level, pc, tag, format, args);
449-
}
450-
#endif /* !CONFIG_DIAG_LOG_DROP_WIFI_LOGS */
451-
475+
esp_diag_log_write(level, tag, format, args);
452476
__real_esp_log_writev(level, tag, format, args);
453477
}
454478

@@ -458,14 +482,8 @@ void __wrap_esp_log_write(esp_log_level_t level,
458482
{
459483
va_list list;
460484
va_start(list, format);
461-
#ifndef BOOTLOADER_BUILD
462-
/* Logs with "wifi" tag, will be collected in esp_log_writev() */
463-
if (strcmp(tag, "wifi") != 0) {
464-
uint32_t pc = 0;
465-
pc = esp_cpu_process_stack_pc((uint32_t)__builtin_return_address(0));
466-
esp_diag_log(level, pc, tag, format, list);
467-
}
468-
#endif
485+
esp_diag_log_writev(level, tag, format, list);
469486
esp_log_writev(level, tag, format, list);
470487
va_end(list);
471488
}
489+
#endif // CONFIG_DIAG_USE_EXTERNAL_LOG_WRAP

0 commit comments

Comments
 (0)