tdlight-java/tdlight-java/src/main/java/it/tdlight/Log.java

102 lines
3.7 KiB
Java
Raw Normal View History

2023-04-27 02:14:39 +02:00
package it.tdlight;
2020-08-18 23:13:36 +02:00
2020-10-11 12:41:29 +02:00
import it.tdlight.jni.TdApi;
2021-10-22 19:53:23 +02:00
import it.tdlight.jni.TdApi.LogStreamDefault;
import it.tdlight.jni.TdApi.LogStreamFile;
import it.tdlight.jni.TdApi.SetLogVerbosityLevel;
2022-10-17 12:20:17 +02:00
import it.tdlight.tdnative.NativeClient.LogMessageHandler;
2021-10-22 19:53:23 +02:00
import java.util.concurrent.atomic.AtomicLong;
import java.util.concurrent.atomic.AtomicReference;
2020-08-18 23:13:36 +02:00
2020-08-21 00:01:01 +02:00
/**
2021-09-27 19:27:13 +02:00
* Class used for managing internal TDLib logging. Use TdApi.*Log* methods instead.
2020-08-21 00:01:01 +02:00
*/
2021-09-27 19:27:13 +02:00
public final class Log {
2020-08-18 23:13:36 +02:00
static {
try {
2023-05-10 10:12:43 +02:00
Init.init();
2020-08-18 23:13:36 +02:00
} catch (Throwable throwable) {
throwable.printStackTrace();
System.exit(0);
}
}
2021-10-22 19:53:23 +02:00
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;
}
2020-08-21 00:01:01 +02:00
/**
2021-09-27 19:27:13 +02:00
* 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.
2020-09-08 16:23:46 +02:00
*
2021-09-27 19:27:13 +02:00
* @param filePath Path to a file for writing TDLib internal log. Use an empty path to switch back to logging to the
* System.err.
2020-09-08 16:23:46 +02:00
* @return whether opening the log file succeeded.
2021-09-27 19:27:13 +02:00
* @deprecated As of TDLib 1.4.0 in favor of {@link TdApi.SetLogStream}, to be removed in the future.
2020-08-21 00:01:01 +02:00
*/
2020-09-02 00:38:21 +02:00
@Deprecated
2021-10-22 19:53:23 +02:00
public static boolean setFilePath(String filePath) {
LOG_FILE_PATH.set(filePath);
return updateLog();
2020-08-21 00:01:01 +02:00
}
/**
2020-09-08 16:23:46 +02:00
* Changes the maximum size of TDLib log file.
*
2021-09-27 19:27:13 +02:00
* @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.
2020-09-08 16:23:46 +02:00
* @deprecated As of TDLib 1.4.0 in favor of {@link TdApi.SetLogStream}, to be removed in the future.
2020-08-21 00:01:01 +02:00
*/
2020-09-08 16:23:46 +02:00
@Deprecated
2021-10-22 19:53:23 +02:00
public static void setMaxFileSize(long maxFileSize) {
LOG_MAX_FILE_SIZE.set(maxFileSize);
updateLog();
2020-08-21 00:01:01 +02:00
}
/**
2020-09-08 16:23:46 +02:00
* Changes TDLib log verbosity.
*
2021-09-27 19:27:13 +02:00
* @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.
2020-09-08 16:23:46 +02:00
* Default value of the log verbosity level is 5.
2021-09-27 19:27:13 +02:00
* @deprecated As of TDLib 1.4.0 in favor of {@link TdApi.SetLogVerbosityLevel}, to be removed in the future.
2020-08-21 00:01:01 +02:00
*/
2020-09-08 16:23:46 +02:00
@Deprecated
2021-10-22 19:53:23 +02:00
public static void setVerbosityLevel(int verbosityLevel) {
NativeClientAccess.execute(new SetLogVerbosityLevel(verbosityLevel));
2022-10-17 12:20:17 +02:00
updateLog();
2020-08-21 00:01:01 +02:00
}
2020-08-18 23:13:36 +02:00
/**
2021-09-27 19:27:13 +02:00
*
2022-10-17 12:20:17 +02:00
* 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
2020-08-18 23:13:36 +02:00
*/
2022-10-17 12:20:17 +02:00
public static void setLogMessageHandler(int maxVerbosityLevel, LogMessageHandler logMessageHandler) {
NativeClientAccess.setLogMessageHandler(maxVerbosityLevel, logMessageHandler);
2020-08-18 23:13:36 +02:00
}
}