From 32e928d29c5edb8a196e876d71a4bb124bff9de2 Mon Sep 17 00:00:00 2001 From: Andy Getzendanner Date: Thu, 3 Nov 2022 18:18:19 -0700 Subject: [PATCH] Small cleanups in logging test helpers PiperOrigin-RevId: 486023797 Change-Id: I0b82754fd40c2d6fb451f304aaf1d795eed8d009 --- absl/log/internal/BUILD.bazel | 2 ++ absl/log/internal/test_actions.cc | 47 ++++++++++++++++-------------- absl/log/internal/test_matchers.cc | 2 ++ 3 files changed, 29 insertions(+), 22 deletions(-) diff --git a/absl/log/internal/BUILD.bazel b/absl/log/internal/BUILD.bazel index 19243a58..3ddae933 100644 --- a/absl/log/internal/BUILD.bazel +++ b/absl/log/internal/BUILD.bazel @@ -230,6 +230,7 @@ cc_library( deps = [ ":config", "//absl/base:config", + "//absl/base:core_headers", "//absl/base:log_severity", "//absl/log:log_entry", "//absl/strings", @@ -265,6 +266,7 @@ cc_library( ":config", ":test_helpers", "//absl/base:config", + "//absl/base:core_headers", "//absl/base:log_severity", "//absl/log:log_entry", "//absl/strings", diff --git a/absl/log/internal/test_actions.cc b/absl/log/internal/test_actions.cc index 41ca9887..c0d04e18 100644 --- a/absl/log/internal/test_actions.cc +++ b/absl/log/internal/test_actions.cc @@ -19,9 +19,11 @@ #include #include +#include "absl/base/attributes.h" #include "absl/base/config.h" #include "absl/log/internal/config.h" #include "absl/strings/escaping.h" +#include "absl/strings/str_cat.h" #include "absl/strings/string_view.h" #include "absl/time/time.h" @@ -30,37 +32,38 @@ ABSL_NAMESPACE_BEGIN namespace log_internal { void WriteToStderrWithFilename::operator()(const absl::LogEntry& entry) const { - std::cerr << message << " (file: " << entry.source_filename() << ")" - << std::endl; + std::cerr << message << " (file: " << entry.source_filename() << ")\n"; } void WriteEntryToStderr::operator()(const absl::LogEntry& entry) const { - if (!message.empty()) std::cerr << message << std::endl; + if (!message.empty()) std::cerr << message << "\n"; - std::cerr << "LogEntry{\n" - << " source_filename: \"" - << absl::CHexEscape(entry.source_filename()) << "\"\n" - << " source_basename: \"" - << absl::CHexEscape(entry.source_basename()) << "\"\n" - << " source_line: " << entry.source_line() << "\n" - << " prefix: " << (entry.prefix() ? "true\n" : "false\n") - << " log_severity: " << entry.log_severity() << "\n" - << " timestamp: " << entry.timestamp() << "\n" - << " text_message: \"" << absl::CHexEscape(entry.text_message()) - << "\"\n verbosity: " << entry.verbosity() << "\n" - << "}" << std::endl; + const std::string source_filename = absl::CHexEscape(entry.source_filename()); + const std::string source_basename = absl::CHexEscape(entry.source_basename()); + const std::string text_message = absl::CHexEscape(entry.text_message()); + std::cerr << "LogEntry{\n" // + << " source_filename: \"" << source_filename << "\"\n" // + << " source_basename: \"" << source_basename << "\"\n" // + << " source_line: " << entry.source_line() << "\n" // + << " prefix: " << (entry.prefix() ? "true\n" : "false\n") // + << " log_severity: " << entry.log_severity() << "\n" // + << " timestamp: " << entry.timestamp() << "\n" // + << " text_message: \"" << text_message << "\"\n" // + << " verbosity: " << entry.verbosity() << "\n" // + << "}\n"; } void WriteEntryToStderr::operator()(absl::LogSeverity severity, absl::string_view filename, absl::string_view log_message) const { - if (!message.empty()) std::cerr << message << std::endl; - - std::cerr << "LogEntry{\n" - << " source_filename: \"" << absl::CHexEscape(filename) << "\"\n" - << " log_severity: " << severity << "\n" - << " text_message: \"" << absl::CHexEscape(log_message) << "}" - << std::endl; + if (!message.empty()) std::cerr << message << "\n"; + const std::string source_filename = absl::CHexEscape(filename); + const std::string text_message = absl::CHexEscape(log_message); + std::cerr << "LogEntry{\n" // + << " source_filename: \"" << source_filename << "\"\n" // + << " log_severity: " << severity << "\n" // + << " text_message: \"" << text_message << "\"\n" // + << "}\n"; } } // namespace log_internal diff --git a/absl/log/internal/test_matchers.cc b/absl/log/internal/test_matchers.cc index ee32617b..10f4111f 100644 --- a/absl/log/internal/test_matchers.cc +++ b/absl/log/internal/test_matchers.cc @@ -17,10 +17,12 @@ #include #include +#include #include #include "gmock/gmock.h" #include "gtest/gtest.h" +#include "absl/base/attributes.h" #include "absl/base/config.h" #include "absl/log/internal/config.h" #include "absl/log/internal/test_helpers.h"