xref: /aosp_15_r20/external/webrtc/sdk/android/src/jni/pc/call_session_file_rotating_log_sink.cc (revision d9f758449e529ab9291ac668be2861e7a55c2422)
1 /*
2  *  Copyright 2017 The WebRTC project authors. All Rights Reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "rtc_base/log_sinks.h"
12 #include "sdk/android/generated_peerconnection_jni/CallSessionFileRotatingLogSink_jni.h"
13 #include "sdk/android/native_api/jni/java_types.h"
14 #include "sdk/android/src/jni/jni_helpers.h"
15 
16 namespace webrtc {
17 namespace jni {
18 
JNI_CallSessionFileRotatingLogSink_AddSink(JNIEnv * jni,const JavaParamRef<jstring> & j_dirPath,jint j_maxFileSize,jint j_severity)19 static jlong JNI_CallSessionFileRotatingLogSink_AddSink(
20     JNIEnv* jni,
21     const JavaParamRef<jstring>& j_dirPath,
22     jint j_maxFileSize,
23     jint j_severity) {
24   std::string dir_path = JavaToStdString(jni, j_dirPath);
25   rtc::CallSessionFileRotatingLogSink* sink =
26       new rtc::CallSessionFileRotatingLogSink(dir_path, j_maxFileSize);
27   if (!sink->Init()) {
28     RTC_LOG_V(rtc::LoggingSeverity::LS_WARNING)
29         << "Failed to init CallSessionFileRotatingLogSink for path "
30         << dir_path;
31     delete sink;
32     return 0;
33   }
34   rtc::LogMessage::AddLogToStream(
35       sink, static_cast<rtc::LoggingSeverity>(j_severity));
36   return jlongFromPointer(sink);
37 }
38 
JNI_CallSessionFileRotatingLogSink_DeleteSink(JNIEnv * jni,jlong j_sink)39 static void JNI_CallSessionFileRotatingLogSink_DeleteSink(
40     JNIEnv* jni,
41     jlong j_sink) {
42   rtc::CallSessionFileRotatingLogSink* sink =
43       reinterpret_cast<rtc::CallSessionFileRotatingLogSink*>(j_sink);
44   rtc::LogMessage::RemoveLogToStream(sink);
45   delete sink;
46 }
47 
48 static ScopedJavaLocalRef<jbyteArray>
JNI_CallSessionFileRotatingLogSink_GetLogData(JNIEnv * jni,const JavaParamRef<jstring> & j_dirPath)49 JNI_CallSessionFileRotatingLogSink_GetLogData(
50     JNIEnv* jni,
51     const JavaParamRef<jstring>& j_dirPath) {
52   std::string dir_path = JavaToStdString(jni, j_dirPath);
53   rtc::CallSessionFileRotatingStreamReader file_reader(dir_path);
54   size_t log_size = file_reader.GetSize();
55   if (log_size == 0) {
56     RTC_LOG_V(rtc::LoggingSeverity::LS_WARNING)
57         << "CallSessionFileRotatingStream returns 0 size for path " << dir_path;
58     return ScopedJavaLocalRef<jbyteArray>(jni, jni->NewByteArray(0));
59   }
60 
61   // TODO(nisse, sakal): To avoid copying, change api to use ByteBuffer.
62   std::unique_ptr<jbyte> buffer(static_cast<jbyte*>(malloc(log_size)));
63   size_t read = file_reader.ReadAll(buffer.get(), log_size);
64 
65   ScopedJavaLocalRef<jbyteArray> result =
66       ScopedJavaLocalRef<jbyteArray>(jni, jni->NewByteArray(read));
67   jni->SetByteArrayRegion(result.obj(), 0, read, buffer.get());
68 
69   return result;
70 }
71 
72 }  // namespace jni
73 }  // namespace webrtc
74