Commit f3ea24d7 by Abseil Team Committed by Copybara-Service

Rolls back use of emscripten_errn as some implementations may not be compatible.

PiperOrigin-RevId: 544677169
Change-Id: I98874c5c8d1c9a057958b63e2b3c4fdd5daccd39
parent 6879e28c
...@@ -206,31 +206,27 @@ void DefaultInternalLog(absl::LogSeverity severity, const char* file, int line, ...@@ -206,31 +206,27 @@ 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.
if (s[len - 1] == '\n') { // 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.
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;
constexpr size_t trunc_len = sizeof(kTruncated) - 2; size_t trunc_len = sizeof(kTruncated) - 2;
memcpy(buf + len - trunc_len, kTruncated, trunc_len); strncpy(buf + len - trunc_len, kTruncated, trunc_len);
buf[len] = '\0'; buf[len] = '\0';
len -= trunc_len; len -= trunc_len;
} else { } else if (len && s[len - 1] == '\n') {
buf[len] = '\0'; len--;
} }
memcpy(buf, s, len); strncpy(buf, s, len);
if (len) {
buf[len] = '\0';
// Skip a trailing newline character as emscripten_err adds one itself.
_emscripten_err(buf); _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,7 +16,6 @@ ...@@ -16,7 +16,6 @@
#include <atomic> #include <atomic>
#include <cstdio> #include <cstdio>
#if defined(__EMSCRIPTEN__) #if defined(__EMSCRIPTEN__)
#include <emscripten/console.h> #include <emscripten/console.h>
#endif #endif
...@@ -26,7 +25,6 @@ ...@@ -26,7 +25,6 @@
#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 {
...@@ -60,18 +58,16 @@ void SetInitialized() { ...@@ -60,18 +58,16 @@ 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.
// Skip a trailing newline character as emscripten_errn adds one itself. // TODO(b/282811932): Avoid this copy if these emscripten functions can
const auto message_minus_newline = absl::StripSuffix(message, "\n"); // be updated to accept size directly.
// emscripten_errn introduced in emscripten 3.1.41 std::string null_terminated_message(message);
#if ABSL_INTERNAL_EMSCRIPTEN_VERSION >= 3001041 if (!null_terminated_message.empty() &&
emscripten_errn(message_minus_newline.data(), message_minus_newline.size()); null_terminated_message.back() == '\n') {
#else null_terminated_message.pop_back();
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