From e077941c43da6b050101b5af151396053fb251d8 Mon Sep 17 00:00:00 2001 From: Abseil Team Date: Tue, 30 May 2023 15:28:44 -0700 Subject: For web assembly, implement WriteToStderr as emscripten_err. This avoids the need to use filesystem APIs just to write to stderr, which emscripten implements the same as this under the hood. PiperOrigin-RevId: 536525710 Change-Id: I0a647a4593eacfba324505b6e8c4acfb577ac839 --- absl/log/internal/globals.cc | 15 +++++++++++++++ 1 file changed, 15 insertions(+) (limited to 'absl') diff --git a/absl/log/internal/globals.cc b/absl/log/internal/globals.cc index 863b047f..9ba997d5 100644 --- a/absl/log/internal/globals.cc +++ b/absl/log/internal/globals.cc @@ -16,6 +16,9 @@ #include #include +#if defined(__EMSCRIPTEN__) +#include +#endif #include "absl/base/attributes.h" #include "absl/base/config.h" @@ -55,9 +58,21 @@ void SetInitialized() { } void WriteToStderr(absl::string_view message, absl::LogSeverity severity) { +#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. + std::string null_terminated_message(message); + if (!null_terminated_message.empty() && + null_terminated_message.back() == '\n') { + null_terminated_message.pop_back(); + } + _emscripten_err(null_terminated_message.c_str()); +#else // Avoid using std::cerr from this module since we may get called during // exit code, and cerr may be partially or fully destroyed by then. std::fwrite(message.data(), message.size(), 1, stderr); +#endif #if defined(_WIN64) || defined(_WIN32) || defined(_WIN16) // C99 requires stderr to not be fully-buffered by default (7.19.3.7), but -- cgit v1.2.3