/
FileLogger.java
122 lines (107 loc) · 4.73 KB
/
FileLogger.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
package net.ME1312.Galaxi.Engine.Runtime;
import net.ME1312.Galaxi.Galaxi;
import net.ME1312.Galaxi.Library.Util;
import net.ME1312.Galaxi.Log.LogMessenger;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.AnsiString;
import java.awt.*;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.regex.Pattern;
import static java.nio.charset.StandardCharsets.UTF_8;
import static net.ME1312.Galaxi.Engine.GalaxiOption.*;
final class FileLogger implements LogMessenger {
private FileOutputStream iwriter = null;
private FileOutputStream tmpwriter = null;
private OutputStream writer = null;
private File file = null;
private File tmp = null;
private final ConsoleLogger child;
FileLogger(ConsoleLogger child) throws IOException {
this.child = child;
if (tmpwriter == null) {
File dir = LOG_DIRECTORY.app();
int i = 1;
if (dir.isDirectory()) try {
for (File file : dir.listFiles()) {
if (Pattern.compile("^" + Pattern.quote(Galaxi.getInstance().getAppInfo().getName()) + " #\\d+ \\((?:\\d{1,2}-){2}\\d+\\)\\.log(?:\\.(?:txt|htm|zip))?$").matcher(file.getName()).find()) i++;
}
} catch (Exception e) {}
Date time = Calendar.getInstance().getTime();
String name = Galaxi.getInstance().getAppInfo().getName() + " #" + i + " (" + new SimpleDateFormat("MM-dd-yyyy").format(time) + ')';
String nameX = Galaxi.getInstance().getAppInfo().getName() + " #" + i + " [" + new SimpleDateFormat("M/d/yyyy").format(time) + ']';
tmp = File.createTempFile(Galaxi.getInstance().getAppInfo().getName() + '.', ".log");
tmp.deleteOnExit();
tmpwriter = new FileOutputStream(tmp);
if (USE_LOG_FILE.usr().equalsIgnoreCase("true") || (USE_LOG_FILE.usr().length() == 0 && USE_LOG_FILE.app())) {
dir.mkdirs();
if (USE_RAW_LOG.usr().equalsIgnoreCase("true") || (USE_RAW_LOG.usr().length() == 0 && USE_RAW_LOG.app())) {
file = new File(dir, name + ".log.txt");
writer = iwriter = new FileOutputStream(file);
} else {
file = new File(dir, name + ".log.htm");
Util.copyFromJar(FileLogger.class.getClassLoader(), "net/ME1312/Galaxi/Engine/Runtime/Files/GalaxiLog.htm", file.getAbsolutePath());
iwriter = new FileOutputStream(file, true);
iwriter.write(("<h1>" + nameX.replace("<", "<").replace(">", ">").replace("&", "&") + "</h1>\n").getBytes(UTF_8));
iwriter.flush();
writer = HTMLogger.wrap(iwriter);
}
}
}
}
private final boolean ANSI = USE_RAW_LOG_ANSI.usr().equalsIgnoreCase("true") || (USE_RAW_LOG_ANSI.usr().length() == 0 && USE_ANSI.def());
public void log(String s) throws IOException {
child.log(s);
if (writer instanceof HTMLogger) {
writer.write(s.getBytes(UTF_8));
writer.flush();
} else if (writer != null) {
if (ANSI) writer.write(s.getBytes(UTF_8));
else writer.write(((String) new AnsiString(s).getPlain()).getBytes(UTF_8));
writer.flush();
}
if (tmpwriter != null) {
tmpwriter.write(s.getBytes(UTF_8));
tmpwriter.flush();
}
}
File history() {
return tmp;
}
void close() {
File compressed = (file != null)?new File(file.getParentFile(), file.getName().substring(0, file.getName().length() - 4) + ".zip"):null;
try {
if (writer != null) {
if (writer instanceof HTMLogger) iwriter.write(("</body></html>").getBytes(UTF_8));
else if (ANSI) iwriter.write(Ansi.ansi().a(Ansi.Attribute.RESET).toString().getBytes(UTF_8));
writer.close();
}
if (file != null && compressed != null) {
FileOutputStream fos = new FileOutputStream(compressed);
Util.zip(file, fos);
fos.flush();
fos.close();
file.delete();
}
if (tmpwriter != null) {
tmpwriter.close();
}
} catch (Exception e) {
if (compressed != null && !compressed.exists()) compressed.delete();
}
if (tmp != null) tmp.delete();
tmp = null;
file = null;
iwriter = null;
writer = null;
}
@Override
public Color parse256(int color) {
return Console.parse256(color);
}
}