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
diff --git a/absl/log/internal/globals.cc b/absl/log/internal/globals.cc
index 863b047..9ba997d 100644
--- a/absl/log/internal/globals.cc
+++ b/absl/log/internal/globals.cc
@@ -16,6 +16,9 @@
 
 #include <atomic>
 #include <cstdio>
+#if defined(__EMSCRIPTEN__)
+#include <emscripten/console.h>
+#endif
 
 #include "absl/base/attributes.h"
 #include "absl/base/config.h"
@@ -55,9 +58,21 @@
 }
 
 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