Fix fatal error callback

This commit is contained in:
Andrea Cavalli 2020-09-08 16:23:46 +02:00
parent e92c04098f
commit 107307d5d0
4 changed files with 91 additions and 20 deletions

View File

@ -15,7 +15,7 @@
* along with JTdlib. If not, see <http://www.gnu.org/licenses/>.
*/
package it.tdlight.tdlight;
package it.tdlight.tdlib;
/**
* A type of callback function that will be called when a fatal error happens.

View File

@ -17,11 +17,69 @@
package it.tdlight.tdlib;
/**
* Class used for managing internal TDLib logging.
* Use TdApi.*Log* methods instead.
*/
public class NativeLog {
private static final FatalErrorCallbackPtr defaultFatalErrorCallbackPtr = System.err::println;
private static FatalErrorCallbackPtr 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);
/**
* 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 synchronized void onFatalError(String errorMessage) {
new Thread(() -> NativeLog.fatalErrorCallback.onFatalError(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.
*/
public static synchronized void setFatalErrorCallback(FatalErrorCallbackPtr fatalErrorCallback) {
NativeLog.fatalErrorCallback = ObjectsUtils.requireNonNullElse(fatalErrorCallback, defaultFatalErrorCallbackPtr);
}
}

View File

@ -1,4 +1,4 @@
package it.tdlight.tdlight.utils;
package it.tdlight.tdlib;
public class ObjectsUtils {
/**

View File

@ -1,10 +1,12 @@
package it.tdlight.tdlight;
import it.tdlight.tdlib.FatalErrorCallbackPtr;
import it.tdlight.tdlib.NativeLog;
import it.tdlight.tdlight.utils.ObjectsUtils;
import it.tdlight.tdlib.TdApi;
/**
* 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.
* Class used for managing internal TDLib logging.
* Use TdApi.*Log* methods instead.
*/
public class Log {
@ -17,15 +19,15 @@ public class Log {
}
}
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.
* @deprecated Unfortunately TDLib redirects all STDERR to a file, try to avoid this function.
* 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 synchronized boolean setFilePath(String filePath) {
@ -33,17 +35,32 @@ public class Log {
}
/**
* 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.
* 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 synchronized 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.
* 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 synchronized void setVerbosityLevel(int verbosityLevel) {
NativeLog.setVerbosityLevel(verbosityLevel);
}
@ -53,10 +70,6 @@ public class Log {
* @param fatalErrorCallback Callback that will be called when a fatal error happens. Pass null to restore default callback.
*/
public static synchronized void setFatalErrorCallback(FatalErrorCallbackPtr fatalErrorCallback) {
Log.fatalErrorCallback = ObjectsUtils.requireNonNullElse(fatalErrorCallback, defaultFatalErrorCallbackPtr);
}
private static synchronized void onFatalError(String errorMessage) {
new Thread(() -> Log.fatalErrorCallback.onFatalError(errorMessage)).start();
NativeLog.setFatalErrorCallback(fatalErrorCallback);
}
}