summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2023-05-30 15:28:44 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2023-05-30 15:29:44 -0700
commite077941c43da6b050101b5af151396053fb251d8 (patch)
tree42e8a9d01589168d2102751a2ad8a5a6566eb87b
parent8e45685002488b55f24cb67a795eaa8d1c3297a1 (diff)
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
-rw-r--r--absl/log/internal/globals.cc15
1 files changed, 15 insertions, 0 deletions
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 <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 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