summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2023-07-13 13:05:05 -0700
committerGravatar Copybara-Service <copybara-worker@google.com>2023-07-13 13:05:57 -0700
commitc16a2f43206b0235d49d4f6155f285a4d4939c58 (patch)
tree1e9931334ce46bdbaba59ef12585924d843df24f
parentb8ebbc2ecadb64239dc0767b55296ab9c4510b0f (diff)
Use new emscripten_errn to avoid copying strings.
PiperOrigin-RevId: 547895328 Change-Id: If5da952604415fa6ed2402052f80add6c4b7dfb3
-rw-r--r--absl/base/internal/raw_logging.cc27
-rw-r--r--absl/log/internal/globals.cc19
2 files changed, 28 insertions, 18 deletions
diff --git a/absl/base/internal/raw_logging.cc b/absl/base/internal/raw_logging.cc
index c866d957..4c922ccf 100644
--- a/absl/base/internal/raw_logging.cc
+++ b/absl/base/internal/raw_logging.cc
@@ -206,27 +206,32 @@ 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.
- // TODO(b/282811932): Avoid this copy if these emscripten functions can
- // be updated to accept size directly.
+ if (s[len - 1] == '\n') {
+ // Skip a trailing newline character as emscripten_errn adds one itself.
+ len--;
+ }
+ // emscripten_errn was introduced in 3.1.41 but broken in standalone mode
+ // until 3.1.43.
+#if ABSL_INTERNAL_EMSCRIPTEN_VERSION >= 3001043
+ emscripten_errn(s, len);
+#else
char buf[kLogBufSize];
if (len >= kLogBufSize) {
len = kLogBufSize - 1;
- size_t trunc_len = sizeof(kTruncated) - 2;
- strncpy(buf + len - trunc_len, kTruncated, trunc_len);
+ constexpr size_t trunc_len = sizeof(kTruncated) - 2;
+ memcpy(buf + len - trunc_len, kTruncated, trunc_len);
buf[len] = '\0';
len -= trunc_len;
- } else if (len && s[len - 1] == '\n') {
- len--;
- }
- strncpy(buf, s, len);
- if (len) {
+ } else {
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".
diff --git a/absl/log/internal/globals.cc b/absl/log/internal/globals.cc
index 9ba997d5..359858f1 100644
--- a/absl/log/internal/globals.cc
+++ b/absl/log/internal/globals.cc
@@ -16,6 +16,7 @@
#include <atomic>
#include <cstdio>
+
#if defined(__EMSCRIPTEN__)
#include <emscripten/console.h>
#endif
@@ -25,6 +26,7 @@
#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 {
@@ -58,16 +60,19 @@ 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.
- // 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();
- }
+ // Skip a trailing newline character as emscripten_errn adds one itself.
+ const auto message_minus_newline = absl::StripSuffix(message, "\n");
+ // emscripten_errn was introduced in 3.1.41 but broken in standalone mode
+ // until 3.1.43.
+#if ABSL_INTERNAL_EMSCRIPTEN_VERSION >= 3001043
+ emscripten_errn(message_minus_newline.data(), message_minus_newline.size());
+#else
+ std::string null_terminated_message(message_minus_newline);
_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.