Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Forward log level to custom vprintf #3070

Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
test: add example to show usage of vprintf override
  • Loading branch information
eloparco committed Jan 22, 2024
commit 1a00803b353fb5f21b1a444ee6baf5a5553b1f3b
2 changes: 1 addition & 1 deletion core/shared/platform/cosmopolitan/platform_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ os_printf(const char *format, ...)
#ifndef BH_VPRINTF
ret += vprintf(format, ap);
#else
ret += BH_VPRINTF(format, ap);
ret += BH_VPRINTF(BH_LOG_LEVEL_DEBUG, format, ap);
#endif
va_end(ap);

Expand Down
2 changes: 1 addition & 1 deletion core/shared/platform/darwin/platform_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ os_printf(const char *format, ...)
#ifndef BH_VPRINTF
ret += vprintf(format, ap);
#else
ret += BH_VPRINTF(format, ap);
ret += BH_VPRINTF(BH_LOG_LEVEL_DEBUG, format, ap);
#endif
va_end(ap);

Expand Down
2 changes: 1 addition & 1 deletion core/shared/platform/freebsd/platform_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ os_printf(const char *format, ...)
#ifndef BH_VPRINTF
ret += vprintf(format, ap);
#else
ret += BH_VPRINTF(format, ap);
ret += BH_VPRINTF(BH_LOG_LEVEL_DEBUG, format, ap);
#endif
va_end(ap);

Expand Down
5 changes: 3 additions & 2 deletions core/shared/platform/include/platform_common.h
Original file line number Diff line number Diff line change
Expand Up @@ -54,10 +54,11 @@ BH_FREE(void *ptr);

#if defined(BH_VPRINTF)
#if defined(MSVC)
__declspec(dllimport) int BH_VPRINTF(const char *format, va_list ap);
__declspec(dllimport) int BH_VPRINTF(uint32_t log_level, const char *format,
va_list ap);
#else
int
BH_VPRINTF(LogLevel log_level, const char *format, va_list ap);
BH_VPRINTF(uint32_t log_level, const char *format, va_list ap);
#endif
#endif

Expand Down
2 changes: 1 addition & 1 deletion core/shared/platform/linux/platform_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ os_printf(const char *format, ...)
#ifndef BH_VPRINTF
ret += vprintf(format, ap);
#else
ret += BH_VPRINTF(format, ap);
ret += BH_VPRINTF(BH_LOG_LEVEL_DEBUG, format, ap);
#endif
va_end(ap);

Expand Down
2 changes: 1 addition & 1 deletion core/shared/platform/vxworks/platform_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ os_printf(const char *format, ...)
#ifndef BH_VPRINTF
ret += vprintf(format, ap);
#else
ret += BH_VPRINTF(format, ap);
ret += BH_VPRINTF(BH_LOG_LEVEL_DEBUG, format, ap);
#endif
va_end(ap);

Expand Down
2 changes: 1 addition & 1 deletion core/shared/platform/windows/platform_init.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ os_printf(const char *format, ...)
#ifndef BH_VPRINTF
ret += vprintf(format, ap);
#else
ret += BH_VPRINTF(format, ap);
ret += BH_VPRINTF(BH_LOG_LEVEL_DEBUG, format, ap);
#endif
va_end(ap);

Expand Down
2 changes: 1 addition & 1 deletion core/shared/platform/zephyr/zephyr_platform.c
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,7 @@ os_printf(const char *format, ...)
#ifndef BH_VPRINTF
ret += vprintf(format, ap);
#else
ret += BH_VPRINTF(format, ap);
ret += BH_VPRINTF(BH_LOG_LEVEL_DEBUG, format, ap);
#endif
va_end(ap);

Expand Down
4 changes: 2 additions & 2 deletions doc/build_wamr.md
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ Currently we only profile the memory consumption of module, module_instance and
> Note: if the vprintf_callback function is provided by developer, the os_printf() and os_vprintf() in Linux, Darwin, Windows and VxWorks platforms, besides WASI Libc output will call the callback function instead of libc vprintf() function to redirect the stdout output. For example, developer can define the callback function like below outside runtime lib:
>
> ```C
> int my_vprintf(LogLevel log_level, const char *format, va_list ap)
> int my_vprintf(log_level_t log_level, const char *format, va_list ap)
> {
> /* output to pre-opened file stream */
> FILE *my_file = ...;
Expand All @@ -179,7 +179,7 @@ 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.

#### **Enable reference types feature**
- **WAMR_BUILD_REF_TYPES**=1/0, default to disable if not set
Expand Down
2 changes: 1 addition & 1 deletion samples/basic/build.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
make -j ${nproc}
if [ $? != 0 ];then
echo "BUILD_FAIL basic exit as $?\n"
Expand Down
17 changes: 17 additions & 0 deletions samples/basic/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,23 @@ get_pow(int x, int y);
int32_t
calculate_native(int32_t n, int32_t func1, int32_t func2);

int
my_vprintf(log_level_t log_level, const char *format, va_list ap)
{
if (log_level == WASM_LOG_LEVEL_DEBUG) {
/* Print in blue */
char buf[200];
snprintf(buf, 200,
"\x1b[34m"
"%s"
"\x1b[0m",
format);
return vprintf(buf, ap);
}

return vprintf(format, ap);
}

void
print_usage(void)
{
Expand Down