Provide default vprintf on UWP (#2725)

UWP apps do not have a console attached so any output to stdout/stderr
is lost. Therefore, provide a default BH_VPRINTF in that case for debug
builds which redirects output to the debugger.
This commit is contained in:
zoraaver
2023-11-07 12:37:43 +00:00
committed by GitHub
parent 77b6bce679
commit 3c9cd40aa6
3 changed files with 50 additions and 8 deletions

View File

@ -179,6 +179,17 @@ typedef uint32_t os_raw_file_handle;
#define bh_socket_t windows_handle *
// UWP apps do not have stdout/stderr handles so provide a default
// implementation of vprintf on debug builds so output from WASI libc is sent to
// the debugger and not lost completely.
#if !defined(BH_VPRINTF) && !defined(NDEBUG) && WINAPI_PARTITION_DESKTOP == 0
int
uwp_print_to_debugger(const char *format, va_list ap);
#define BH_VPRINTF uwp_print_to_debugger
#define UWP_DEFAULT_VPRINTF
#endif
#ifdef __cplusplus
}
#endif

View File

@ -57,4 +57,37 @@ convert_windows_error_code(DWORD windows_error_code)
default:
return __WASI_EINVAL;
}
}
}
#ifdef UWP_DEFAULT_VPRINTF
int
uwp_print_to_debugger(const char *format, va_list ap)
{
// Provide a stack buffer which should be large enough for any realistic
// string so we avoid making an allocation on every printf call.
char stack_buf[2048];
char *buf = stack_buf;
int ret = vsnprintf(stack_buf, sizeof(stack_buf), format, ap);
if (ret >= sizeof(stack_buf)) {
// Allocate an extra byte for the null terminator.
char *heap_buf = BH_MALLOC(ret + 1);
buf = heap_buf;
if (heap_buf == NULL) {
errno = ENOMEM;
return -1;
}
ret = vsnprintf(heap_buf, ret + 1, format, ap);
}
if (ret >= 0)
OutputDebugStringA(buf);
if (buf != stack_buf)
BH_FREE(buf);
return ret;
}
#endif