Enable to use BH_VPRINTF macro to redirect stdout output (#560)

Enable to use BH_VPRINTF macro for platform Linux/Windows/Darwin/VxWorks to redirect the stdout output from platform os_printf/os_vprintf, or the wasi output from wasm app to the vprintf like callback function specified by BH_VPRINTF macro of cmake WAMR_BH_VPRINTF variable.

Signed-off-by: Wenyong Huang <wenyong.huang@intel.com>
This commit is contained in:
Wenyong Huang
2021-03-06 08:29:58 -06:00
committed by GitHub
parent a4239f1ffd
commit a1568825e8
10 changed files with 132 additions and 7 deletions

View File

@ -604,8 +604,31 @@ wasi_fd_write(wasm_exec_env_t exec_env, wasi_fd_t fd,
ciovec->buf_len = iovec_app->buf_len;
}
err = uvwasi_fd_write(uvwasi, fd,
ciovec_begin, iovs_len, &nwritten);
#ifndef BH_VPRINTF
err = uvwasi_fd_write(uvwasi, fd, ciovec_begin, iovs_len, &nwritten);
#else
/* redirect stdout/stderr output to BH_VPRINTF function */
if (fd == 1 || fd == 2) {
int i;
const struct iovec *iov1 = (const struct iovec *)ciovec_begin;
nwritten = 0;
for (i = 0; i < (int)iovs_len; i++, iov1++) {
if (iov1->iov_len > 0 && iov1->iov_base) {
char format[16];
/* make up format string "%.ns" */
snprintf(format, sizeof(format), "%%.%ds", (int)iov1->iov_len);
nwritten += (uvwasi_size_t)os_printf(format, iov1->iov_base);
}
}
err = 0;
}
else {
err = uvwasi_fd_write(uvwasi, fd, ciovec_begin, iovs_len, &nwritten);
}
#endif /* end of BH_VPRINTF */
if (err)
goto fail;

View File

@ -1237,7 +1237,29 @@ __wasi_errno_t wasmtime_ssp_fd_write(
if (error != 0)
return error;
#ifndef BH_VPRINTF
ssize_t len = writev(fd_number(fo), (const struct iovec *)iov, (int)iovcnt);
#else
ssize_t len = 0;
/* redirect stdout/stderr output to BH_VPRINTF function */
if (fd_number(fo) == 1 || fd_number(fo) == 2) {
int i;
const struct iovec *iov1 = (const struct iovec *)iov;
for (i = 0; i < (int)iovcnt; i++, iov1++) {
if (iov1->iov_len > 0 && iov1->iov_base) {
char format[16];
/* make up format string "%.ns" */
snprintf(format, sizeof(format), "%%.%ds", (int)iov1->iov_len);
len += (ssize_t)os_printf(format, iov1->iov_base);
}
}
}
else {
len = writev(fd_number(fo), (const struct iovec *)iov, (int)iovcnt);
}
#endif /* end of BH_VPRINTF */
fd_object_release(fo);
if (len < 0)
return convert_errno(errno);

View File

@ -23,7 +23,11 @@ os_printf(const char *format, ...)
va_list ap;
va_start(ap, format);
#ifndef BH_VPRINTF
ret += vprintf(format, ap);
#else
ret += BH_VPRINTF(format, ap);
#endif
va_end(ap);
return ret;
@ -32,6 +36,10 @@ os_printf(const char *format, ...)
int
os_vprintf(const char *format, va_list ap)
{
#ifndef BH_VPRINTF
return vprintf(format, ap);
#else
return BH_VPRINTF(format, ap);
#endif
}

View File

@ -34,13 +34,21 @@ extern "C" {
#endif
#if defined(MSVC)
__declspec(dllimport) void *BH_MALLOC(unsigned int size);
__declspec(dllimport) void BH_FREE(void *ptr);
__declspec(dllimport) void *BH_MALLOC(unsigned int size);
__declspec(dllimport) void BH_FREE(void *ptr);
#else
void *BH_MALLOC(unsigned int size);
void BH_FREE(void *ptr);
#endif
#if defined(BH_VPRINTF)
#if defined(MSVC)
__declspec(dllimport) int BH_VPRINTF(const char *format, va_list ap);
#else
int BH_VPRINTF(const char *format, va_list ap);
#endif
#endif
#ifndef NULL
#define NULL (void*)0
#endif

View File

@ -23,7 +23,11 @@ os_printf(const char *format, ...)
va_list ap;
va_start(ap, format);
#ifndef BH_VPRINTF
ret += vprintf(format, ap);
#else
ret += BH_VPRINTF(format, ap);
#endif
va_end(ap);
return ret;
@ -32,6 +36,10 @@ os_printf(const char *format, ...)
int
os_vprintf(const char *format, va_list ap)
{
#ifndef BH_VPRINTF
return vprintf(format, ap);
#else
return BH_VPRINTF(format, ap);
#endif
}

View File

@ -23,7 +23,11 @@ os_printf(const char *format, ...)
va_list ap;
va_start(ap, format);
#ifndef BH_VPRINTF
ret += vprintf(format, ap);
#else
ret += BH_VPRINTF(format, ap);
#endif
va_end(ap);
return ret;
@ -32,6 +36,10 @@ os_printf(const char *format, ...)
int
os_vprintf(const char *format, va_list ap)
{
#ifndef BH_VPRINTF
return vprintf(format, ap);
#else
return BH_VPRINTF(format, ap);
#endif
}

View File

@ -16,3 +16,30 @@ bh_platform_destroy()
{
}
int
os_printf(const char *format, ...)
{
int ret = 0;
va_list ap;
va_start(ap, format);
#ifndef BH_VPRINTF
ret += vprintf(format, ap);
#else
ret += BH_VPRINTF(format, ap);
#endif
va_end(ap);
return ret;
}
int
os_vprintf(const char *format, va_list ap)
{
#ifndef BH_VPRINTF
return vprintf(format, ap);
#else
return BH_VPRINTF(format, ap);
#endif
}

View File

@ -49,9 +49,6 @@ typedef struct {
unsigned int waiting_count;
} korp_cond;
#define os_printf printf
#define os_vprintf vprintf
static inline size_t
getpagesize()
{