Add JNI/java wrapper for the file rotating logging class.

BUG=
R=glaznev@webrtc.org

Review URL: https://codereview.webrtc.org/1309073004 .

Cr-Commit-Position: refs/heads/master@{#9840}
diff --git a/talk/app/webrtc/java/jni/peerconnection_jni.cc b/talk/app/webrtc/java/jni/peerconnection_jni.cc
index ed5545e..e9e4c07 100644
--- a/talk/app/webrtc/java/jni/peerconnection_jni.cc
+++ b/talk/app/webrtc/java/jni/peerconnection_jni.cc
@@ -73,6 +73,7 @@
 #include "webrtc/base/bind.h"
 #include "webrtc/base/checks.h"
 #include "webrtc/base/logging.h"
+#include "webrtc/base/logsinks.h"
 #include "webrtc/base/messagequeue.h"
 #include "webrtc/base/ssladapter.h"
 #include "webrtc/base/stringutils.h"
@@ -925,6 +926,20 @@
   }
 }
 
+JOW(void, Logging_nativeEnableLogThreads)(JNIEnv* jni, jclass) {
+  rtc::LogMessage::LogThreads(true);
+}
+
+JOW(void, Logging_nativeEnableLogTimeStamps)(JNIEnv* jni, jclass) {
+  rtc::LogMessage::LogTimestamps(true);
+}
+
+JOW(void, Logging_nativeLog)(
+    JNIEnv* jni, jclass, jint j_severity, jstring j_message) {
+  std::string message = JavaToStdString(jni, j_message);
+  LOG_V(static_cast<rtc::LoggingSeverity>(j_severity)) << message;
+}
+
 JOW(void, PeerConnection_freePeerConnection)(JNIEnv*, jclass, jlong j_p) {
   CHECK_RELEASE(reinterpret_cast<PeerConnectionInterface*>(j_p));
 }
@@ -1765,4 +1780,56 @@
       reinterpret_cast<VideoRendererInterface*>(j_renderer_pointer));
 }
 
+JOW(jlong, CallSessionFileRotatingLogSink_nativeAddSink)(
+    JNIEnv* jni, jclass,
+    jstring j_dirPath, jint j_maxFileSize, jint j_severity) {
+  std::string dir_path = JavaToStdString(jni, j_dirPath);
+  rtc::CallSessionFileRotatingLogSink* sink =
+      new rtc::CallSessionFileRotatingLogSink(dir_path, j_maxFileSize);
+  if (!sink->Init()) {
+    LOG_V(rtc::LoggingSeverity::LS_WARNING) <<
+        "Failed to init CallSessionFileRotatingLogSink for path " << dir_path;
+    delete sink;
+    return 0;
+  }
+  rtc::LogMessage::AddLogToStream(
+      sink, static_cast<rtc::LoggingSeverity>(j_severity));
+  return (jlong) sink;
+}
+
+JOW(void, CallSessionFileRotatingLogSink_nativeDeleteSink)(
+    JNIEnv* jni, jclass, jlong j_sink) {
+  rtc::CallSessionFileRotatingLogSink* sink =
+      reinterpret_cast<rtc::CallSessionFileRotatingLogSink*>(j_sink);
+  rtc::LogMessage::RemoveLogToStream(sink);
+  delete sink;
+}
+
+JOW(jbyteArray, CallSessionFileRotatingLogSink_nativeGetLogData)(
+    JNIEnv* jni, jclass, jstring j_dirPath) {
+  std::string dir_path = JavaToStdString(jni, j_dirPath);
+  rtc::scoped_ptr<rtc::CallSessionFileRotatingStream> stream(
+      new rtc::CallSessionFileRotatingStream(dir_path));
+  if (!stream->Open()) {
+    LOG_V(rtc::LoggingSeverity::LS_WARNING) <<
+        "Failed to open CallSessionFileRotatingStream for path " << dir_path;
+    return jni->NewByteArray(0);
+  }
+  size_t log_size = 0;
+  if (!stream->GetSize(&log_size) || log_size == 0) {
+    LOG_V(rtc::LoggingSeverity::LS_WARNING) <<
+        "CallSessionFileRotatingStream returns 0 size for path " << dir_path;
+    return jni->NewByteArray(0);
+  }
+
+  size_t read = 0;
+  rtc::scoped_ptr<jbyte> buffer(static_cast<jbyte*>(malloc(log_size)));
+  stream->ReadAll(buffer.get(), log_size, &read, nullptr);
+
+  jbyteArray result = jni->NewByteArray(read);
+  jni->SetByteArrayRegion(result, 0, read, buffer.get());
+
+  return result;
+}
+
 }  // namespace webrtc_jni
