Cleanup native code

This commit is contained in:
Andrea Cavalli 2021-10-22 10:11:49 +02:00
parent 517e0e4378
commit 327db25c6c
5 changed files with 34 additions and 156 deletions

View File

@ -1,4 +1,3 @@
../src/main/jni-java-src/it/tdlight/jni/TdApi.java
../src/main/jni-java-src/it/tdlight/tdnative/NativeClient.java
../src/main/jni-java-src/it/tdlight/tdnative/NativeLog.java
../src/main/jni-java-src/it/tdlight/tdnative/ObjectsUtils.java

View File

@ -51,10 +51,10 @@ static jint Client_nativeClientReceive(JNIEnv *env, jclass clazz, jintArray clie
auto *manager = get_manager();
auto response = manager->receive(timeout);
while (response.object) {
jint client_id = static_cast<jint>(response.client_id);
auto client_id = static_cast<jint>(response.client_id);
env->SetIntArrayRegion(client_ids, result_size, 1, &client_id);
jlong request_id = static_cast<jlong>(response.request_id);
auto request_id = static_cast<jlong>(response.request_id);
env->SetLongArrayRegion(ids, result_size, 1, &request_id);
jobject object;
@ -78,18 +78,6 @@ static jobject Client_nativeClientExecute(JNIEnv *env, jclass clazz, jobject fun
return result;
}
static void Log_setVerbosityLevel(JNIEnv *env, jclass clazz, jint new_log_verbosity_level) {
td::Log::set_verbosity_level(static_cast<int>(new_log_verbosity_level));
}
static jboolean Log_setFilePath(JNIEnv *env, jclass clazz, jstring file_path) {
return td::Log::set_file_path(td::jni::from_jstring(env, file_path)) ? JNI_TRUE : JNI_FALSE;
}
static void Log_setMaxFileSize(JNIEnv *env, jclass clazz, jlong max_file_size) {
td::Log::set_max_file_size(max_file_size);
}
static jstring Object_toString(JNIEnv *env, jobject object) {
return td::jni::to_jstring(env, to_string(td::td_api::Object::fetch(env, object)));
}
@ -107,8 +95,11 @@ static void on_log_message(int verbosity_level, const char *error_message) {
return;
}
auto env = td::jni::get_jni_env(java_vm, JAVA_VERSION);
if (env == nullptr) {
return;
}
jmethodID on_fatal_error_method = env->GetStaticMethodID(log_class, "onFatalError", "(Ljava/lang/String;)V");
if (env && on_fatal_error_method) {
if (on_fatal_error_method) {
jstring error_str = td::jni::to_jstring(env.get(), error_message);
env->CallStaticVoidMethod(log_class, on_fatal_error_method, error_str);
if (error_str) {
@ -142,10 +133,6 @@ static jint register_native(JavaVM *vm) {
register_method(client_class, "nativeClientReceive", "([I[J[" TD_OBJECT "D)I", Client_nativeClientReceive);
register_method(client_class, "nativeClientExecute", "(" TD_FUNCTION ")" TD_OBJECT, Client_nativeClientExecute);
register_method(log_class, "setVerbosityLevel", "(I)V", Log_setVerbosityLevel);
register_method(log_class, "setFilePath", "(Ljava/lang/String;)Z", Log_setFilePath);
register_method(log_class, "setMaxFileSize", "(J)V", Log_setMaxFileSize);
register_method(object_class, "toString", "()Ljava/lang/String;", Object_toString);
register_method(function_class, "toString", "()Ljava/lang/String;", Function_toString);

View File

@ -1,6 +1,5 @@
//
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2018
// Copyright Aliaksei Levin (levlam@telegram.org), Arseny Smirnov (arseny30@gmail.com) 2014-2021
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@ -51,10 +50,10 @@ static jint Client_nativeClientReceive(JNIEnv *env, jclass clazz, jintArray clie
auto *manager = get_manager();
auto response = manager->receive(timeout);
while (response.object) {
jint client_id = static_cast<jint>(response.client_id);
auto client_id = static_cast<jint>(response.client_id);
env->SetIntArrayRegion(client_ids, result_size, 1, &client_id);
jlong request_id = static_cast<jlong>(response.request_id);
auto request_id = static_cast<jlong>(response.request_id);
env->SetLongArrayRegion(ids, result_size, 1, &request_id);
jobject object;
@ -78,18 +77,6 @@ static jobject Client_nativeClientExecute(JNIEnv *env, jclass clazz, jobject fun
return result;
}
static void Log_setVerbosityLevel(JNIEnv *env, jclass clazz, jint new_log_verbosity_level) {
td::Log::set_verbosity_level(static_cast<int>(new_log_verbosity_level));
}
static jboolean Log_setFilePath(JNIEnv *env, jclass clazz, jstring file_path) {
return td::Log::set_file_path(td::jni::from_jstring(env, file_path)) ? JNI_TRUE : JNI_FALSE;
}
static void Log_setMaxFileSize(JNIEnv *env, jclass clazz, jlong max_file_size) {
td::Log::set_max_file_size(max_file_size);
}
static jstring Object_toString(JNIEnv *env, jobject object) {
return td::jni::to_jstring(env, to_string(td::td_api::Object::fetch(env, object)));
}
@ -107,8 +94,11 @@ static void on_log_message(int verbosity_level, const char *error_message) {
return;
}
auto env = td::jni::get_jni_env(java_vm, JAVA_VERSION);
if (env == nullptr) {
return;
}
jmethodID on_fatal_error_method = env->GetStaticMethodID(log_class, "onFatalError", "(Ljava/lang/String;)V");
if (env && on_fatal_error_method) {
if (on_fatal_error_method) {
jstring error_str = td::jni::to_jstring(env.get(), error_message);
env->CallStaticVoidMethod(log_class, on_fatal_error_method, error_str);
if (error_str) {
@ -142,10 +132,6 @@ static jint register_native(JavaVM *vm) {
register_method(client_class, "nativeClientReceive", "([I[J[" TD_OBJECT "D)I", Client_nativeClientReceive);
register_method(client_class, "nativeClientExecute", "(" TD_FUNCTION ")" TD_OBJECT, Client_nativeClientExecute);
register_method(log_class, "setVerbosityLevel", "(I)V", Log_setVerbosityLevel);
register_method(log_class, "setFilePath", "(Ljava/lang/String;)Z", Log_setFilePath);
register_method(log_class, "setMaxFileSize", "(J)V", Log_setMaxFileSize);
register_method(object_class, "toString", "()Ljava/lang/String;", Object_toString);
register_method(function_class, "toString", "()Ljava/lang/String;", Function_toString);

View File

@ -2,54 +2,16 @@ package it.tdlight.tdnative;
import it.tdlight.jni.TdApi;
import java.util.function.Consumer;
import java.util.concurrent.atomic.AtomicReference;
/**
* Class used for managing internal TDLib logging.
* Use TdApi.*Log* methods instead.
*/
public class NativeLog {
private static final Consumer<String> defaultFatalErrorCallbackPtr = System.err::println;
private static Consumer<String> fatalErrorCallback = defaultFatalErrorCallbackPtr;
/**
* Sets file path for writing TDLib internal log. By default TDLib writes logs to the System.err.
* Use this method to write the log to a file instead.
*
* @deprecated As of TDLib 1.4.0 in favor of {@link TdApi.SetLogStream}, to be removed in the future.
* @param filePath Path to a file for writing TDLib internal log. Use an empty path to
* switch back to logging to the System.err.
* @return whether opening the log file succeeded.
*/
@Deprecated
public static native boolean setFilePath(String filePath);
/**
* Changes the maximum size of TDLib log file.
*
* @deprecated As of TDLib 1.4.0 in favor of {@link TdApi.SetLogStream}, to be removed in the future.
* @param maxFileSize The maximum size of the file to where the internal TDLib log is written
* before the file will be auto-rotated. Must be positive. Defaults to 10 MB.
*/
@Deprecated
public static native void setMaxFileSize(long maxFileSize);
/**
* Changes TDLib log verbosity.
*
* @deprecated As of TDLib 1.4.0 in favor of {@link TdApi.SetLogVerbosityLevel}, to be removed in the future.
* @param verbosityLevel New value of log verbosity level. Must be non-negative.
* Value 0 corresponds to fatal errors,
* value 1 corresponds to java.util.logging.Level.SEVERE,
* value 2 corresponds to java.util.logging.Level.WARNING,
* value 3 corresponds to java.util.logging.Level.INFO,
* value 4 corresponds to java.util.logging.Level.FINE,
* value 5 corresponds to java.util.logging.Level.FINER,
* value greater than 5 can be used to enable even more logging.
* Default value of the log verbosity level is 5.
*/
@Deprecated
public static native void setVerbosityLevel(int verbosityLevel);
private static final Consumer<String> defaultFatalErrorCallback = NativeLog::printFatalError;
private static final AtomicReference<Consumer<String>> fatalErrorCallback
= new AtomicReference<>(defaultFatalErrorCallback);
/**
* This function is called from the JNI when a fatal error happens to provide a better error message.
@ -57,15 +19,26 @@ public class NativeLog {
*
* @param errorMessage Error message.
*/
private static synchronized void onFatalError(String errorMessage) {
new Thread(() -> NativeLog.fatalErrorCallback.accept(errorMessage)).start();
private static void onFatalError(String errorMessage) {
new Thread(() -> NativeLog.fatalErrorCallback.get().accept(errorMessage)).start();
}
/**
* Sets the callback that will be called when a fatal error happens. None of the TDLib methods can be called from the callback. The TDLib will crash as soon as callback returns. By default the callback set to print in stderr.
* @param fatalErrorCallback Callback that will be called when a fatal error happens. Pass null to restore default callback.
* Sets the callback that will be called when a fatal error happens.
* None of the TDLib methods can be called from the callback.
* TDLib will crash as soon as the callback returns.
* By default the callback set to print in stderr.
* @param fatalErrorCallback Callback that will be called when a fatal error happens.
* Pass null to restore default callback.
*/
public static synchronized void setFatalErrorCallback(Consumer<String> fatalErrorCallback) {
NativeLog.fatalErrorCallback = ObjectsUtils.requireNonNullElse(fatalErrorCallback, defaultFatalErrorCallbackPtr);
public static void setFatalErrorCallback(Consumer<String> fatalErrorCallback) {
if (fatalErrorCallback == null) {
fatalErrorCallback = defaultFatalErrorCallback;
}
NativeLog.fatalErrorCallback.set(fatalErrorCallback);
}
private static void printFatalError(String errorMessage) {
System.err.println("TDLib fatal error: " + errorMessage);
}
}

View File

@ -1,67 +0,0 @@
package it.tdlight.tdnative;
public class ObjectsUtils {
/**
* Returns the first argument if it is non-{@code null} and
* otherwise returns the non-{@code null} second argument.
*
* @param obj an object
* @param defaultObj a non-{@code null} object to return if the first argument
* is {@code null}
* @param <T> the type of the reference
* @return the first argument if it is non-{@code null} and
* otherwise the second argument if it is non-{@code null}
* @throws NullPointerException if both {@code obj} is null and
* {@code defaultObj} is {@code null}
* @since 9
*/
public static <T> T requireNonNullElse(T obj, T defaultObj) {
return (obj != null) ? obj : requireNonNull(defaultObj, "defaultObj");
}
/**
* Checks that the specified object reference is not {@code null}. This
* method is designed primarily for doing parameter validation in methods
* and constructors, as demonstrated below:
* <blockquote><pre>
* public Foo(Bar bar) {
* this.bar = Objects.requireNonNull(bar);
* }
* </pre></blockquote>
*
* @param obj the object reference to check for nullity
* @param <T> the type of the reference
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public static <T> T requireNonNull(T obj) {
if (obj == null)
throw new NullPointerException();
return obj;
}
/**
* Checks that the specified object reference is not {@code null} and
* throws a customized {@link NullPointerException} if it is. This method
* is designed primarily for doing parameter validation in methods and
* constructors with multiple parameters, as demonstrated below:
* <blockquote><pre>
* public Foo(Bar bar, Baz baz) {
* this.bar = Objects.requireNonNull(bar, "bar must not be null");
* this.baz = Objects.requireNonNull(baz, "baz must not be null");
* }
* </pre></blockquote>
*
* @param obj the object reference to check for nullity
* @param message detail message to be used in the event that a {@code
* NullPointerException} is thrown
* @param <T> the type of the reference
* @return {@code obj} if not {@code null}
* @throws NullPointerException if {@code obj} is {@code null}
*/
public static <T> T requireNonNull(T obj, String message) {
if (obj == null)
throw new NullPointerException(message);
return obj;
}
}