diff options
author | Abseil Team <absl-team@google.com> | 2023-06-29 14:20:23 -0700 |
---|---|---|
committer | Copybara-Service <copybara-worker@google.com> | 2023-06-29 14:21:05 -0700 |
commit | d9a01008ab5bd8b5845b4f3074b3bdb453373736 (patch) | |
tree | d2a208e732b91363b8fcd6678fe9fb03ff0027bf /absl/log/internal/globals.cc | |
parent | a3020c763c12bd16bbf00804abe853afa5778174 (diff) |
Use new emscripten_errn to avoid copying strings.
PiperOrigin-RevId: 544461113
Change-Id: Iafbd6daf2d03ae18a49ea449315ee7cd6a0e615e
Diffstat (limited to 'absl/log/internal/globals.cc')
-rw-r--r-- | absl/log/internal/globals.cc | 18 |
1 files changed, 11 insertions, 7 deletions
diff --git a/absl/log/internal/globals.cc b/absl/log/internal/globals.cc index 9ba997d5..cc7a9836 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,18 @@ 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 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); _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. |