Update Log interface
This commit is contained in:
parent
7f0f820262
commit
0f15d05450
@ -14,6 +14,8 @@ import it.tdlight.jni.TdApi.ChatListArchive;
|
||||
import it.tdlight.jni.TdApi.ChatListMain;
|
||||
import it.tdlight.jni.TdApi.Function;
|
||||
import it.tdlight.jni.TdApi.User;
|
||||
import it.tdlight.tdnative.NativeClient;
|
||||
import it.tdlight.tdnative.NativeClient.LogMessageHandler;
|
||||
import java.io.IOException;
|
||||
import java.io.UncheckedIOException;
|
||||
import java.nio.file.Files;
|
||||
|
@ -20,7 +20,11 @@ package it.tdlight.common;
|
||||
import it.tdlight.client.SimpleTelegramClient;
|
||||
import it.tdlight.common.utils.CantLoadLibrary;
|
||||
import it.tdlight.common.utils.LoadLibrary;
|
||||
import it.tdlight.jni.TdApi;
|
||||
import it.tdlight.jni.TdApi.LogStreamEmpty;
|
||||
import it.tdlight.jni.TdApi.SetLogStream;
|
||||
import it.tdlight.jni.TdApi.SetLogVerbosityLevel;
|
||||
import it.tdlight.tdnative.NativeClient.LogMessageHandler;
|
||||
import java.util.concurrent.atomic.AtomicBoolean;
|
||||
import org.slf4j.Logger;
|
||||
import org.slf4j.LoggerFactory;
|
||||
@ -30,7 +34,7 @@ import org.slf4j.LoggerFactory;
|
||||
*/
|
||||
public final class Init {
|
||||
|
||||
public static final Logger LOG = LoggerFactory.getLogger(Init.class);
|
||||
public static final Logger LOG = LoggerFactory.getLogger("it.tdlight.TDLight");
|
||||
|
||||
private static volatile boolean started = false;
|
||||
|
||||
@ -46,7 +50,29 @@ public final class Init {
|
||||
LoadLibrary.load("tdjni");
|
||||
ConstructorDetector.init();
|
||||
try {
|
||||
NativeClientAccess.execute(new SetLogVerbosityLevel(1));
|
||||
NativeClientAccess.execute(new SetLogVerbosityLevel(2));
|
||||
Log.setLogMessageHandler(2, (verbosityLevel, message) -> {
|
||||
switch (verbosityLevel) {
|
||||
case -1:
|
||||
case 0:
|
||||
case 1:
|
||||
LOG.error(message);
|
||||
break;
|
||||
case 2:
|
||||
LOG.warn(message);
|
||||
break;
|
||||
case 3:
|
||||
LOG.info(message);
|
||||
break;
|
||||
case 4:
|
||||
LOG.debug(message);
|
||||
break;
|
||||
default:
|
||||
LOG.trace(message);
|
||||
break;
|
||||
}
|
||||
});
|
||||
NativeClientAccess.execute(new SetLogStream(new LogStreamEmpty()));
|
||||
} catch (Throwable ex) {
|
||||
LOG.error("Can't set verbosity level on startup", ex);
|
||||
}
|
||||
|
@ -4,7 +4,9 @@ import it.tdlight.jni.TdApi;
|
||||
import it.tdlight.jni.TdApi.LogStreamDefault;
|
||||
import it.tdlight.jni.TdApi.LogStreamFile;
|
||||
import it.tdlight.jni.TdApi.SetLogVerbosityLevel;
|
||||
import it.tdlight.tdnative.NativeLog;
|
||||
import it.tdlight.tdnative.NativeClient;
|
||||
import it.tdlight.tdnative.NativeClient.LogMessageHandler;
|
||||
import java.util.concurrent.atomic.AtomicInteger;
|
||||
import java.util.concurrent.atomic.AtomicLong;
|
||||
import java.util.concurrent.atomic.AtomicReference;
|
||||
import java.util.function.Consumer;
|
||||
@ -84,16 +86,19 @@ public final class Log {
|
||||
@Deprecated
|
||||
public static void setVerbosityLevel(int verbosityLevel) {
|
||||
NativeClientAccess.execute(new SetLogVerbosityLevel(verbosityLevel));
|
||||
updateLog();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 log message handler
|
||||
*
|
||||
* @param maxVerbosityLevel Log verbosity level with which the message was added from -1 up to 1024.
|
||||
* If 0, then TDLib will crash as soon as the callback returns.
|
||||
* None of the TDLib methods can be called from the callback.
|
||||
* @param logMessageHandler handler
|
||||
*/
|
||||
public static void setFatalErrorCallback(Consumer<String> fatalErrorCallback) {
|
||||
NativeLog.setFatalErrorCallback(fatalErrorCallback);
|
||||
public static void setLogMessageHandler(int maxVerbosityLevel, LogMessageHandler logMessageHandler) {
|
||||
NativeClientAccess.setLogMessageHandler(maxVerbosityLevel, logMessageHandler);
|
||||
}
|
||||
}
|
||||
|
@ -9,4 +9,8 @@ final class NativeClientAccess extends NativeClient {
|
||||
public static <R extends TdApi.Object> TdApi.Object execute(Function<R> function) {
|
||||
return nativeClientExecute(function);
|
||||
}
|
||||
|
||||
public static void setLogMessageHandler(int maxVerbosityLevel, LogMessageHandler logMessageHandler) {
|
||||
nativeClientSetLogMessageHandler(maxVerbosityLevel, logMessageHandler);
|
||||
}
|
||||
}
|
||||
|
@ -21,4 +21,8 @@ final class NativeClientAccess extends NativeClient {
|
||||
public static int receive(int[] clientIds, long[] eventIds, TdApi.Object[] events, double timeout) {
|
||||
return NativeClientAccess.nativeClientReceive(clientIds, eventIds, events, timeout);
|
||||
}
|
||||
|
||||
public static void setLogMessageHandler(int maxVerbosityLevel, LogMessageHandler logMessageHandler) {
|
||||
NativeClientAccess.nativeClientSetLogMessageHandler(maxVerbosityLevel, logMessageHandler);
|
||||
}
|
||||
}
|
||||
|
@ -15,4 +15,21 @@ public class NativeClient {
|
||||
double timeout);
|
||||
|
||||
protected static native TdApi.Object nativeClientExecute(TdApi.Function function);
|
||||
|
||||
protected static native void nativeClientSetLogMessageHandler(int maxVerbosityLevel, LogMessageHandler logMessageHandler);
|
||||
|
||||
/**
|
||||
* Interface for handler of messages that are added to the internal TDLib log.
|
||||
*/
|
||||
public interface LogMessageHandler {
|
||||
/**
|
||||
* Callback called on messages that are added to the internal TDLib log.
|
||||
*
|
||||
* @param verbosityLevel Log verbosity level with which the message was added from -1 up to 1024.
|
||||
* If 0, then TDLib will crash as soon as the callback returns.
|
||||
* None of the TDLib methods can be called from the callback.
|
||||
* @param message The message added to the internal TDLib log.
|
||||
*/
|
||||
void onLogMessage(int verbosityLevel, String message);
|
||||
}
|
||||
}
|
||||
|
@ -1,44 +0,0 @@
|
||||
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.
|
||||
*/
|
||||
public class NativeLog {
|
||||
|
||||
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.
|
||||
* The function does not return.
|
||||
*
|
||||
* @param errorMessage Error message.
|
||||
*/
|
||||
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.
|
||||
* 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 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);
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue
Block a user