Factor out the internal helper AppendTruncated, which is used and redefined in a couple places, plus several more that have yet to be released.

PiperOrigin-RevId: 486759835
Change-Id: Ib1b24f287f856ca38b691fbce7e747f0f5a34626
pull/1310/merge
Andy Getzendanner 2 years ago committed by Copybara-Service
parent d459fe7137
commit 615f214702
  1. 1
      absl/log/BUILD.bazel
  2. 19
      absl/log/CMakeLists.txt
  3. 14
      absl/log/internal/BUILD.bazel
  4. 40
      absl/log/internal/append_truncated.h
  5. 12
      absl/log/internal/log_format.cc
  6. 11
      absl/log/internal/log_message.cc
  7. 14
      absl/log/log_entry_test.cc

@ -303,6 +303,7 @@ cc_test(
"//absl/base:config", "//absl/base:config",
"//absl/base:core_headers", "//absl/base:core_headers",
"//absl/base:log_severity", "//absl/base:log_severity",
"//absl/log/internal:append_truncated",
"//absl/log/internal:format", "//absl/log/internal:format",
"//absl/log/internal:test_helpers", "//absl/log/internal:test_helpers",
"//absl/strings", "//absl/strings",

@ -96,6 +96,7 @@ absl_cc_library(
DEPS DEPS
absl::config absl::config
absl::core_headers absl::core_headers
absl::log_internal_append_truncated
absl::log_internal_config absl::log_internal_config
absl::log_internal_globals absl::log_internal_globals
absl::log_severity absl::log_severity
@ -143,6 +144,7 @@ absl_cc_library(
absl::errno_saver absl::errno_saver
absl::inlined_vector absl::inlined_vector
absl::examine_stack absl::examine_stack
absl::log_internal_append_truncated
absl::log_internal_config absl::log_internal_config
absl::log_internal_format absl::log_internal_format
absl::log_internal_globals absl::log_internal_globals
@ -318,6 +320,22 @@ absl_cc_library(
absl::config absl::config
) )
absl_cc_library(
NAME
log_internal_append_truncated
SRCS
HDRS
"internal/append_truncated.h"
COPTS
${ABSL_DEFAULT_COPTS}
LINKOPTS
${ABSL_DEFAULT_LINKOPTS}
DEPS
absl::config
absl::strings
absl::span
)
# Public targets # Public targets
absl_cc_library( absl_cc_library(
NAME NAME
@ -636,6 +654,7 @@ absl_cc_test(
absl::config absl::config
absl::core_headers absl::core_headers
absl::log_entry absl::log_entry
absl::log_internal_append_truncated
absl::log_internal_format absl::log_internal_format
absl::log_internal_globals absl::log_internal_globals
absl::log_internal_test_helpers absl::log_internal_test_helpers

@ -91,6 +91,7 @@ cc_library(
copts = ABSL_DEFAULT_COPTS, copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS, linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [ deps = [
":append_truncated",
":config", ":config",
":globals", ":globals",
"//absl/base:config", "//absl/base:config",
@ -132,6 +133,7 @@ cc_library(
"//absl/log:__pkg__", "//absl/log:__pkg__",
], ],
deps = [ deps = [
":append_truncated",
":config", ":config",
":format", ":format",
":globals", ":globals",
@ -158,6 +160,18 @@ cc_library(
], ],
) )
cc_library(
name = "append_truncated",
hdrs = ["append_truncated.h"],
copts = ABSL_DEFAULT_COPTS,
linkopts = ABSL_DEFAULT_LINKOPTS,
deps = [
"//absl/base:config",
"//absl/strings",
"//absl/types:span",
],
)
cc_library( cc_library(
name = "log_sink_set", name = "log_sink_set",
srcs = ["log_sink_set.cc"], srcs = ["log_sink_set.cc"],

@ -0,0 +1,40 @@
// Copyright 2022 The Abseil Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#ifndef ABSL_LOG_INTERNAL_APPEND_TRUNCATED_H_
#define ABSL_LOG_INTERNAL_APPEND_TRUNCATED_H_
#include <cstddef>
#include <cstring>
#include "absl/base/config.h"
#include "absl/strings/string_view.h"
#include "absl/types/span.h"
namespace absl {
ABSL_NAMESPACE_BEGIN
namespace log_internal {
// Copies into `dst` as many bytes of `src` as will fit, then truncates the
// copied bytes from the front of `dst` and returns the number of bytes written.
inline size_t AppendTruncated(absl::string_view src, absl::Span<char> &dst) {
if (src.size() > dst.size()) src = src.substr(0, dst.size());
memcpy(dst.data(), src.data(), src.size());
dst.remove_prefix(src.size());
return src.size();
}
} // namespace log_internal
ABSL_NAMESPACE_END
} // namespace absl
#endif // ABSL_LOG_INTERNAL_APPEND_TRUNCATED_H_

@ -32,6 +32,7 @@
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/base/log_severity.h" #include "absl/base/log_severity.h"
#include "absl/base/optimization.h" #include "absl/base/optimization.h"
#include "absl/log/internal/append_truncated.h"
#include "absl/log/internal/config.h" #include "absl/log/internal/config.h"
#include "absl/log/internal/globals.h" #include "absl/log/internal/globals.h"
#include "absl/strings/numbers.h" #include "absl/strings/numbers.h"
@ -143,15 +144,6 @@ size_t FormatBoundedFields(absl::LogSeverity severity, absl::Time timestamp,
return bytes_formatted; return bytes_formatted;
} }
// Copies into `dst` as many bytes of `src` as will fit, then advances `dst`
// past the copied bytes and returns the number of bytes written.
size_t AppendTruncated(absl::string_view src, absl::Span<char>& dst) {
if (src.size() > dst.size()) src = src.substr(0, dst.size());
memcpy(dst.data(), src.data(), src.size());
dst.remove_prefix(src.size());
return src.size();
}
size_t FormatLineNumber(int line, absl::Span<char>& buf) { size_t FormatLineNumber(int line, absl::Span<char>& buf) {
constexpr size_t kLineFieldMaxLen = constexpr size_t kLineFieldMaxLen =
sizeof(":] ") + (1 + std::numeric_limits<int>::digits10 + 1) - sizeof(""); sizeof(":] ") + (1 + std::numeric_limits<int>::digits10 + 1) - sizeof("");
@ -199,7 +191,7 @@ size_t FormatLogPrefix(absl::LogSeverity severity, absl::Time timestamp,
log_internal::Tid tid, absl::string_view basename, log_internal::Tid tid, absl::string_view basename,
int line, absl::Span<char>& buf) { int line, absl::Span<char>& buf) {
auto prefix_size = FormatBoundedFields(severity, timestamp, tid, buf); auto prefix_size = FormatBoundedFields(severity, timestamp, tid, buf);
prefix_size += AppendTruncated(basename, buf); prefix_size += log_internal::AppendTruncated(basename, buf);
prefix_size += FormatLineNumber(line, buf); prefix_size += FormatLineNumber(line, buf);
return prefix_size; return prefix_size;
} }

@ -41,6 +41,7 @@
#include "absl/container/inlined_vector.h" #include "absl/container/inlined_vector.h"
#include "absl/debugging/internal/examine_stack.h" #include "absl/debugging/internal/examine_stack.h"
#include "absl/log/globals.h" #include "absl/log/globals.h"
#include "absl/log/internal/append_truncated.h"
#include "absl/log/internal/config.h" #include "absl/log/internal/config.h"
#include "absl/log/internal/globals.h" #include "absl/log/internal/globals.h"
#include "absl/log/internal/log_format.h" #include "absl/log/internal/log_format.h"
@ -65,14 +66,6 @@ ABSL_NAMESPACE_BEGIN
namespace log_internal { namespace log_internal {
namespace { namespace {
// Copies into `dst` as many bytes of `src` as will fit, then truncates the
// copied bytes from the front of `dst` and returns the number of bytes written.
size_t AppendTruncated(absl::string_view src, absl::Span<char>* dst) {
if (src.size() > dst->size()) src = src.substr(0, dst->size());
memcpy(dst->data(), src.data(), src.size());
dst->remove_prefix(src.size());
return src.size();
}
absl::string_view Basename(absl::string_view filepath) { absl::string_view Basename(absl::string_view filepath) {
#ifdef _WIN32 #ifdef _WIN32
@ -163,7 +156,7 @@ class LogEntryStreambuf final : public std::streambuf {
size_t Append(absl::string_view data) { size_t Append(absl::string_view data) {
absl::Span<char> remaining(pptr(), static_cast<size_t>(epptr() - pptr())); absl::Span<char> remaining(pptr(), static_cast<size_t>(epptr() - pptr()));
const size_t written = AppendTruncated(data, &remaining); const size_t written = log_internal::AppendTruncated(data, remaining);
pbump(static_cast<int>(written)); pbump(static_cast<int>(written));
return written; return written;
} }

@ -30,6 +30,7 @@
#include "absl/base/attributes.h" #include "absl/base/attributes.h"
#include "absl/base/config.h" #include "absl/base/config.h"
#include "absl/base/log_severity.h" #include "absl/base/log_severity.h"
#include "absl/log/internal/append_truncated.h"
#include "absl/log/internal/log_format.h" #include "absl/log/internal/log_format.h"
#include "absl/log/internal/test_helpers.h" #include "absl/log/internal/test_helpers.h"
#include "absl/strings/numbers.h" #include "absl/strings/numbers.h"
@ -40,7 +41,6 @@
#include "absl/types/span.h" #include "absl/types/span.h"
namespace { namespace {
using ::absl::log_internal::LogEntryTestPeer; using ::absl::log_internal::LogEntryTestPeer;
using ::testing::Eq; using ::testing::Eq;
using ::testing::IsTrue; using ::testing::IsTrue;
@ -49,16 +49,6 @@ using ::testing::StrEq;
auto* test_env ABSL_ATTRIBUTE_UNUSED = ::testing::AddGlobalTestEnvironment( auto* test_env ABSL_ATTRIBUTE_UNUSED = ::testing::AddGlobalTestEnvironment(
new absl::log_internal::LogTestEnvironment); new absl::log_internal::LogTestEnvironment);
// Copies into `dst` as many bytes of `src` as will fit, then truncates the
// copied bytes from the front of `dst` and returns the number of bytes written.
size_t AppendTruncated(absl::string_view src, absl::Span<char>& dst) {
if (src.size() > dst.size()) src = src.substr(0, dst.size());
memcpy(dst.data(), src.data(), src.size());
dst.remove_prefix(src.size());
return src.size();
}
} // namespace } // namespace
namespace absl { namespace absl {
@ -103,7 +93,7 @@ class LogEntryTestPeer {
EXPECT_THAT(entry_.prefix_len_, EXPECT_THAT(entry_.prefix_len_,
Eq(static_cast<size_t>(view.data() - buf_.data()))); Eq(static_cast<size_t>(view.data() - buf_.data())));
AppendTruncated(text_message, view); log_internal::AppendTruncated(text_message, view);
view = absl::Span<char>(view.data(), view.size() + 2); view = absl::Span<char>(view.data(), view.size() + 2);
view[0] = '\n'; view[0] = '\n';
view[1] = '\0'; view[1] = '\0';

Loading…
Cancel
Save