Skip to content

Commit 41e2acc

Browse files
committed
feat(logs): redirect wamr logs to logger defined in embedder
1 parent 93edc1e commit 41e2acc

File tree

6 files changed

+50
-3
lines changed

6 files changed

+50
-3
lines changed

build-scripts/config_common.cmake

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -338,6 +338,9 @@ endif ()
338338
if (DEFINED WAMR_BH_VPRINTF)
339339
add_definitions (-DBH_VPRINTF=${WAMR_BH_VPRINTF})
340340
endif ()
341+
if (DEFINED WAMR_BH_LOG)
342+
add_definitions (-DBH_LOG=${WAMR_BH_LOG})
343+
endif ()
341344
if (WAMR_DISABLE_APP_ENTRY EQUAL 1)
342345
message (" WAMR application entry functions excluded")
343346
endif ()

core/shared/utils/bh_log.c

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ bh_log_set_verbose_level(uint32 level)
1717
log_verbose_level = level;
1818
}
1919

20+
#ifndef BH_LOG
2021
void
2122
bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...)
2223
{
@@ -56,6 +57,7 @@ bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...)
5657

5758
os_printf("\n");
5859
}
60+
#endif
5961

6062
static uint32 last_time_ms = 0;
6163
static uint32 total_time_ms = 0;

core/shared/utils/bh_log.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,14 @@ typedef enum {
3838
void
3939
bh_log_set_verbose_level(uint32 level);
4040

41+
#ifndef BH_LOG
4142
void
4243
bh_log(LogLevel log_level, const char *file, int line, const char *fmt, ...);
44+
#else
45+
void
46+
BH_LOG(uint32 log_level, const char *file, int line, const char *fmt, ...);
47+
#define bh_log BH_LOG
48+
#endif
4349

4450
#ifdef BH_PLATFORM_NUTTX
4551

doc/build_wamr.md

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,17 @@ Currently we only profile the memory consumption of module, module_instance and
179179
> }
180180
> ```
181181
>
182-
> 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.
182+
> 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.
183+
184+
#### **WAMR_BH_LOG**=<log_callback>, default to disable if not set
185+
> Note: if the log_callback function is provided by the developer, WAMR logs are redirected to such callback. For example:
186+
> ```C
187+
> void my_log(uint32 log_level, const char *file, int line, const char *fmt, ...)
188+
> {
189+
> /* Usage of custom logger */
190+
> }
191+
> ```
192+
> See [basic sample](../samples/basic/src/main.c) for a usage example.
183193
184194
#### **Enable reference types feature**
185195
- **WAMR_BUILD_REF_TYPES**=1/0, default to disable if not set

samples/basic/build.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ echo "#####################build basic project"
2121
cd ${CURR_DIR}
2222
mkdir -p cmake_build
2323
cd cmake_build
24-
cmake .. -DCMAKE_BUILD_TYPE=Debug
24+
cmake .. -DCMAKE_BUILD_TYPE=Debug -DWAMR_BH_VPRINTF=my_vprintf -DWAMR_BH_LOG=my_log
2525
make -j ${nproc}
2626
if [ $? != 0 ];then
2727
echo "BUILD_FAIL basic exit as $?\n"

samples/basic/src/main.c

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,30 @@ get_pow(int x, int y);
1515
int32_t
1616
calculate_native(int32_t n, int32_t func1, int32_t func2);
1717

18+
void
19+
my_log(uint32 log_level, const char *file, int line, const char *fmt, ...)
20+
{
21+
char buf[200];
22+
snprintf(buf, 200,
23+
log_level == WASM_LOG_LEVEL_VERBOSE ? "[WamrLogger - VERBOSE] %s"
24+
: "[WamrLogger] %s",
25+
fmt);
26+
27+
va_list ap;
28+
va_start(ap, fmt);
29+
vprintf(buf, ap);
30+
va_end(ap);
31+
}
32+
33+
int
34+
my_vprintf(const char *format, va_list ap)
35+
{
36+
/* Print in blue */
37+
char buf[200];
38+
snprintf(buf, 200, "\x1b[34m%s\x1b[0m", format);
39+
return vprintf(buf, ap);
40+
}
41+
1842
void
1943
print_usage(void)
2044
{
@@ -95,6 +119,7 @@ main(int argc, char *argv_main[])
95119
printf("Init runtime environment failed.\n");
96120
return -1;
97121
}
122+
wasm_runtime_set_log_level(WASM_LOG_LEVEL_VERBOSE);
98123

99124
buffer = bh_read_file_to_buffer(wasm_path, &buf_size);
100125

@@ -103,7 +128,8 @@ main(int argc, char *argv_main[])
103128
goto fail;
104129
}
105130

106-
module = wasm_runtime_load(buffer, buf_size, error_buf, sizeof(error_buf));
131+
module = wasm_runtime_load((uint8 *)buffer, buf_size, error_buf,
132+
sizeof(error_buf));
107133
if (!module) {
108134
printf("Load wasm module failed. error: %s\n", error_buf);
109135
goto fail;

0 commit comments

Comments
 (0)