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=<log_callback>`,
and update sample/basic to show the usage.
This commit is contained in:
Enrico Loparco
2024-01-24 06:05:07 +01:00
committed by GitHub
parent 1505e61704
commit 3fcd79867d
6 changed files with 50 additions and 3 deletions

View File

@ -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;