From 615f2147028688b5e36f159ccd2958552488215c Mon Sep 17 00:00:00 2001 From: Andy Getzendanner Date: Mon, 7 Nov 2022 14:12:00 -0800 Subject: 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 --- absl/log/BUILD.bazel | 1 + absl/log/CMakeLists.txt | 19 +++++++++++++++++ absl/log/internal/BUILD.bazel | 14 +++++++++++++ absl/log/internal/append_truncated.h | 40 ++++++++++++++++++++++++++++++++++++ absl/log/internal/log_format.cc | 12 ++--------- absl/log/internal/log_message.cc | 11 ++-------- absl/log/log_entry_test.cc | 14 ++----------- 7 files changed, 80 insertions(+), 31 deletions(-) create mode 100644 absl/log/internal/append_truncated.h (limited to 'absl/log') diff --git a/absl/log/BUILD.bazel b/absl/log/BUILD.bazel index 16788ae2..261a3384 100644 --- a/absl/log/BUILD.bazel +++ b/absl/log/BUILD.bazel @@ -303,6 +303,7 @@ cc_test( "//absl/base:config", "//absl/base:core_headers", "//absl/base:log_severity", + "//absl/log/internal:append_truncated", "//absl/log/internal:format", "//absl/log/internal:test_helpers", "//absl/strings", diff --git a/absl/log/CMakeLists.txt b/absl/log/CMakeLists.txt index 28d4b519..f5b608ce 100644 --- a/absl/log/CMakeLists.txt +++ b/absl/log/CMakeLists.txt @@ -96,6 +96,7 @@ absl_cc_library( DEPS absl::config absl::core_headers + absl::log_internal_append_truncated absl::log_internal_config absl::log_internal_globals absl::log_severity @@ -143,6 +144,7 @@ absl_cc_library( absl::errno_saver absl::inlined_vector absl::examine_stack + absl::log_internal_append_truncated absl::log_internal_config absl::log_internal_format absl::log_internal_globals @@ -318,6 +320,22 @@ absl_cc_library( 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 absl_cc_library( NAME @@ -636,6 +654,7 @@ absl_cc_test( absl::config absl::core_headers absl::log_entry + absl::log_internal_append_truncated absl::log_internal_format absl::log_internal_globals absl::log_internal_test_helpers diff --git a/absl/log/internal/BUILD.bazel b/absl/log/internal/BUILD.bazel index 3ddae933..af0a4b88 100644 --- a/absl/log/internal/BUILD.bazel +++ b/absl/log/internal/BUILD.bazel @@ -91,6 +91,7 @@ cc_library( copts = ABSL_DEFAULT_COPTS, linkopts = ABSL_DEFAULT_LINKOPTS, deps = [ + ":append_truncated", ":config", ":globals", "//absl/base:config", @@ -132,6 +133,7 @@ cc_library( "//absl/log:__pkg__", ], deps = [ + ":append_truncated", ":config", ":format", ":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( name = "log_sink_set", srcs = ["log_sink_set.cc"], diff --git a/absl/log/internal/append_truncated.h b/absl/log/internal/append_truncated.h new file mode 100644 index 00000000..096b7517 --- /dev/null +++ b/absl/log/internal/append_truncated.h @@ -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 +#include + +#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 &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_ diff --git a/absl/log/internal/log_format.cc b/absl/log/internal/log_format.cc index 5b280a2d..cf8cdfd9 100644 --- a/absl/log/internal/log_format.cc +++ b/absl/log/internal/log_format.cc @@ -32,6 +32,7 @@ #include "absl/base/config.h" #include "absl/base/log_severity.h" #include "absl/base/optimization.h" +#include "absl/log/internal/append_truncated.h" #include "absl/log/internal/config.h" #include "absl/log/internal/globals.h" #include "absl/strings/numbers.h" @@ -143,15 +144,6 @@ size_t FormatBoundedFields(absl::LogSeverity severity, absl::Time timestamp, 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& 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& buf) { constexpr size_t kLineFieldMaxLen = sizeof(":] ") + (1 + std::numeric_limits::digits10 + 1) - sizeof(""); @@ -199,7 +191,7 @@ size_t FormatLogPrefix(absl::LogSeverity severity, absl::Time timestamp, log_internal::Tid tid, absl::string_view basename, int line, absl::Span& 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); return prefix_size; } diff --git a/absl/log/internal/log_message.cc b/absl/log/internal/log_message.cc index f32c7e63..98e45f87 100644 --- a/absl/log/internal/log_message.cc +++ b/absl/log/internal/log_message.cc @@ -41,6 +41,7 @@ #include "absl/container/inlined_vector.h" #include "absl/debugging/internal/examine_stack.h" #include "absl/log/globals.h" +#include "absl/log/internal/append_truncated.h" #include "absl/log/internal/config.h" #include "absl/log/internal/globals.h" #include "absl/log/internal/log_format.h" @@ -65,14 +66,6 @@ ABSL_NAMESPACE_BEGIN namespace log_internal { 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* 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) { #ifdef _WIN32 @@ -163,7 +156,7 @@ class LogEntryStreambuf final : public std::streambuf { size_t Append(absl::string_view data) { absl::Span remaining(pptr(), static_cast(epptr() - pptr())); - const size_t written = AppendTruncated(data, &remaining); + const size_t written = log_internal::AppendTruncated(data, remaining); pbump(static_cast(written)); return written; } diff --git a/absl/log/log_entry_test.cc b/absl/log/log_entry_test.cc index 7238356e..b1b21c0b 100644 --- a/absl/log/log_entry_test.cc +++ b/absl/log/log_entry_test.cc @@ -30,6 +30,7 @@ #include "absl/base/attributes.h" #include "absl/base/config.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/test_helpers.h" #include "absl/strings/numbers.h" @@ -40,7 +41,6 @@ #include "absl/types/span.h" namespace { - using ::absl::log_internal::LogEntryTestPeer; using ::testing::Eq; using ::testing::IsTrue; @@ -49,16 +49,6 @@ using ::testing::StrEq; auto* test_env ABSL_ATTRIBUTE_UNUSED = ::testing::AddGlobalTestEnvironment( 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& 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 absl { @@ -103,7 +93,7 @@ class LogEntryTestPeer { EXPECT_THAT(entry_.prefix_len_, Eq(static_cast(view.data() - buf_.data()))); - AppendTruncated(text_message, view); + log_internal::AppendTruncated(text_message, view); view = absl::Span(view.data(), view.size() + 2); view[0] = '\n'; view[1] = '\0'; -- cgit v1.2.3