diff --git a/rtc_base/logging.h b/rtc_base/logging.h index a1339aaee5..13738f0d0e 100644 --- a/rtc_base/logging.h +++ b/rtc_base/logging.h @@ -49,6 +49,7 @@ #include #include // no-presubmit-check TODO(webrtc:8982) #include +#include #include #include "absl/base/attributes.h" @@ -277,8 +278,15 @@ inline Val MakeVal( template struct has_to_log_string : std::false_type {}; template -struct has_to_log_string()))> - : std::true_type {}; +struct has_to_log_string())), + std::string>::value>> : std::true_type {}; + +template ::value>* = nullptr> +ToStringVal MakeVal(const T& x) { + return {ToLogString(x)}; +} // Handle arbitrary types other than the above by falling back to stringstream. // TODO(bugs.webrtc.org/9278): Get rid of this overload when callers don't need @@ -300,11 +308,6 @@ ToStringVal MakeVal(const T& x) { return {os.str()}; } -template ::value>* = nullptr> -ToStringVal MakeVal(const T& x) { - return {ToLogString(x)}; -} - #if RTC_LOG_ENABLED() void Log(const LogArgType* fmt, ...); #else diff --git a/rtc_base/logging_unittest.cc b/rtc_base/logging_unittest.cc index cd8d753fb3..c267778951 100644 --- a/rtc_base/logging_unittest.cc +++ b/rtc_base/logging_unittest.cc @@ -22,6 +22,7 @@ #include "rtc_base/event.h" #include "rtc_base/platform_thread.h" #include "rtc_base/time_utils.h" +#include "test/gmock.h" #include "test/gtest.h" namespace rtc { @@ -299,5 +300,20 @@ TEST(LogTest, NoopSeverityDoesNotRunStringFormatting) { EXPECT_FALSE(was_called); } +struct TestStruct {}; +std::string ToLogString(TestStruct foo) { + return "bar"; +} + +TEST(LogTest, ToLogStringUsedForUnknownTypes) { + std::string str; + LogSinkImpl stream(&str); + LogMessage::AddLogToStream(&stream, LS_INFO); + TestStruct t; + RTC_LOG(LS_INFO) << t; + EXPECT_THAT(str, ::testing::HasSubstr("bar")); + LogMessage::RemoveLogToStream(&stream); +} + } // namespace rtc #endif // RTC_LOG_ENABLED()