Commit d9a01008 by Abseil Team Committed by Copybara-Service

Use new emscripten_errn to avoid copying strings.

PiperOrigin-RevId: 544461113
Change-Id: Iafbd6daf2d03ae18a49ea449315ee7cd6a0e615e
parent a3020c76
...@@ -206,27 +206,31 @@ void DefaultInternalLog(absl::LogSeverity severity, const char* file, int line, ...@@ -206,27 +206,31 @@ void DefaultInternalLog(absl::LogSeverity severity, const char* file, int line,
} // namespace } // namespace
void AsyncSignalSafeWriteError(const char* s, size_t len) { void AsyncSignalSafeWriteError(const char* s, size_t len) {
if (!len) return;
absl::base_internal::ErrnoSaver errno_saver; absl::base_internal::ErrnoSaver errno_saver;
#if defined(__EMSCRIPTEN__) #if defined(__EMSCRIPTEN__)
// In WebAssembly, bypass filesystem emulation via fwrite. // In WebAssembly, bypass filesystem emulation via fwrite.
// TODO(b/282811932): Avoid this copy if these emscripten functions can if (s[len - 1] == '\n') {
// be updated to accept size directly. // Skip a trailing newline character as emscripten_errn adds one itself.
len--;
}
// emscripten_errn introduced in emscripten 3.1.41
#if ABSL_INTERNAL_EMSCRIPTEN_VERSION >= 3001041
emscripten_errn(s, len);
#else
char buf[kLogBufSize]; char buf[kLogBufSize];
if (len >= kLogBufSize) { if (len >= kLogBufSize) {
len = kLogBufSize - 1; len = kLogBufSize - 1;
size_t trunc_len = sizeof(kTruncated) - 2; constexpr size_t trunc_len = sizeof(kTruncated) - 2;
strncpy(buf + len - trunc_len, kTruncated, trunc_len); memcpy(buf + len - trunc_len, kTruncated, trunc_len);
buf[len] = '\0'; buf[len] = '\0';
len -= trunc_len; len -= trunc_len;
} else if (len && s[len - 1] == '\n') { } else {
len--;
}
strncpy(buf, s, len);
if (len) {
buf[len] = '\0'; buf[len] = '\0';
// Skip a trailing newline character as emscripten_err adds one itself.
_emscripten_err(buf);
} }
memcpy(buf, s, len);
_emscripten_err(buf);
#endif
#elif defined(ABSL_HAVE_SYSCALL_WRITE) #elif defined(ABSL_HAVE_SYSCALL_WRITE)
// We prefer calling write via `syscall` to minimize the risk of libc doing // We prefer calling write via `syscall` to minimize the risk of libc doing
// something "helpful". // something "helpful".
......
...@@ -16,6 +16,7 @@ ...@@ -16,6 +16,7 @@
#include <atomic> #include <atomic>
#include <cstdio> #include <cstdio>
#if defined(__EMSCRIPTEN__) #if defined(__EMSCRIPTEN__)
#include <emscripten/console.h> #include <emscripten/console.h>
#endif #endif
...@@ -25,6 +26,7 @@ ...@@ -25,6 +26,7 @@
#include "absl/base/internal/raw_logging.h" #include "absl/base/internal/raw_logging.h"
#include "absl/base/log_severity.h" #include "absl/base/log_severity.h"
#include "absl/strings/string_view.h" #include "absl/strings/string_view.h"
#include "absl/strings/strip.h"
#include "absl/time/time.h" #include "absl/time/time.h"
namespace absl { namespace absl {
...@@ -58,16 +60,18 @@ void SetInitialized() { ...@@ -58,16 +60,18 @@ void SetInitialized() {
} }
void WriteToStderr(absl::string_view message, absl::LogSeverity severity) { void WriteToStderr(absl::string_view message, absl::LogSeverity severity) {
if (message.empty()) return;
#if defined(__EMSCRIPTEN__) #if defined(__EMSCRIPTEN__)
// In WebAssembly, bypass filesystem emulation via fwrite. // In WebAssembly, bypass filesystem emulation via fwrite.
// TODO(b/282811932): Avoid this copy if these emscripten functions can // Skip a trailing newline character as emscripten_errn adds one itself.
// be updated to accept size directly. const auto message_minus_newline = absl::StripSuffix(message, "\n");
std::string null_terminated_message(message); // emscripten_errn introduced in emscripten 3.1.41
if (!null_terminated_message.empty() && #if ABSL_INTERNAL_EMSCRIPTEN_VERSION >= 3001041
null_terminated_message.back() == '\n') { emscripten_errn(message_minus_newline.data(), message_minus_newline.size());
null_terminated_message.pop_back(); #else
} std::string null_terminated_message(message_minus_newline);
_emscripten_err(null_terminated_message.c_str()); _emscripten_err(null_terminated_message.c_str());
#endif
#else #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.
......
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