From 4a82a1f4699348673426e255bbf8f8d8fadc7682 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Wed, 14 Jun 2023 13:41:04 -0700 Subject: For web assembly, implement raw logging as direct JS console logging. This bypasses the need for filesystem APIs just for stderr access. PiperOrigin-RevId: 540367155 Change-Id: Ib91dadac28cb5c8e571407b5dfa60c3e65539c95 --- absl/base/internal/raw_logging.cc | 26 +++++++++++++++++++++++++- 1 file changed, 25 insertions(+), 1 deletion(-) diff --git a/absl/base/internal/raw_logging.cc b/absl/base/internal/raw_logging.cc index 6273e847..5ea49bca 100644 --- a/absl/base/internal/raw_logging.cc +++ b/absl/base/internal/raw_logging.cc @@ -21,6 +21,10 @@ #include #include +#ifdef __EMSCRIPTEN__ +#include +#endif + #include "absl/base/attributes.h" #include "absl/base/config.h" #include "absl/base/internal/atomic_hook.h" @@ -203,7 +207,27 @@ void DefaultInternalLog(absl::LogSeverity severity, const char* file, int line, void AsyncSignalSafeWriteToStderr(const char* s, size_t len) { absl::base_internal::ErrnoSaver errno_saver; -#if defined(ABSL_HAVE_SYSCALL_WRITE) +#if defined(__EMSCRIPTEN__) + // In WebAssembly, bypass filesystem emulation via fwrite. + // TODO(b/282811932): Avoid this copy if these emscripten functions can + // be updated to accept size directly. + char buf[kLogBufSize]; + if (len >= kLogBufSize) { + len = kLogBufSize - 1; + size_t trunc_len = sizeof(kTruncated) - 2; + strncpy(buf + len - trunc_len, kTruncated, trunc_len); + buf[len] = '\0'; + len -= trunc_len; + } else if (len && s[len - 1] == '\n') { + len--; + } + strncpy(buf, s, len); + if (len) { + buf[len] = '\0'; + // Skip a trailing newline character as emscripten_err adds one itself. + _emscripten_err(buf); + } +#elif defined(ABSL_HAVE_SYSCALL_WRITE) // We prefer calling write via `syscall` to minimize the risk of libc doing // something "helpful". syscall(SYS_write, STDERR_FILENO, s, len); -- cgit v1.2.3