Alternative library loading

This commit is contained in:
Andrea Cavalli 2020-08-21 00:01:01 +02:00
parent 4d3b9ad4e1
commit 09ec6c98e4
3 changed files with 53 additions and 18 deletions

View File

@ -17,27 +17,11 @@
package it.tdlight.tdlib;
/**
* Interface for managing the internal logging of TDLib. By default TDLib writes logs to stderr or an OS specific log and uses a verbosity level of 5.
*/
public class NativeLog {
/**
* Sets the path to the file to where the internal TDLib log will be written. By default TDLib writes logs to stderr or an OS specific log. Use this method to write the log to a file instead.
* @param filePath Path to a file where the internal TDLib log will be written. Use an empty path to switch back to the default logging behaviour.
* @return True on success, or false otherwise, i.e. if the file can't be opened for writing.
*/
public static native boolean setFilePath(String filePath);
/**
* Sets maximum size of the file to where the internal TDLib log is written before the file will be auto-rotated. Unused if log is not written to a file. Defaults to 10 MB.
* @param maxFileSize Maximum size of the file to where the internal TDLib log is written before the file will be auto-rotated. Should be positive.
*/
public static native void setMaxFileSize(long maxFileSize);
/**
* Sets the verbosity level of the internal logging of TDLib. By default the TDLib uses a verbosity level of 5 for logging.
* @param verbosityLevel New value of the verbosity level for logging. Value 0 corresponds to fatal errors, value 1 corresponds to errors, value 2 corresponds to warnings and debug warnings, value 3 corresponds to informational, value 4 corresponds to debug, value 5 corresponds to verbose debug, value greater than 5 and up to 1024 can be used to enable even more logging.
*/
public static native void setVerbosityLevel(int verbosityLevel);
}

View File

@ -3,7 +3,10 @@ package it.tdlight.tdlight;
import it.tdlight.tdlib.NativeLog;
import it.tdlight.tdlight.utils.ObjectsUtils;
public class Log extends NativeLog {
/**
* Interface for managing the internal logging of TDLib. By default TDLib writes logs to stderr or an OS specific log and uses a verbosity level of 5.
*/
public class Log {
static {
try {
@ -17,6 +20,32 @@ public class Log extends NativeLog {
private static final FatalErrorCallbackPtr defaultFatalErrorCallbackPtr = System.err::println;
private static FatalErrorCallbackPtr fatalErrorCallback = defaultFatalErrorCallbackPtr;
/**
* Sets the path to the file to where the internal TDLib log will be written. By default TDLib writes logs to stderr or an OS specific log. Use this method to write the log to a file instead.
* @param filePath Path to a file where the internal TDLib log will be written. Use an empty path to switch back to the default logging behaviour.
* @return True on success, or false otherwise, i.e. if the file can't be opened for writing.
*/
public static boolean setFilePath(String filePath) {
return NativeLog.setFilePath(filePath);
}
/**
* Sets maximum size of the file to where the internal TDLib log is written before the file will be auto-rotated. Unused if log is not written to a file. Defaults to 10 MB.
* @param maxFileSize Maximum size of the file to where the internal TDLib log is written before the file will be auto-rotated. Should be positive.
*/
public static void setMaxFileSize(long maxFileSize) {
NativeLog.setMaxFileSize(maxFileSize);
}
/**
* Sets the verbosity level of the internal logging of TDLib. By default the TDLib uses a verbosity level of 5 for logging.
* @param verbosityLevel New value of the verbosity level for logging. Value 0 corresponds to fatal errors, value 1 corresponds to errors, value 2 corresponds to warnings and debug warnings, value 3 corresponds to informational, value 4 corresponds to debug, value 5 corresponds to verbose debug, value greater than 5 and up to 1024 can be used to enable even more logging.
*/
public static void setVerbosityLevel(int verbosityLevel) {
NativeLog.setVerbosityLevel(verbosityLevel);
}
/**
* 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.

View File

@ -100,7 +100,29 @@ public class LoadLibrary {
private static void loadJarLibrary(String libname, Arch arch, Os os) throws IOException, CantLoadLibrary {
Path tempPath = Files.createDirectories(librariesPath.resolve("version-" + libsVersion).resolve(libname));
Path tempFile = Paths.get(tempPath.toString(), libname + getExt(os));
InputStream libInputStream = LoadLibrary.class.getResourceAsStream(createPath("libs", os.name(), arch.name(), libname) + getExt(os));
Class<?> classForResource = null;
switch (os) {
case linux:
switch (arch) {
case amd64:
classForResource = it.tdlight.tdlight.linux.amd64.LoadLibrary.class;
break;
case aarch64:
classForResource = it.tdlight.tdlight.linux.aarch64.LoadLibrary.class;
break;
}
break;
case win:
if (arch == Arch.amd64) {
classForResource = it.tdlight.tdlight.win.amd64.LoadLibrary.class;
}
break;
}
if (classForResource == null) {
throw new IOException("Native libraries for platform " + os + "-" + arch + " not found!");
}
String libPath = createPath("libs", os.name(), arch.name(), libname) + getExt(os);
InputStream libInputStream = classForResource.getResourceAsStream(libPath);
if (Files.notExists(tempFile)) {
Files.copy(libInputStream, tempFile);
}