diff --git a/talk/app/webrtc/java/src/org/webrtc/CallSessionFileRotatingLogSink.java b/talk/app/webrtc/java/src/org/webrtc/CallSessionFileRotatingLogSink.java
new file mode 100644
index 0000000..f7032a7
--- /dev/null
+++ b/talk/app/webrtc/java/src/org/webrtc/CallSessionFileRotatingLogSink.java
@@ -0,0 +1,57 @@
+/*
+ * libjingle
+ * Copyright 2015 Google Inc.
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ *  1. Redistributions of source code must retain the above copyright notice,
+ *     this list of conditions and the following disclaimer.
+ *  2. Redistributions in binary form must reproduce the above copyright notice,
+ *     this list of conditions and the following disclaimer in the documentation
+ *     and/or other materials provided with the distribution.
+ *  3. The name of the author may not be used to endorse or promote products
+ *     derived from this software without specific prior written permission.
+ *
+ * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
+ * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
+ * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
+ * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
+ * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
+ * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
+ * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ */
+
+package org.webrtc;
+
+public class CallSessionFileRotatingLogSink {
+  static {
+    System.loadLibrary("jingle_peerconnection_so");
+  }
+
+  private long nativeSink;
+
+  public static byte[] getLogData(String dirPath) {
+    return nativeGetLogData(dirPath);
+  }
+
+  public CallSessionFileRotatingLogSink(
+      String dirPath, int maxFileSize, Logging.Severity severity) {
+    nativeSink = nativeAddSink(dirPath, maxFileSize, severity.ordinal());
+  }
+
+  public void dispose() {
+    if (nativeSink != 0) {
+      nativeDeleteSink(nativeSink);
+      nativeSink = 0;
+    }
+  }
+
+  private static native long nativeAddSink(
+      String dirPath, int maxFileSize, int severity);
+  private static native void nativeDeleteSink(long nativeSink);
+  private static native byte[] nativeGetLogData(String dirPath);
+}
diff --git a/talk/app/webrtc/java/src/org/webrtc/Logging.java b/talk/app/webrtc/java/src/org/webrtc/Logging.java
index fd49bce..75f7589 100644
--- a/talk/app/webrtc/java/src/org/webrtc/Logging.java
+++ b/talk/app/webrtc/java/src/org/webrtc/Logging.java
@@ -64,6 +64,13 @@
     LS_SENSITIVE, LS_VERBOSE, LS_INFO, LS_WARNING, LS_ERROR,
   };
 
+  public static void enableLogThreads() {
+    nativeEnableLogThreads();
+  }
+
+  public static void enableLogTimeStamps() {
+    nativeEnableLogTimeStamps();
+  }
 
   // Enable tracing to |path| of messages of |levels| and |severity|.
   // On Android, use "logcat:" for |path| to send output there.
@@ -76,6 +83,13 @@
     nativeEnableTracing(path, nativeLevel, severity.ordinal());
   }
 
+  public static void log(Severity severity, String tag, String message) {
+    nativeLog(severity.ordinal(), tag + ": " + message);
+  }
+
   private static native void nativeEnableTracing(
       String path, int nativeLevels, int nativeSeverity);
+  private static native void nativeEnableLogThreads();
+  private static native void nativeEnableLogTimeStamps();
+  private static native void nativeLog(int severity, String message);
 }
diff --git a/talk/libjingle.gyp b/talk/libjingle.gyp
index 339fa54..34e9034 100755
--- a/talk/libjingle.gyp
+++ b/talk/libjingle.gyp
@@ -118,6 +118,7 @@
                 'peerconnection_java_files': [
                   'app/webrtc/java/src/org/webrtc/AudioSource.java',
                   'app/webrtc/java/src/org/webrtc/AudioTrack.java',
+                  'app/webrtc/java/src/org/webrtc/CallSessionFileRotatingLogSink.java',
                   'app/webrtc/java/src/org/webrtc/DataChannel.java',
                   'app/webrtc/java/src/org/webrtc/IceCandidate.java',
                   'app/webrtc/java/src/org/webrtc/Logging.java',