/
FileLogger.java
110 lines (97 loc) · 3.97 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
package net.ME1312.Galaxi.Engine.Library.Log;
import net.ME1312.Galaxi.Galaxi;
import net.ME1312.Galaxi.Library.Util;
import java.io.*;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.regex.Pattern;
import static net.ME1312.Galaxi.Engine.GalaxiOption.LOG_DIRECTORY;
import static net.ME1312.Galaxi.Engine.GalaxiOption.USE_LOG_FILE;
import static net.ME1312.Galaxi.Engine.GalaxiOption.USE_RAW_LOG;
/**
* Log File Writer Class
*/
public final class FileLogger extends OutputStream {
private static FileOutputStream iwriter = null;
private static FileOutputStream tmpwriter = null;
private static OutputStream writer = null;
private static File file = null;
private static File tmp = null;
private OutputStream origin;
FileLogger(OutputStream origin) throws IOException {
this.origin = origin;
if (tmpwriter == null) {
File dir = LOG_DIRECTORY.get();
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) {}
String name = Galaxi.getInstance().getAppInfo().getName() + " #" + i + " (" + new SimpleDateFormat("MM-dd-yyyy").format(Calendar.getInstance().getTime()) + ')';
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.get())) {
dir.mkdirs();
if (USE_RAW_LOG.usr().equalsIgnoreCase("true") || (USE_RAW_LOG.usr().length() <= 0 && USE_RAW_LOG.get())) {
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/Library/Files/GalaxiLog.htm", file.getAbsolutePath());
iwriter = new FileOutputStream(file, true);
iwriter.write(("<h1>" + name + "</h1>\n").getBytes("UTF-8"));
iwriter.flush();
writer = HTMLogger.wrap(iwriter);
}
}
}
}
@Override
public void write(int b) throws IOException {
origin.write(b);
if (writer != null) {
writer.write(b);
writer.flush();
}
if (tmpwriter != null) {
tmpwriter.write(b);
tmpwriter.flush();
}
}
/**
* Get the file that is currently being written to
*
* @return Log File
*/
public static File getFile() {
return tmp;
}
private static void stop() {
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>\n</html>").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;
}
}