From f75ab82b46cabd0a4ac65b0ca1f4cd7c6c90ec7f Mon Sep 17 00:00:00 2001 From: Danil Chapovalov Date: Thu, 3 Oct 2024 11:45:27 +0200 Subject: [PATCH] Support RTC_LOG for types that implement both AbslStringify and ToLogString To support libraries and dependencies compatible with absl way of debug printing custom types. In particular gtest can use AbslStringify to produce nice output when unit types are compared with EXPECT macros. Bug: None Change-Id: Ie78293a225f61977f256f0234e07d166b1977e2d Reviewed-on: https://webrtc-review.googlesource.com/c/src/+/364162 Reviewed-by: Harald Alvestrand Commit-Queue: Danil Chapovalov Cr-Commit-Position: refs/heads/main@{#43164} --- api/units/BUILD.gn | 5 +++++ api/units/data_rate.h | 11 ++++++++++- api/units/data_size.h | 11 ++++++++++- api/units/frequency.h | 11 ++++++++++- api/units/time_delta.h | 11 ++++++++++- api/units/timestamp.h | 11 ++++++++++- modules/pacing/pacing_controller.cc | 4 ++-- .../source/rtcp_packet/transport_feedback.cc | 5 ++--- rtc_base/BUILD.gn | 2 ++ rtc_base/checks.h | 6 ++++-- rtc_base/logging.h | 12 +++--------- rtc_base/type_traits.h | 11 +++++++++++ 12 files changed, 79 insertions(+), 21 deletions(-) diff --git a/api/units/BUILD.gn b/api/units/BUILD.gn index 9d4f76179b..85d6d1d4e9 100644 --- a/api/units/BUILD.gn +++ b/api/units/BUILD.gn @@ -22,6 +22,7 @@ rtc_library("data_rate") { "..:array_view", "../../rtc_base:checks", "../../rtc_base:stringutils", + "../../rtc_base/system:rtc_export", "../../rtc_base/units:unit_base", ] } @@ -37,6 +38,7 @@ rtc_library("data_size") { "..:array_view", "../../rtc_base:checks", "../../rtc_base:stringutils", + "../../rtc_base/system:rtc_export", "../../rtc_base/units:unit_base", ] } @@ -52,6 +54,7 @@ rtc_library("time_delta") { "..:array_view", "../../rtc_base:checks", "../../rtc_base:stringutils", + "../../rtc_base/system:rtc_export", "../../rtc_base/units:unit_base", ] } @@ -68,6 +71,7 @@ rtc_library("frequency") { "..:array_view", "../../rtc_base:checks", "../../rtc_base:stringutils", + "../../rtc_base/system:rtc_export", "../../rtc_base/units:unit_base", ] } @@ -84,6 +88,7 @@ rtc_library("timestamp") { "..:array_view", "../../rtc_base:checks", "../../rtc_base:stringutils", + "../../rtc_base/system:rtc_export", "../../rtc_base/units:unit_base", ] } diff --git a/api/units/data_rate.h b/api/units/data_rate.h index 9a7d5805b1..341f74e338 100644 --- a/api/units/data_rate.h +++ b/api/units/data_rate.h @@ -20,6 +20,7 @@ #include "api/units/frequency.h" #include "api/units/time_delta.h" #include "rtc_base/checks.h" +#include "rtc_base/system/rtc_export.h" #include "rtc_base/units/unit_base.h" // IWYU pragma: export namespace webrtc { @@ -47,6 +48,9 @@ class DataRate final : public rtc_units_impl::RelativeUnit { DataRate() = delete; + template + friend void AbslStringify(Sink& sink, DataRate value); + template constexpr T bps() const { return ToValue(); @@ -134,11 +138,16 @@ inline constexpr DataRate operator*(const Frequency frequency, return size * frequency; } -std::string ToString(DataRate value); +RTC_EXPORT std::string ToString(DataRate value); inline std::string ToLogString(DataRate value) { return ToString(value); } +template +void AbslStringify(Sink& sink, DataRate value) { + sink.Append(ToString(value)); +} + } // namespace webrtc #endif // API_UNITS_DATA_RATE_H_ diff --git a/api/units/data_size.h b/api/units/data_size.h index 8d55a127c2..409df61daf 100644 --- a/api/units/data_size.h +++ b/api/units/data_size.h @@ -15,6 +15,7 @@ #include #include +#include "rtc_base/system/rtc_export.h" #include "rtc_base/units/unit_base.h" // IWYU pragma: export namespace webrtc { @@ -30,6 +31,9 @@ class DataSize final : public rtc_units_impl::RelativeUnit { DataSize() = delete; + template + friend void AbslStringify(Sink& sink, DataSize value); + template constexpr T bytes() const { return ToValue(); @@ -45,11 +49,16 @@ class DataSize final : public rtc_units_impl::RelativeUnit { static constexpr bool one_sided = true; }; -std::string ToString(DataSize value); +RTC_EXPORT std::string ToString(DataSize value); inline std::string ToLogString(DataSize value) { return ToString(value); } +template +void AbslStringify(Sink& sink, DataSize value) { + sink.Append(ToString(value)); +} + } // namespace webrtc #endif // API_UNITS_DATA_SIZE_H_ diff --git a/api/units/frequency.h b/api/units/frequency.h index 0faa388165..225db55500 100644 --- a/api/units/frequency.h +++ b/api/units/frequency.h @@ -18,6 +18,7 @@ #include "api/units/time_delta.h" #include "rtc_base/checks.h" +#include "rtc_base/system/rtc_export.h" #include "rtc_base/units/unit_base.h" // IWYU pragma: export namespace webrtc { @@ -42,6 +43,9 @@ class Frequency final : public rtc_units_impl::RelativeUnit { Frequency() = delete; + template + friend void AbslStringify(Sink& sink, Frequency value); + template constexpr T hertz() const { return ToFraction<1000, T>(); @@ -82,10 +86,15 @@ inline constexpr double operator*(TimeDelta time_delta, Frequency frequency) { return frequency * time_delta; } -std::string ToString(Frequency value); +RTC_EXPORT std::string ToString(Frequency value); inline std::string ToLogString(Frequency value) { return ToString(value); } +template +void AbslStringify(Sink& sink, Frequency value) { + sink.Append(ToString(value)); +} + } // namespace webrtc #endif // API_UNITS_FREQUENCY_H_ diff --git a/api/units/time_delta.h b/api/units/time_delta.h index 33b06c546c..c00b443014 100644 --- a/api/units/time_delta.h +++ b/api/units/time_delta.h @@ -16,6 +16,7 @@ #include #include +#include "rtc_base/system/rtc_export.h" #include "rtc_base/units/unit_base.h" // IWYU pragma: export namespace webrtc { @@ -52,6 +53,9 @@ class TimeDelta final : public rtc_units_impl::RelativeUnit { TimeDelta() = delete; + template + friend void AbslStringify(Sink& sink, TimeDelta value); + template constexpr T seconds() const { return ToFraction<1000000, T>(); @@ -89,11 +93,16 @@ class TimeDelta final : public rtc_units_impl::RelativeUnit { static constexpr bool one_sided = false; }; -std::string ToString(TimeDelta value); +RTC_EXPORT std::string ToString(TimeDelta value); inline std::string ToLogString(TimeDelta value) { return ToString(value); } +template +void AbslStringify(Sink& sink, TimeDelta value) { + sink.Append(ToString(value)); +} + } // namespace webrtc #endif // API_UNITS_TIME_DELTA_H_ diff --git a/api/units/timestamp.h b/api/units/timestamp.h index abc731e72e..177bea11af 100644 --- a/api/units/timestamp.h +++ b/api/units/timestamp.h @@ -17,6 +17,7 @@ #include "api/units/time_delta.h" #include "rtc_base/checks.h" +#include "rtc_base/system/rtc_export.h" #include "rtc_base/units/unit_base.h" // IWYU pragma: export namespace webrtc { @@ -44,6 +45,9 @@ class Timestamp final : public rtc_units_impl::UnitBase { Timestamp() = delete; + template + friend void AbslStringify(Sink& sink, Timestamp value); + template constexpr T seconds() const { return ToFraction<1000000, T>(); @@ -118,11 +122,16 @@ class Timestamp final : public rtc_units_impl::UnitBase { static constexpr bool one_sided = true; }; -std::string ToString(Timestamp value); +RTC_EXPORT std::string ToString(Timestamp value); inline std::string ToLogString(Timestamp value) { return ToString(value); } +template +void AbslStringify(Sink& sink, Timestamp value) { + sink.Append(ToString(value)); +} + } // namespace webrtc #endif // API_UNITS_TIMESTAMP_H_ diff --git a/modules/pacing/pacing_controller.cc b/modules/pacing/pacing_controller.cc index 011b1c8462..ed07ff2454 100644 --- a/modules/pacing/pacing_controller.cc +++ b/modules/pacing/pacing_controller.cc @@ -310,9 +310,9 @@ TimeDelta PacingController::UpdateTimeAndGetElapsed(Timestamp now) { TimeDelta elapsed_time = now - last_process_time_; last_process_time_ = now; if (elapsed_time > kMaxElapsedTime) { - RTC_LOG(LS_WARNING) << "Elapsed time (" << ToLogString(elapsed_time) + RTC_LOG(LS_WARNING) << "Elapsed time (" << elapsed_time << ") longer than expected, limiting to " - << ToLogString(kMaxElapsedTime); + << kMaxElapsedTime; elapsed_time = kMaxElapsedTime; } return elapsed_time; diff --git a/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.cc b/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.cc index 003effad29..049c3175a2 100644 --- a/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.cc +++ b/modules/rtp_rtcp/source/rtcp_packet/transport_feedback.cc @@ -582,9 +582,8 @@ bool TransportFeedback::IsConsistent() const { return false; } if (timestamp != last_timestamp_) { - RTC_LOG(LS_ERROR) << "Last timestamp mismatch. Calculated: " - << ToLogString(timestamp) - << ". Saved: " << ToLogString(last_timestamp_); + RTC_LOG(LS_ERROR) << "Last timestamp mismatch. Calculated: " << timestamp + << ". Saved: " << last_timestamp_; return false; } if (size_bytes_ != packet_size) { diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn index 804d7aee3c..a18e69590d 100644 --- a/rtc_base/BUILD.gn +++ b/rtc_base/BUILD.gn @@ -437,6 +437,7 @@ rtc_library("logging") { ":platform_thread_types", ":stringutils", ":timeutils", + ":type_traits", "../api/units:timestamp", "synchronization:mutex", "//third_party/abseil-cpp/absl/base:core_headers", @@ -486,6 +487,7 @@ rtc_library("checks") { ] deps = [ ":safe_compare", + ":type_traits", "../api:scoped_refptr", "system:inline", "system:rtc_export", diff --git a/rtc_base/checks.h b/rtc_base/checks.h index 130b8b9259..f82078955f 100644 --- a/rtc_base/checks.h +++ b/rtc_base/checks.h @@ -62,6 +62,7 @@ RTC_NORETURN void rtc_FatalMessage(const char* file, int line, const char* msg); #include "rtc_base/numerics/safe_compare.h" #include "rtc_base/system/inline.h" #include "rtc_base/system/rtc_export.h" +#include "rtc_base/type_traits.h" // The macros here print a message to stderr and abort under various // conditions. All will accept additional stream messages. For example: @@ -217,13 +218,14 @@ inline decltype(MakeVal(std::declval>())) MakeVal( return {static_cast>(x)}; } -template ()))* = nullptr> +template >* = nullptr> ToStringVal MakeVal(const T& x) { return {ToLogString(x)}; } template ::value>* = nullptr> + std::enable_if_t::value && + !has_to_log_string_v>* = nullptr> ToStringVal MakeVal(const T& x) { return {absl::StrCat(x)}; } diff --git a/rtc_base/logging.h b/rtc_base/logging.h index 208e8b9172..d5be6b19ba 100644 --- a/rtc_base/logging.h +++ b/rtc_base/logging.h @@ -66,6 +66,7 @@ #include "rtc_base/platform_thread_types.h" #include "rtc_base/strings/string_builder.h" #include "rtc_base/system/inline.h" +#include "rtc_base/type_traits.h" #if !defined(NDEBUG) || defined(DLOG_ALWAYS_ON) #define RTC_DLOG_IS_ON 1 @@ -330,21 +331,14 @@ inline Val MakeVal( } #endif -template -struct has_to_log_string : std::false_type {}; -template -struct has_to_log_string())), - std::string>::value>> : std::true_type {}; - template ::value>* = nullptr> ToStringVal MakeVal(const T& x) { return {ToLogString(x)}; } template ::value>* = nullptr> + std::enable_if_t::value && + !has_to_log_string::value>* = nullptr> ToStringVal MakeVal(const T& x) { return {absl::StrCat(x)}; } diff --git a/rtc_base/type_traits.h b/rtc_base/type_traits.h index 0cb899c47f..e040e60837 100644 --- a/rtc_base/type_traits.h +++ b/rtc_base/type_traits.h @@ -12,6 +12,7 @@ #define RTC_BASE_TYPE_TRAITS_H_ #include +#include #include namespace rtc { @@ -36,6 +37,16 @@ class HasDataAndSize { static constexpr bool value = std::is_same(0)), int>::value; }; +template +struct has_to_log_string : std::false_type {}; +template +struct has_to_log_string())), + std::string>>> : std::true_type {}; +template +constexpr bool has_to_log_string_v = has_to_log_string::value; + namespace test_has_data_and_size { template