forked from pquiring/javaforce
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathJFLog.java
More file actions
executable file
·257 lines (230 loc) · 6.62 KB
/
Copy pathJFLog.java
File metadata and controls
executable file
·257 lines (230 loc) · 6.62 KB
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
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
package javaforce;
import java.io.*;
import java.util.*;
/**
* JFLog is a file logger with support for multiple files and optional outputs
* to System.out as well.
*/
public class JFLog {
private static class LogInstance {
private Object lock = new Object();
private FileOutputStream fos;
private PrintStream stdout;
private long filesize;
private String filename;
private boolean enabled = true;
}
private static Hashtable<Integer, LogInstance> list = new Hashtable<Integer, LogInstance>();
private static boolean useTimestamp = false;
private static long timestampBase;
private static class TraceException extends Exception {
public TraceException(String msg) {
super(msg);
}
}
public static boolean init(int id, String filename, boolean append, PrintStream stdout) {
LogInstance log = new LogInstance();
log.stdout = stdout;
log.filename = filename;
if (filename != null) {
if (append) {
File file = new File(filename);
log.filesize = file.length();
} else {
log.filesize = 0;
}
try {
log.fos = new FileOutputStream(filename, append);
} catch (Exception e) {
System.out.println("Log:create file failed:" + filename);
return false;
}
}
synchronized(list) {
list.put(id, log);
}
return true;
}
public static boolean init(String filename, boolean stdout) {
return init(0, filename, false, stdout ? System.out : null);
}
public static boolean init(int id, String filename, boolean stdout) {
return init(id, filename, false, stdout ? System.out : null);
}
public static boolean append(String filename, boolean stdout) {
return init(0, filename, true, stdout ? System.out : null);
}
public static boolean append(int id, String filename, boolean stdout) {
return init(id, filename, true, stdout ? System.out : null);
}
public static boolean close(int id) {
LogInstance log = list.get(id);
if (log == null) {
return false;
}
synchronized(list) {
list.remove(id);
}
try {
if (log.fos != null) {
log.fos.close();
}
} catch (Exception e) {
}
return true;
}
public static boolean close() {
return close(0);
}
/** Uses a timestamp instead of date/time for each message logged. */
public static void enableTimestamp(boolean state) {
timestampBase = System.nanoTime() / 1000000;
useTimestamp = state;
}
public static boolean log(String msg) {
return log(0, msg);
}
public static boolean log(int id, String msg) {
LogInstance log = list.get(id);
if (log == null) {
System.out.println(msg);
return false;
}
if (!log.enabled) {
return true;
}
if (!useTimestamp) {
Calendar cal = Calendar.getInstance();
msg = String.format("[%1$04d/%2$02d/%3$02d %4$02d:%5$02d:%6$02d] %7$s\r\n",
cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY),
cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND), msg);
} else {
msg = String.format("[%1$d] %2$s\r\n", (System.nanoTime() / 1000000) - timestampBase, msg);
}
synchronized (log.lock) {
if (log.stdout != null) {
log.stdout.print(msg);
}
if (log.fos == null) return true;
try {
log.fos.write(msg.getBytes());
log.fos.flush();
} catch (Exception e) {
System.out.println("Log:write file failed:" + id);
return false;
}
log.filesize += msg.length();
if (log.filesize > 1024 * 1024) {
log.filesize = 0;
//start new log file
Calendar cal = Calendar.getInstance();
String tmp = String.format(".%1$04d-%2$02d-%3$02d-%4$02d-%5$02d-%6$02d",
cal.get(Calendar.YEAR), cal.get(Calendar.MONTH) + 1, cal.get(Calendar.DAY_OF_MONTH), cal.get(Calendar.HOUR_OF_DAY),
cal.get(Calendar.MINUTE), cal.get(Calendar.SECOND));
int idx = log.filename.lastIndexOf('.');
if (idx == -1) {
tmp = log.filename + tmp;
} else {
tmp = log.filename.substring(0, idx) + tmp + log.filename.substring(idx);
}
try {
log.fos.close();
} catch (Exception e1) {
}
File file = new File(log.filename);
file.renameTo(new File(tmp));
try {
log.fos = new FileOutputStream(log.filename);
} catch (Exception e2) {
}
}
}
return true;
}
public static boolean log(Throwable t) {
return log(0, t);
}
public static boolean log(int id, Throwable t) {
StringBuffer buf = new StringBuffer();
buf.append(t.toString());
buf.append("\r\n");
StackTraceElement ste[] = t.getStackTrace();
int start = 0;
if (t instanceof TraceException) {
start = 1; //skip JFLog.logTrace() step
}
if (ste != null) {
for (int a = start; a < ste.length; a++) {
buf.append("\tat ");
buf.append(ste[a].toString());
buf.append("\r\n");
}
}
return log(id, buf.toString());
}
public static boolean log(String msg, Throwable t) {
return log(0, msg, t);
}
public static boolean log(int id, String msg, Throwable t) {
if (!log(id, msg)) {
return false;
}
return log(id, t);
}
public static void setEnabled(int id, boolean state) {
LogInstance log = list.get(id);
if (log == null) {
return;
}
log.enabled = state;
}
public static void setEnabled(boolean state) {
setEnabled(0, state);
}
/**
* NOTE: write() doesn't cycle log files.
*/
public static boolean write(int id, byte data[], int off, int len) {
LogInstance log = list.get(id);
synchronized (log.lock) {
try {
log.fos.write(data, off, len);
log.fos.flush();
log.filesize += len;
} catch (Exception e) {
return false;
}
}
return true;
}
/**
* NOTE: write() doesn't cycle log files.
*/
public static boolean write(byte data[], int off, int len) {
return write(0, data, off, len);
}
public static OutputStream getOutputStream(int id) {
LogInstance log = list.get(id);
if (log == null) {
return null;
}
return log.fos;
}
public static OutputStream getOutputStream() {
return getOutputStream(0);
}
public static void logTrace(int id, String msg) {
try {
throw new TraceException(msg);
} catch (Exception e) {
log(id, e);
}
}
public static void logTrace(String msg) {
try {
throw new TraceException(msg);
} catch (Exception e) {
log(0, e);
}
}
}