Commit e077941c by Abseil Team Committed by Copybara-Service

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
parent 8e456850
...@@ -16,6 +16,9 @@ ...@@ -16,6 +16,9 @@
#include <atomic> #include <atomic>
#include <cstdio> #include <cstdio>
#if defined(__EMSCRIPTEN__)
#include <emscripten/console.h>
#endif
#include "absl/base/attributes.h" #include "absl/base/attributes.h"
#include "absl/base/config.h" #include "absl/base/config.h"
...@@ -55,9 +58,21 @@ void SetInitialized() { ...@@ -55,9 +58,21 @@ void SetInitialized() {
} }
void WriteToStderr(absl::string_view message, absl::LogSeverity severity) { 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 // 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. // exit code, and cerr may be partially or fully destroyed by then.
std::fwrite(message.data(), message.size(), 1, stderr); std::fwrite(message.data(), message.size(), 1, stderr);
#endif
#if defined(_WIN64) || defined(_WIN32) || defined(_WIN16) #if defined(_WIN64) || defined(_WIN32) || defined(_WIN16)
// C99 requires stderr to not be fully-buffered by default (7.19.3.7), but // C99 requires stderr to not be fully-buffered by default (7.19.3.7), but
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment