2024-10-07 17:46:38 +03:00
|
|
|
package Common.Utils;
|
2024-10-08 22:33:49 +03:00
|
|
|
import Common.Visual.CommonUI;
|
2024-10-07 00:58:29 +03:00
|
|
|
import Common_old.Utils.Utils;
|
2023-09-17 22:13:42 +03:00
|
|
|
|
|
|
|
|
import java.io.File;
|
|
|
|
|
import java.io.FileWriter;
|
|
|
|
|
import java.io.PrintWriter;
|
|
|
|
|
import java.io.StringWriter;
|
|
|
|
|
import java.nio.file.Paths;
|
|
|
|
|
import java.util.Date;
|
|
|
|
|
public interface Loggable {
|
|
|
|
|
String getLogHomePath();
|
|
|
|
|
String getLogName();
|
|
|
|
|
default File getLogFile() {
|
|
|
|
|
return Paths.get(getLogHomePath(), (getLogName() + "_log.txt")).toFile();
|
|
|
|
|
}
|
|
|
|
|
default void ClearLog() {
|
|
|
|
|
try {
|
|
|
|
|
Utils.forceDeleteWithCheck(getLogFile());
|
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
default void Print(String message) {
|
|
|
|
|
try {
|
|
|
|
|
FileWriter Log = new FileWriter(getLogFile(), true);
|
2024-10-07 14:22:52 +03:00
|
|
|
String datedMessage = CommonUtils.Brackets(new Date()) + " " + message;
|
2023-09-17 22:13:42 +03:00
|
|
|
Log.write(datedMessage + "\n");
|
|
|
|
|
Log.close();
|
|
|
|
|
} catch (Exception ignored) {
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
default void PrintException(Exception ex) {
|
|
|
|
|
StringWriter out = new StringWriter();
|
|
|
|
|
PrintWriter writer = new PrintWriter(out);
|
|
|
|
|
ex.printStackTrace(writer);
|
|
|
|
|
writer.flush();
|
|
|
|
|
Print(out.toString());
|
2024-10-08 22:33:49 +03:00
|
|
|
if (CommonUI.isActive())
|
|
|
|
|
CommonUI.Error("Возникло исключение. Подробности в файле журнала\n" +
|
2024-10-07 14:22:52 +03:00
|
|
|
CommonUtils.Brackets(getLogFile().getAbsolutePath()));
|
2023-09-17 22:13:42 +03:00
|
|
|
}
|
|
|
|
|
}
|