mirror of
https://github.com/abseil/abseil-cpp.git
synced 2026-06-04 12:07:05 +08:00
Rolls back use of emscripten_errn as some implementations may not be compatible.
PiperOrigin-RevId: 544677169 Change-Id: I98874c5c8d1c9a057958b63e2b3c4fdd5daccd39
This commit is contained in:
committed by
Copybara-Service
parent
6879e28c7b
commit
f3ea24d72e
@@ -206,31 +206,27 @@ void DefaultInternalLog(absl::LogSeverity severity, const char* file, int line,
|
||||
} // namespace
|
||||
|
||||
void AsyncSignalSafeWriteError(const char* s, size_t len) {
|
||||
if (!len) return;
|
||||
absl::base_internal::ErrnoSaver errno_saver;
|
||||
#if defined(__EMSCRIPTEN__)
|
||||
// In WebAssembly, bypass filesystem emulation via fwrite.
|
||||
if (s[len - 1] == '\n') {
|
||||
// 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
|
||||
// TODO(b/282811932): Avoid this copy if these emscripten functions can
|
||||
// be updated to accept size directly.
|
||||
char buf[kLogBufSize];
|
||||
if (len >= kLogBufSize) {
|
||||
len = kLogBufSize - 1;
|
||||
constexpr size_t trunc_len = sizeof(kTruncated) - 2;
|
||||
memcpy(buf + len - trunc_len, kTruncated, trunc_len);
|
||||
size_t trunc_len = sizeof(kTruncated) - 2;
|
||||
strncpy(buf + len - trunc_len, kTruncated, trunc_len);
|
||||
buf[len] = '\0';
|
||||
len -= trunc_len;
|
||||
} else {
|
||||
buf[len] = '\0';
|
||||
} else if (len && s[len - 1] == '\n') {
|
||||
len--;
|
||||
}
|
||||
strncpy(buf, s, len);
|
||||
if (len) {
|
||||
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)
|
||||
// We prefer calling write via `syscall` to minimize the risk of libc doing
|
||||
// something "helpful".
|
||||
|
||||
@@ -16,7 +16,6 @@
|
||||
|
||||
#include <atomic>
|
||||
#include <cstdio>
|
||||
|
||||
#if defined(__EMSCRIPTEN__)
|
||||
#include <emscripten/console.h>
|
||||
#endif
|
||||
@@ -26,7 +25,6 @@
|
||||
#include "absl/base/internal/raw_logging.h"
|
||||
#include "absl/base/log_severity.h"
|
||||
#include "absl/strings/string_view.h"
|
||||
#include "absl/strings/strip.h"
|
||||
#include "absl/time/time.h"
|
||||
|
||||
namespace absl {
|
||||
@@ -60,18 +58,16 @@ void SetInitialized() {
|
||||
}
|
||||
|
||||
void WriteToStderr(absl::string_view message, absl::LogSeverity severity) {
|
||||
if (message.empty()) return;
|
||||
#if defined(__EMSCRIPTEN__)
|
||||
// In WebAssembly, bypass filesystem emulation via fwrite.
|
||||
// Skip a trailing newline character as emscripten_errn adds one itself.
|
||||
const auto message_minus_newline = absl::StripSuffix(message, "\n");
|
||||
// emscripten_errn introduced in emscripten 3.1.41
|
||||
#if ABSL_INTERNAL_EMSCRIPTEN_VERSION >= 3001041
|
||||
emscripten_errn(message_minus_newline.data(), message_minus_newline.size());
|
||||
#else
|
||||
std::string null_terminated_message(message_minus_newline);
|
||||
// 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());
|
||||
#endif
|
||||
#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.
|
||||
|
||||
Reference in New Issue
Block a user