Reimplement legacy logging
This commit is contained in:
parent
6ada063456
commit
c0a275ae87
@ -26,24 +26,24 @@
|
|||||||
<dependency>
|
<dependency>
|
||||||
<groupId>it.tdlight</groupId>
|
<groupId>it.tdlight</groupId>
|
||||||
<artifactId>tdlight-java</artifactId>
|
<artifactId>tdlight-java</artifactId>
|
||||||
<version>2.7.8.28</version>
|
<version>2.7.8.29</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- TDLight natives -->
|
<!-- TDLight natives -->
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>it.tdlight</groupId>
|
<groupId>it.tdlight</groupId>
|
||||||
<artifactId>tdlight-natives-linux-amd64</artifactId>
|
<artifactId>tdlight-natives-linux-amd64</artifactId>
|
||||||
<version>4.0.168</version>
|
<version>4.0.169</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>it.tdlight</groupId>
|
<groupId>it.tdlight</groupId>
|
||||||
<artifactId>tdlight-natives-windows-amd64</artifactId>
|
<artifactId>tdlight-natives-windows-amd64</artifactId>
|
||||||
<version>4.0.168</version>
|
<version>4.0.169</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
<dependency>
|
<dependency>
|
||||||
<groupId>it.tdlight</groupId>
|
<groupId>it.tdlight</groupId>
|
||||||
<artifactId>tdlight-natives-osx-amd64</artifactId>
|
<artifactId>tdlight-natives-osx-amd64</artifactId>
|
||||||
<version>4.0.168</version>
|
<version>4.0.169</version>
|
||||||
</dependency>
|
</dependency>
|
||||||
|
|
||||||
<!-- log4j logging -->
|
<!-- log4j logging -->
|
||||||
|
@ -34,12 +34,6 @@ public final class SimpleTelegramClient implements Authenticable {
|
|||||||
} catch (CantLoadLibrary e) {
|
} catch (CantLoadLibrary e) {
|
||||||
throw new RuntimeException("Can't load native libraries", e);
|
throw new RuntimeException("Can't load native libraries", e);
|
||||||
}
|
}
|
||||||
try {
|
|
||||||
//noinspection deprecation
|
|
||||||
Log.setVerbosityLevel(1);
|
|
||||||
} catch (Throwable ex) {
|
|
||||||
LOG.warn("Can't set verbosity level", ex);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
private final TelegramClient client;
|
private final TelegramClient client;
|
||||||
|
@ -17,14 +17,21 @@
|
|||||||
|
|
||||||
package it.tdlight.common;
|
package it.tdlight.common;
|
||||||
|
|
||||||
|
import it.tdlight.client.SimpleTelegramClient;
|
||||||
import it.tdlight.common.utils.CantLoadLibrary;
|
import it.tdlight.common.utils.CantLoadLibrary;
|
||||||
import it.tdlight.common.utils.LoadLibrary;
|
import it.tdlight.common.utils.LoadLibrary;
|
||||||
|
import it.tdlight.jni.TdApi.SetLogVerbosityLevel;
|
||||||
|
import java.util.concurrent.atomic.AtomicBoolean;
|
||||||
|
import org.slf4j.Logger;
|
||||||
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Init class to successfully initialize Tdlib
|
* Init class to successfully initialize Tdlib
|
||||||
*/
|
*/
|
||||||
public final class Init {
|
public final class Init {
|
||||||
|
|
||||||
|
public static final Logger LOG = LoggerFactory.getLogger(Init.class);
|
||||||
|
|
||||||
private static boolean started = false;
|
private static boolean started = false;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -36,6 +43,11 @@ public final class Init {
|
|||||||
if (!started) {
|
if (!started) {
|
||||||
LoadLibrary.load("tdjni");
|
LoadLibrary.load("tdjni");
|
||||||
ConstructorDetector.init();
|
ConstructorDetector.init();
|
||||||
|
try {
|
||||||
|
NativeClientAccess.execute(new SetLogVerbosityLevel(2));
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
LOG.error("Can't set verbosity level on startup", ex);
|
||||||
|
}
|
||||||
started = true;
|
started = true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,7 +1,12 @@
|
|||||||
package it.tdlight.common;
|
package it.tdlight.common;
|
||||||
|
|
||||||
import it.tdlight.jni.TdApi;
|
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.NativeLog;
|
||||||
|
import java.util.concurrent.atomic.AtomicLong;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -18,6 +23,24 @@ public final class Log {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private static final AtomicReference<String> LOG_FILE_PATH = new AtomicReference<>(null);
|
||||||
|
private static final AtomicLong LOG_MAX_FILE_SIZE = new AtomicLong(10 * 1024 * 1024);
|
||||||
|
|
||||||
|
private static boolean updateLog() {
|
||||||
|
try {
|
||||||
|
String path = LOG_FILE_PATH.get();
|
||||||
|
long maxSize = LOG_MAX_FILE_SIZE.get();
|
||||||
|
if (path == null) {
|
||||||
|
NativeClientAccess.execute(new TdApi.SetLogStream(new LogStreamDefault()));
|
||||||
|
} else {
|
||||||
|
NativeClientAccess.execute(new TdApi.SetLogStream(new LogStreamFile(path, maxSize, false)));
|
||||||
|
}
|
||||||
|
} catch (Throwable ex) {
|
||||||
|
ex.printStackTrace();
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets file path for writing TDLib internal log. By default TDLib writes logs to the System.err. Use this method to
|
* Sets file path for writing TDLib internal log. By default TDLib writes logs to the System.err. Use this method to
|
||||||
@ -29,8 +52,9 @@ public final class Log {
|
|||||||
* @deprecated As of TDLib 1.4.0 in favor of {@link TdApi.SetLogStream}, to be removed in the future.
|
* @deprecated As of TDLib 1.4.0 in favor of {@link TdApi.SetLogStream}, to be removed in the future.
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public static synchronized boolean setFilePath(String filePath) {
|
public static boolean setFilePath(String filePath) {
|
||||||
return NativeLog.setFilePath(filePath);
|
LOG_FILE_PATH.set(filePath);
|
||||||
|
return updateLog();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -41,8 +65,9 @@ public final class Log {
|
|||||||
* @deprecated As of TDLib 1.4.0 in favor of {@link TdApi.SetLogStream}, to be removed in the future.
|
* @deprecated As of TDLib 1.4.0 in favor of {@link TdApi.SetLogStream}, to be removed in the future.
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public static synchronized void setMaxFileSize(long maxFileSize) {
|
public static void setMaxFileSize(long maxFileSize) {
|
||||||
NativeLog.setMaxFileSize(maxFileSize);
|
LOG_MAX_FILE_SIZE.set(maxFileSize);
|
||||||
|
updateLog();
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -57,8 +82,8 @@ public final class Log {
|
|||||||
* @deprecated As of TDLib 1.4.0 in favor of {@link TdApi.SetLogVerbosityLevel}, to be removed in the future.
|
* @deprecated As of TDLib 1.4.0 in favor of {@link TdApi.SetLogVerbosityLevel}, to be removed in the future.
|
||||||
*/
|
*/
|
||||||
@Deprecated
|
@Deprecated
|
||||||
public static synchronized void setVerbosityLevel(int verbosityLevel) {
|
public static void setVerbosityLevel(int verbosityLevel) {
|
||||||
NativeLog.setVerbosityLevel(verbosityLevel);
|
NativeClientAccess.execute(new SetLogVerbosityLevel(verbosityLevel));
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -68,7 +93,7 @@ public final class Log {
|
|||||||
* @param fatalErrorCallback Callback that will be called when a fatal error happens. Pass null to restore default
|
* @param fatalErrorCallback Callback that will be called when a fatal error happens. Pass null to restore default
|
||||||
* callback.
|
* callback.
|
||||||
*/
|
*/
|
||||||
public static synchronized void setFatalErrorCallback(Consumer<String> fatalErrorCallback) {
|
public static void setFatalErrorCallback(Consumer<String> fatalErrorCallback) {
|
||||||
NativeLog.setFatalErrorCallback(fatalErrorCallback);
|
NativeLog.setFatalErrorCallback(fatalErrorCallback);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
12
src/main/java/it/tdlight/common/NativeClientAccess.java
Normal file
12
src/main/java/it/tdlight/common/NativeClientAccess.java
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
package it.tdlight.common;
|
||||||
|
|
||||||
|
import it.tdlight.jni.TdApi;
|
||||||
|
import it.tdlight.jni.TdApi.Function;
|
||||||
|
import it.tdlight.tdnative.NativeClient;
|
||||||
|
|
||||||
|
final class NativeClientAccess extends NativeClient {
|
||||||
|
|
||||||
|
public static <R extends TdApi.Object> TdApi.Object execute(Function<R> function) {
|
||||||
|
return nativeClientExecute(function);
|
||||||
|
}
|
||||||
|
}
|
@ -2,18 +2,17 @@ package it.tdlight.tdnative;
|
|||||||
|
|
||||||
import it.tdlight.jni.TdApi;
|
import it.tdlight.jni.TdApi;
|
||||||
|
|
||||||
|
@SuppressWarnings("rawtypes")
|
||||||
public class NativeClient {
|
public class NativeClient {
|
||||||
|
|
||||||
protected static native int createNativeClient();
|
protected static native int createNativeClient();
|
||||||
|
|
||||||
protected static native <R extends TdApi.Object> void nativeClientSend(int nativeClientId,
|
protected static native void nativeClientSend(int nativeClientId, long eventId, TdApi.Function function);
|
||||||
long eventId,
|
|
||||||
TdApi.Function<R> function);
|
|
||||||
|
|
||||||
protected static native int nativeClientReceive(int[] clientIds,
|
protected static native int nativeClientReceive(int[] clientIds,
|
||||||
long[] eventIds,
|
long[] eventIds,
|
||||||
TdApi.Object[] events,
|
TdApi.Object[] events,
|
||||||
double timeout);
|
double timeout);
|
||||||
|
|
||||||
protected static native <R extends TdApi.Object> TdApi.Object nativeClientExecute(TdApi.Function<R> function);
|
protected static native TdApi.Object nativeClientExecute(TdApi.Function function);
|
||||||
}
|
}
|
||||||
|
@ -2,73 +2,43 @@ package it.tdlight.tdnative;
|
|||||||
|
|
||||||
import it.tdlight.jni.TdApi;
|
import it.tdlight.jni.TdApi;
|
||||||
import java.util.function.Consumer;
|
import java.util.function.Consumer;
|
||||||
|
import java.util.concurrent.atomic.AtomicReference;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Class used for managing internal TDLib logging. Use TdApi.*Log* methods instead.
|
* Class used for managing internal TDLib logging.
|
||||||
*/
|
*/
|
||||||
public class NativeLog {
|
public class NativeLog {
|
||||||
|
|
||||||
private static final Consumer<String> defaultFatalErrorCallbackPtr = System.err::println;
|
private static final Consumer<String> defaultFatalErrorCallback = NativeLog::printFatalError;
|
||||||
private static Consumer<String> fatalErrorCallback = defaultFatalErrorCallbackPtr;
|
private static final AtomicReference<Consumer<String>> fatalErrorCallback
|
||||||
|
= new AtomicReference<>(defaultFatalErrorCallback);
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Sets file path for writing TDLib internal log. By default TDLib writes logs to the System.err. Use this method to
|
* This function is called from the JNI when a fatal error happens to provide a better error message.
|
||||||
* write the log to a file instead.
|
* The function does not return.
|
||||||
*
|
|
||||||
* @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 As of TDLib 1.4.0 in favor of {@link TdApi.SetLogStream}, to be removed in the future.
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public static native boolean setFilePath(String filePath);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Changes the maximum size of TDLib log file.
|
|
||||||
*
|
|
||||||
* @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 As of TDLib 1.4.0 in favor of {@link TdApi.SetLogStream}, to be removed in the future.
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public static native void setMaxFileSize(long maxFileSize);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Changes TDLib log verbosity.
|
|
||||||
*
|
|
||||||
* @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 As of TDLib 1.4.0 in favor of {@link TdApi.SetLogVerbosityLevel}, to be removed in the future.
|
|
||||||
*/
|
|
||||||
@Deprecated
|
|
||||||
public static native void setVerbosityLevel(int verbosityLevel);
|
|
||||||
|
|
||||||
/**
|
|
||||||
* 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.
|
* @param errorMessage Error message.
|
||||||
*/
|
*/
|
||||||
private static synchronized void onFatalError(String errorMessage) {
|
private static void onFatalError(String errorMessage) {
|
||||||
new Thread(() -> NativeLog.fatalErrorCallback.accept(errorMessage)).start();
|
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
|
* Sets the callback that will be called when a fatal error happens.
|
||||||
* callback. The TDLib will crash as soon as callback returns. By default the callback set to print in stderr.
|
* None of the TDLib methods can be called from the callback.
|
||||||
*
|
* TDLib will crash as soon as the callback returns.
|
||||||
* @param fatalErrorCallback Callback that will be called when a fatal error happens. Pass null to restore default
|
* By default the callback set to print in stderr.
|
||||||
* callback.
|
* @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) {
|
public static void setFatalErrorCallback(Consumer<String> fatalErrorCallback) {
|
||||||
if (fatalErrorCallback == null) {
|
if (fatalErrorCallback == null) {
|
||||||
NativeLog.fatalErrorCallback = defaultFatalErrorCallbackPtr;
|
fatalErrorCallback = defaultFatalErrorCallback;
|
||||||
} else {
|
|
||||||
NativeLog.fatalErrorCallback = fatalErrorCallback;
|
|
||||||
}
|
}
|
||||||
|
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