RTC_LOG(): Internally, pass logging severity as template argument

When the logging severity is statically known, passing it as a
template argument instead of as a function argument saves space at the
call site.

In aggregate, this reduces the size of libjingle_peerconnection_so.so
by 8 kB.

Bug: webrtc:9185
Change-Id: I9ca363845216370e97b230952c86e6d07719962f
Reviewed-on: https://webrtc-review.googlesource.com/74480
Reviewed-by: Fredrik Solenberg <solenberg@webrtc.org>
Reviewed-by: Jonas Olsson <jonasolsson@webrtc.org>
Commit-Queue: Karl Wiberg <kwiberg@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#23121}
diff --git a/rtc_base/BUILD.gn b/rtc_base/BUILD.gn
index 35c9da8..c3d0546 100644
--- a/rtc_base/BUILD.gn
+++ b/rtc_base/BUILD.gn
@@ -218,6 +218,7 @@
       "logging.cc",
       "logging.h",
     ]
+    deps += [ "system:no_inline" ]
 
     # logging.h needs the deprecation header while downstream projects are
     # removing code that depends on logging implementation details.
diff --git a/rtc_base/logging.h b/rtc_base/logging.h
index 82960a4..6ff31f3 100644
--- a/rtc_base/logging.h
+++ b/rtc_base/logging.h
@@ -58,6 +58,7 @@
 #include "rtc_base/basictypes.h"
 #include "rtc_base/constructormagic.h"
 #include "rtc_base/deprecation.h"
+#include "rtc_base/system/no_inline.h"
 #include "rtc_base/thread_annotations.h"
 
 #if !defined(NDEBUG) || defined(DLOG_ALWAYS_ON)
@@ -142,6 +143,16 @@
   ~LogMessage();
 
   static bool Loggable(LoggingSeverity sev);
+
+  // Same as the above, but using a template argument instead of a function
+  // argument. (When the logging severity is statically known, passing it as a
+  // template argument instead of as a function argument saves space at the
+  // call site.)
+  template <LoggingSeverity S>
+  RTC_NO_INLINE static bool Loggable() {
+    return Loggable(S);
+  }
+
   std::ostream& stream();
 
   // Returns the time at which this function was called for the first time.
@@ -268,9 +279,11 @@
     ? (void) 0 \
     : rtc::LogMessageVoidify() &
 
-#define RTC_LOG(sev) \
-  RTC_LOG_SEVERITY_PRECONDITION(rtc::sev) \
-    rtc::LogMessage(__FILE__, __LINE__, rtc::sev).stream()
+#define RTC_LOG_SEVERITY_PRECONDITION_C(sev) \
+  !(rtc::LogMessage::Loggable<rtc::sev>()) ? (void)0 : rtc::LogMessageVoidify()&
+#define RTC_LOG(sev)                   \
+  RTC_LOG_SEVERITY_PRECONDITION_C(sev) \
+  rtc::LogMessage(__FILE__, __LINE__, rtc::sev).stream()
 
 // The _V version is for when a variable is passed in.  It doesn't do the
 // namespace concatenation.
@@ -297,11 +310,11 @@
   return (LogMessage::GetMinLogSeverity() <= sev);
 }
 
-#define RTC_LOG_E(sev, ctx, err, ...) \
-  RTC_LOG_SEVERITY_PRECONDITION(rtc::sev) \
-    rtc::LogMessage(__FILE__, __LINE__, rtc::sev, \
-                    rtc::ERRCTX_ ## ctx, err , ##__VA_ARGS__)   \
-        .stream()
+#define RTC_LOG_E(sev, ctx, err, ...)                                   \
+  RTC_LOG_SEVERITY_PRECONDITION_C(sev)                                  \
+  rtc::LogMessage(__FILE__, __LINE__, rtc::sev, rtc::ERRCTX_##ctx, err, \
+                  ##__VA_ARGS__)                                        \
+      .stream()
 
 #define RTC_LOG_T(sev) RTC_LOG(sev) << this << ": "
 
diff --git a/rtc_base/system/BUILD.gn b/rtc_base/system/BUILD.gn
index 03b57c2..0e42eba 100644
--- a/rtc_base/system/BUILD.gn
+++ b/rtc_base/system/BUILD.gn
@@ -42,3 +42,9 @@
     "ignore_warnings.h",
   ]
 }
+
+rtc_source_set("no_inline") {
+  sources = [
+    "no_inline.h",
+  ]
+}
diff --git a/rtc_base/system/no_inline.h b/rtc_base/system/no_inline.h
new file mode 100644
index 0000000..c309e85
--- /dev/null
+++ b/rtc_base/system/no_inline.h
@@ -0,0 +1,22 @@
+/*
+ *  Copyright (c) 2018 The WebRTC project authors. All Rights Reserved.
+ *
+ *  Use of this source code is governed by a BSD-style license
+ *  that can be found in the LICENSE file in the root of the source
+ *  tree. An additional intellectual property rights grant can be found
+ *  in the file PATENTS.  All contributing project authors may
+ *  be found in the AUTHORS file in the root of the source tree.
+ */
+
+#ifndef RTC_BASE_SYSTEM_NO_INLINE_H_
+#define RTC_BASE_SYSTEM_NO_INLINE_H_
+
+#if defined(_MSC_VER)
+#define RTC_NO_INLINE __declspec(noinline)
+#elif defined(__GNUC__)
+#define RTC_NO_INLINE __attribute__((__noinline__))
+#else
+#define RTC_NO_INLINE
+#endif
+
+#endif  // RTC_BASE_SYSTEM_NO_INLINE_H_