Commit 13708db8 by Andy Getzendanner Committed by Copybara-Service

Write (more) directly into the structured buffer from StringifySink, including…

Write (more) directly into the structured buffer from StringifySink, including for (size_t, char) overload.

PiperOrigin-RevId: 491456410
Change-Id: I76dec24b0bd02204fa38419af9247cee38b1cf50
parent 558a0e46
...@@ -93,7 +93,7 @@ TEST(CHECKTest, TestBoolConvertible) { ...@@ -93,7 +93,7 @@ TEST(CHECKTest, TestBoolConvertible) {
#if GTEST_HAS_DEATH_TEST #if GTEST_HAS_DEATH_TEST
TEST(CHECKDeathTest, TestChecksWithSideeffects) { TEST(CHECKDeathTest, TestChecksWithSideEffects) {
int var = 0; int var = 0;
CHECK([&var]() { CHECK([&var]() {
++var; ++var;
......
...@@ -33,6 +33,13 @@ inline size_t AppendTruncated(absl::string_view src, absl::Span<char> &dst) { ...@@ -33,6 +33,13 @@ inline size_t AppendTruncated(absl::string_view src, absl::Span<char> &dst) {
dst.remove_prefix(src.size()); dst.remove_prefix(src.size());
return src.size(); return src.size();
} }
// Likewise, but `n` copies of `c`.
inline size_t AppendTruncated(char c, size_t n, absl::Span<char> &dst) {
if (n > dst.size()) n = dst.size();
memset(dst.data(), c, n);
dst.remove_prefix(n);
return n;
}
} // namespace log_internal } // namespace log_internal
ABSL_NAMESPACE_END ABSL_NAMESPACE_END
} // namespace absl } // namespace absl
......
...@@ -540,6 +540,27 @@ void LogMessage::CopyToEncodedBuffer(absl::string_view str, ...@@ -540,6 +540,27 @@ void LogMessage::CopyToEncodedBuffer(absl::string_view str,
data_->encoded_remaining.remove_suffix(data_->encoded_remaining.size()); data_->encoded_remaining.remove_suffix(data_->encoded_remaining.size());
} }
} }
void LogMessage::CopyToEncodedBuffer(char ch, size_t num, StringType str_type) {
auto encoded_remaining_copy = data_->encoded_remaining;
auto value_start = EncodeMessageStart(
EventTag::kValue, BufferSizeFor(WireType::kLengthDelimited) + num,
&encoded_remaining_copy);
auto str_start = EncodeMessageStart(str_type == StringType::kLiteral
? ValueTag::kStringLiteral
: ValueTag::kString,
num, &encoded_remaining_copy);
if (str_start.data()) {
// The field headers fit.
log_internal::AppendTruncated(ch, num, encoded_remaining_copy);
EncodeMessageLength(str_start, &encoded_remaining_copy);
EncodeMessageLength(value_start, &encoded_remaining_copy);
data_->encoded_remaining = encoded_remaining_copy;
} else {
// The field header(s) did not fit; zero `encoded_remaining` so we don't
// write anything else later.
data_->encoded_remaining.remove_suffix(data_->encoded_remaining.size());
}
}
LogMessageFatal::LogMessageFatal(const char* file, int line) LogMessageFatal::LogMessageFatal(const char* file, int line)
: LogMessage(file, line, absl::LogSeverity::kFatal) {} : LogMessage(file, line, absl::LogSeverity::kFatal) {}
......
...@@ -47,8 +47,6 @@ ...@@ -47,8 +47,6 @@
namespace absl { namespace absl {
ABSL_NAMESPACE_BEGIN ABSL_NAMESPACE_BEGIN
namespace log_internal { namespace log_internal {
class AsLiteralImpl;
constexpr int kLogMessageBufferSize = 15000; constexpr int kLogMessageBufferSize = 15000;
class LogMessage { class LogMessage {
...@@ -195,6 +193,7 @@ class LogMessage { ...@@ -195,6 +193,7 @@ class LogMessage {
private: private:
struct LogMessageData; // Opaque type containing message state struct LogMessageData; // Opaque type containing message state
friend class AsLiteralImpl; friend class AsLiteralImpl;
friend class StringifySink;
// This streambuf writes directly into the structured logging buffer so that // This streambuf writes directly into the structured logging buffer so that
// arbitrary types can be encoded as string data (using // arbitrary types can be encoded as string data (using
...@@ -222,6 +221,8 @@ class LogMessage { ...@@ -222,6 +221,8 @@ class LogMessage {
}; };
void CopyToEncodedBuffer(absl::string_view str, void CopyToEncodedBuffer(absl::string_view str,
StringType str_type) ABSL_ATTRIBUTE_NOINLINE; StringType str_type) ABSL_ATTRIBUTE_NOINLINE;
void CopyToEncodedBuffer(char ch, size_t num,
StringType str_type) ABSL_ATTRIBUTE_NOINLINE;
// Returns `true` if the message is fatal or enabled debug-fatal. // Returns `true` if the message is fatal or enabled debug-fatal.
bool IsFatal() const; bool IsFatal() const;
...@@ -250,9 +251,14 @@ class StringifySink final { ...@@ -250,9 +251,14 @@ class StringifySink final {
public: public:
explicit StringifySink(LogMessage& message) : message_(message) {} explicit StringifySink(LogMessage& message) : message_(message) {}
void Append(size_t count, char ch) { message_ << std::string(count, ch); } void Append(size_t count, char ch) {
message_.CopyToEncodedBuffer(ch, count,
LogMessage::StringType::kNotLiteral);
}
void Append(absl::string_view v) { message_ << v; } void Append(absl::string_view v) {
message_.CopyToEncodedBuffer(v, LogMessage::StringType::kNotLiteral);
}
// For types that implement `AbslStringify` using `absl::Format()`. // For types that implement `AbslStringify` using `absl::Format()`.
friend void AbslFormatFlush(StringifySink* sink, absl::string_view v) { friend void AbslFormatFlush(StringifySink* sink, absl::string_view v) {
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment