Files
VisualSapfor/src/Common/Utils/Loggable.java

48 lines
1.6 KiB
Java
Raw Normal View History

2024-10-07 17:46:38 +03:00
package Common.Utils;
import Common_old.UI.DebugPrintLevel;
import Common_old.UI.UI;
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 Print(DebugPrintLevel level, String message) {
if (level.isEnabled())
2024-10-07 14:22:52 +03:00
Print(CommonUtils.Brackets(level.getDescription()) + " " + message);
2023-09-17 22:13:42 +03:00
}
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 15:32:39 +03:00
if (CommonUtils.hasUI())
2023-09-17 22:13:42 +03:00
UI.Error("Возникло исключение. Подробности в файле журнала\n" +
2024-10-07 14:22:52 +03:00
CommonUtils.Brackets(getLogFile().getAbsolutePath()));
2023-09-17 22:13:42 +03:00
}
}