Add back loading native library in Logging.java.

Some applications enable logging before using PeerConnectionFactory.
Not loading native library in Logging.java broke these applications.

TBR=magjed@webrtc.org

Bug: webrtc:7474, b/67419105
Change-Id: I5984d2241cfb76e0edb5b5da0974c8693bf50603
Reviewed-on: https://webrtc-review.googlesource.com/6600
Reviewed-by: Sami Kalliomäki <sakal@webrtc.org>
Commit-Queue: Sami Kalliomäki <sakal@webrtc.org>
Cr-Commit-Position: refs/heads/master@{#20151}
diff --git a/rtc_base/java/src/org/webrtc/Logging.java b/rtc_base/java/src/org/webrtc/Logging.java
index d1cc2f0..347d495 100644
--- a/rtc_base/java/src/org/webrtc/Logging.java
+++ b/rtc_base/java/src/org/webrtc/Logging.java
@@ -30,6 +30,8 @@
 public class Logging {
   private static final Logger fallbackLogger = createFallbackLogger();
   private static volatile boolean loggingEnabled;
+  private static enum NativeLibStatus { UNINITIALIZED, LOADED, FAILED }
+  private static volatile NativeLibStatus nativeLibStatus = NativeLibStatus.UNINITIALIZED;
 
   private static Logger createFallbackLogger() {
     final Logger fallbackLogger = Logger.getLogger("org.webrtc.Logging");
@@ -37,6 +39,19 @@
     return fallbackLogger;
   }
 
+  private static boolean loadNativeLibrary() {
+    if (nativeLibStatus == NativeLibStatus.UNINITIALIZED) {
+      try {
+        System.loadLibrary("jingle_peerconnection_so");
+        nativeLibStatus = NativeLibStatus.LOADED;
+      } catch (UnsatisfiedLinkError t) {
+        nativeLibStatus = NativeLibStatus.FAILED;
+        fallbackLogger.log(Level.WARNING, "Failed to load jingle_peerconnection_so: ", t);
+      }
+    }
+    return nativeLibStatus == NativeLibStatus.LOADED;
+  }
+
   // TODO(solenberg): Remove once dependent projects updated.
   @Deprecated
   public enum TraceLevel {
@@ -66,22 +81,34 @@
   public enum Severity { LS_SENSITIVE, LS_VERBOSE, LS_INFO, LS_WARNING, LS_ERROR, LS_NONE }
 
   public static void enableLogThreads() {
+    if (!loadNativeLibrary()) {
+      fallbackLogger.log(Level.WARNING, "Cannot enable log thread because native lib not loaded.");
+      return;
+    }
     nativeEnableLogThreads();
   }
 
   public static void enableLogTimeStamps() {
+    if (!loadNativeLibrary()) {
+      fallbackLogger.log(
+          Level.WARNING, "Cannot enable log timestamps because native lib not loaded.");
+      return;
+    }
     nativeEnableLogTimeStamps();
   }
 
   // TODO(solenberg): Remove once dependent projects updated.
   @Deprecated
-  public static void enableTracing(String path, EnumSet<TraceLevel> levels) {
-  }
+  public static void enableTracing(String path, EnumSet<TraceLevel> levels) {}
 
   // Enable diagnostic logging for messages of |severity| to the platform debug
   // output. On Android, the output will be directed to Logcat.
   // Note: this function starts collecting the output of the LOG() macros.
   public static synchronized void enableLogToDebugOutput(Severity severity) {
+    if (!loadNativeLibrary()) {
+      fallbackLogger.log(Level.WARNING, "Cannot enable logging because native lib not loaded.");
+      return;
+    }
     nativeEnableLogToDebugOutput(severity.ordinal());
     loggingEnabled = true;
   }