diff options
author | Abseil Team <absl-team@google.com> | 2023-06-14 13:41:04 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-06-14 13:42:23 -0700 |
commit | 4a82a1f4699348673426e255bbf8f8d8fadc7682 (patch) | |
tree | 3114204c28c81c1511507c7f781d6d18d2896b22 /absl/base | |
parent | dc37a887fdc3d91ec9a67b93af9fa4c1ccb02545 (diff) |
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
Diffstat (limited to 'absl/base')
-rw-r--r-- | absl/base/internal/raw_logging.cc | 26 |
1 files changed, 25 insertions, 1 deletions
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 <cstring> #include <string> +#ifdef __EMSCRIPTEN__ +#include <emscripten/console.h> +#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); |