From 3fcd79867d8e3d0efd1499f0c2a7a8b2d0ac1a47 Mon Sep 17 00:00:00 2001 From: Enrico Loparco Date: Wed, 24 Jan 2024 06:05:07 +0100 Subject: [PATCH] Forward log and log level to custom bh_log callback (#3070) Follow-up on #2907. The log level is needed in the host embedder to better integrate with the embedder's logger. Allow the developer to customize his bh_log callback with `cmake -DWAMR_BH_LOG=`, and update sample/basic to show the usage. --- build-scripts/config_common.cmake | 3 +++ core/shared/utils/bh_log.c | 2 ++ core/shared/utils/bh_log.h | 6 ++++++ doc/build_wamr.md | 12 +++++++++++- samples/basic/build.sh | 2 +- samples/basic/src/main.c | 28 +++++++++++++++++++++++++++- 6 files changed, 50 insertions(+), 3 deletions(-) diff --git a/build-scripts/config_common.cmake b/build-scripts/config_common.cmake index 6e13d0ffe..cb0b7210b 100644 --- a/build-scripts/config_common.cmake +++ b/build-scripts/config_common.cmake @@ -338,6 +338,9 @@ endif () if (DEFINED WAMR_BH_VPRINTF) add_definitions (-DBH_VPRINTF=${WAMR_BH_VPRINTF}) endif () +if (DEFINED WAMR_BH_LOG) + add_definitions (-DBH_LOG=${WAMR_BH_LOG}) +endif () if (WAMR_DISABLE_APP_ENTRY EQUAL 1) message (" WAMR application entry functions excluded") endif () diff --git a/core/shared/utils/bh_log.c b/core/shared/utils/bh_log.c index 7a9465e0c..1ffd9b764 100644 --- a/core/shared/utils/bh_log.c +++ b/core/shared/utils/bh_log.c @@ -17,6 +17,7 @@ bh_log_set_verbose_level(uint32 level) log_verbose_level = level; } +#ifndef BH_LOG void bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...) { @@ -56,6 +57,7 @@ bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...) os_printf("\n"); } +#endif static uint32 last_time_ms = 0; static uint32 total_time_ms = 0; diff --git a/core/shared/utils/bh_log.h b/core/shared/utils/bh_log.h index e0bc61da2..53921b250 100644 --- a/core/shared/utils/bh_log.h +++ b/core/shared/utils/bh_log.h @@ -38,8 +38,14 @@ typedef enum { void bh_log_set_verbose_level(uint32 level); +#ifndef BH_LOG void bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...); +#else +void +BH_LOG(uint32 log_level, const char *file, int line, const char *fmt, ...); +#define bh_log BH_LOG +#endif #ifdef BH_PLATFORM_NUTTX diff --git a/doc/build_wamr.md b/doc/build_wamr.md index a239a4050..6857478a9 100644 --- a/doc/build_wamr.md +++ b/doc/build_wamr.md @@ -190,7 +190,17 @@ Currently we only profile the memory consumption of module, module_instance and > } > ``` > -> and then use `cmake -DWAMR_BH_VPRINTF=my_vprintf ..` to pass the callback function, or add `BH_VPRINTF=my_vprintf` macro for the compiler, e.g. add line `add_defintions(-DBH_VPRINTF=my_vprintf)` in CMakeListst.txt. +> and then use `cmake -DWAMR_BH_VPRINTF=my_vprintf ..` to pass the callback function, or add `BH_VPRINTF=my_vprintf` macro for the compiler, e.g. add line `add_defintions(-DBH_VPRINTF=my_vprintf)` in CMakeListst.txt. See [basic sample](../samples/basic/src/main.c) for a usage example. + +#### **WAMR_BH_LOG**=, default to disable if not set +> Note: if the log_callback function is provided by the developer, WAMR logs are redirected to such callback. For example: +> ```C +> void my_log(uint32 log_level, const char *file, int line, const char *fmt, ...) +> { +> /* Usage of custom logger */ +> } +> ``` +> See [basic sample](../samples/basic/src/main.c) for a usage example. #### **Enable reference types feature** - **WAMR_BUILD_REF_TYPES**=1/0, default to disable if not set diff --git a/samples/basic/build.sh b/samples/basic/build.sh index bc75ac783..c1d598a8c 100755 --- a/samples/basic/build.sh +++ b/samples/basic/build.sh @@ -21,7 +21,7 @@ echo "#####################build basic project" cd ${CURR_DIR} mkdir -p cmake_build cd cmake_build -cmake .. -DCMAKE_BUILD_TYPE=Debug +cmake .. -DCMAKE_BUILD_TYPE=Debug -DWAMR_BH_VPRINTF=my_vprintf -DWAMR_BH_LOG=my_log make -j ${nproc} if [ $? != 0 ];then echo "BUILD_FAIL basic exit as $?\n" diff --git a/samples/basic/src/main.c b/samples/basic/src/main.c index c35da3179..ca580af33 100644 --- a/samples/basic/src/main.c +++ b/samples/basic/src/main.c @@ -15,6 +15,30 @@ get_pow(int x, int y); int32_t calculate_native(int32_t n, int32_t func1, int32_t func2); +void +my_log(uint32 log_level, const char *file, int line, const char *fmt, ...) +{ + char buf[200]; + snprintf(buf, 200, + log_level == WASM_LOG_LEVEL_VERBOSE ? "[WamrLogger - VERBOSE] %s" + : "[WamrLogger] %s", + fmt); + + va_list ap; + va_start(ap, fmt); + vprintf(buf, ap); + va_end(ap); +} + +int +my_vprintf(const char *format, va_list ap) +{ + /* Print in blue */ + char buf[200]; + snprintf(buf, 200, "\x1b[34m%s\x1b[0m", format); + return vprintf(buf, ap); +} + void print_usage(void) { @@ -95,6 +119,7 @@ main(int argc, char *argv_main[]) printf("Init runtime environment failed.\n"); return -1; } + wasm_runtime_set_log_level(WASM_LOG_LEVEL_VERBOSE); buffer = bh_read_file_to_buffer(wasm_path, &buf_size); @@ -103,7 +128,8 @@ main(int argc, char *argv_main[]) goto fail; } - module = wasm_runtime_load(buffer, buf_size, error_buf, sizeof(error_buf)); + module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf, + sizeof(error_buf)); if (!module) { printf("Load wasm module failed. error: %s\n", error_buf); goto fail;