-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathLogKitten.java
384 lines (352 loc) · 12.3 KB
/
LogKitten.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
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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
// package org.usfirst.frc4904.standard;
// import java.io.BufferedOutputStream;
// import java.io.File;
// import java.io.FileOutputStream;
// import java.io.IOException;
// import java.io.PrintWriter;
// import java.io.StringWriter;
// import java.nio.file.Files;
// import java.text.SimpleDateFormat;
// import java.util.Date;
// import edu.wpi.first.hal.HAL; // DONT REMOVE,
// public class LogKitten {
// private static BufferedOutputStream fileOutput;
// public final static KittenLevel LEVEL_WTF = KittenLevel.WTF;
// public final static KittenLevel LEVEL_FATAL = KittenLevel.FATAL;
// public final static KittenLevel LEVEL_ERROR = KittenLevel.ERROR;
// public final static KittenLevel LEVEL_WARN = KittenLevel.WARN;
// public final static KittenLevel LEVEL_VERBOSE = KittenLevel.VERBOSE;
// public final static KittenLevel LEVEL_DEBUG = KittenLevel.DEBUG;
// public final static KittenLevel DEFAULT_LOG_LEVEL = KittenLevel.DEBUG;
// public final static KittenLevel DEFAULT_PRINT_LEVEL = KittenLevel.WARN;
// public final static KittenLevel DEFAULT_DS_LEVEL = LogKitten.DEFAULT_PRINT_LEVEL;
// private static KittenLevel logLevel = LogKitten.DEFAULT_LOG_LEVEL;
// private static KittenLevel printLevel = LogKitten.DEFAULT_PRINT_LEVEL;
// private static KittenLevel dsLevel = LogKitten.DEFAULT_DS_LEVEL;
// private static String LOG_PATH = "/home/lvuser/logs/";
// private static String LOG_ALIAS_PATH = LogKitten.LOG_PATH + "recent.log";
// private static volatile boolean PRINT_MUTE = false;
// private static final SimpleDateFormat TIMESTAMP_FORMAT = new SimpleDateFormat("yyyy-MM-dd_HH:mm:ss");
// static {
// File logPathDirectory = new File(LogKitten.LOG_PATH);
// try {
// if (!logPathDirectory.isDirectory()) { // ensure that the directory /home/lvuser/logs/ exists
// logPathDirectory.mkdirs(); // otherwise create all the directories of the path
// }
// } catch (SecurityException se) {
// System.out.println("Could not create log directory");
// se.printStackTrace();
// }
// String filePath = LogKitten.LOG_PATH + LogKitten.timestamp() + ".log"; // Set this sessions log to
// // /home/lvuser/logs/[current time].log
// File file = new File(filePath);
// try {
// // Create new file if it doesn't exist (this should happen)
// file.createNewFile(); // creates if does not exist
// // Create FileOutputStream to actually write to the file.
// LogKitten.fileOutput = new BufferedOutputStream(new FileOutputStream(file));
// } catch (IOException ioe) {
// System.out.println("Could not open logfile");
// ioe.printStackTrace();
// }
// File logAlias = new File(LogKitten.LOG_ALIAS_PATH);
// try {
// if (logAlias.exists()) {
// logAlias.delete();
// }
// Files.createSymbolicLink(logAlias.toPath(), file.toPath());
// } catch (IOException ioe) {
// System.out.println("Could not alias logfile");
// ioe.printStackTrace();
// }
// }
// /**
// * Get the name of a logger method's caller
// *
// * @return the caller for the callee `f`, `e`, `w`, `v`, or `d`
// */
// private static String getLoggerMethodCallerMethodName() {
// return Thread.currentThread().getStackTrace()[4].getMethodName(); // caller of the logger method is fifth in the
// // stack trace
// }
// /**
// * Get the name of a logger method's calling class
// *
// * @return the caller for the callee `f`, `e`, `w`, `v`, or `d`
// */
// private static String getLoggerMethodCallerClassName() {
// String[] trace = Thread.currentThread().getStackTrace()[4].getClassName().split("\\."); // caller of the logger
// // method is fifth in
// // the stack trace
// if (trace.length == 0) {
// return "";
// }
// return trace[trace.length - 1]; // don't include the package name
// }
// /**
// * Set the default level for which logs will be streamed to a file (for all
// * LogKitten instances)
// *
// * @param DEFAULT_LOG_LEVEL default write-to-file level
// */
// public static void setDefaultLogLevel(KittenLevel DEFAULT_LOG_LEVEL) {
// LogKitten.logLevel = DEFAULT_LOG_LEVEL;
// }
// /**
// * Set the default level for which logs will be printed to the console (for all
// * LogKitten instances)
// *
// * @param DEFAULT_PRINT_LEVEL default console log level
// */
// public static void setDefaultPrintLevel(KittenLevel DEFAULT_PRINT_LEVEL) {
// LogKitten.printLevel = DEFAULT_PRINT_LEVEL;
// }
// /**
// * Set the default level for which logs will be printed to the driver station
// * (for all LogKitten instances)
// *
// * @param DEFAULT_DS_LEVEL default driver station level
// */
// public static void setDefaultDSLevel(KittenLevel DEFAULT_DS_LEVEL) {
// LogKitten.dsLevel = DEFAULT_DS_LEVEL;
// }
// /**
// * Set the logfile path for all LogKitten instances
// *
// * @param LOG_PATH logfile path as a string
// */
// public static void setLogPath(String LOG_PATH) {
// LogKitten.LOG_PATH = LOG_PATH;
// }
// /**
// * Mutes all messages except those overriding (useful for debugging)
// *
// * @param mute
// */
// public static void setPrintMute(boolean mute) {
// LogKitten.PRINT_MUTE = mute;
// }
// /**
// * Like DriverStation.reportError, but without stack trace nor printing to
// * System.err (updated for 2017 WPILib release)
// *
// * @see edu.wpi.first.wpilibj.DriverStation.reportError
// * @param errorString
// */
// private static void reportErrorToDriverStation(String details, String errorMessage, KittenLevel logLevel) {
// // HAL.sendError(true, logLevel.getSeverity(), false, errorMessage, details, "", false);
// // TO DO: this is now broken. commenting out causes silent failure. how do you fix it
// }
// public static synchronized void logMessage(Object message, KittenLevel level, boolean override) {
// message = message.toString(); // Not strictly needed, but good practice
// if (LogKitten.logLevel.compareTo(level) >= 0) {
// String content = LogKitten.timestamp() + " " + level.getName() + ": "
// + LogKitten.getLoggerMethodCallerMethodName() + ": " + message + " \n";
// try {
// if (LogKitten.fileOutput != null) {
// LogKitten.fileOutput.write(content.getBytes());
// } else {
// System.out.println("Error logging: logfile not open");
// }
// } catch (IOException ioe) {
// System.out.println("Error logging " + level.getName() + " message");
// ioe.printStackTrace();
// }
// }
// if (!LogKitten.PRINT_MUTE || override) {
// String printContent = level.getName() + ": " + LogKitten.getLoggerMethodCallerClassName() + "#"
// + LogKitten.getLoggerMethodCallerMethodName() + ": " + message + " \n";
// if (LogKitten.printLevel.compareTo(level) >= 0) {
// System.out.println(printContent);
// }
// if (LogKitten.dsLevel.compareTo(level) >= 0) {
// LogKitten.reportErrorToDriverStation(
// LogKitten.getLoggerMethodCallerClassName() + "#" + LogKitten.getLoggerMethodCallerMethodName(),
// level.getName() + ": " + message, level);
// }
// }
// }
// /**
// * What a Terrible Failure: Report a condition that should never happen,
// * allowing override
// *
// * @param message
// * @param override
// */
// public static void wtf(Object message, boolean override) {
// LogKitten.logMessage(message, KittenLevel.WTF, override);
// }
// /**
// * What a Terrible Failure: Report a condition that should never happen
// *
// * @param message the message to log
// */
// public static void wtf(Object message) { // Log WTF message
// LogKitten.logMessage(message, KittenLevel.WTF, false);
// }
// /**
// * Log message at level FATAL allowing override
// *
// * @param message
// * @param override
// */
// public static void f(Object message, boolean override) {
// LogKitten.logMessage(message, KittenLevel.FATAL, override);
// }
// /**
// * Log message at level FATAL
// *
// * @param message the message to log
// */
// public static void f(Object message) { // Log fatal message
// LogKitten.logMessage(message, KittenLevel.FATAL, false);
// }
// /**
// * Log message at ERROR allowing override
// *
// * @param message
// * @param override
// */
// public static void e(Object message, boolean override) {
// LogKitten.logMessage(message, KittenLevel.ERROR, override);
// }
// /**
// * Log message at level ERROR
// *
// * @param message the message to log
// */
// public static void e(Object message) { // Log error message
// LogKitten.logMessage(message, KittenLevel.ERROR, false);
// }
// /**
// * Log message at WARN allowing override
// *
// * @param message
// * @param override
// */
// public static void w(Object message, boolean override) {
// LogKitten.logMessage(message, KittenLevel.WARN, override);
// }
// /**
// * Log message at level WARN
// *
// * @param message the message to log
// */
// public static void w(Object message) { // Log warn message
// LogKitten.logMessage(message, KittenLevel.WARN, false);
// }
// /**
// * Log message at VERBOSE allowing override
// *
// * @param message
// * @param override
// */
// public static void v(Object message, boolean override) {
// LogKitten.logMessage(message, KittenLevel.VERBOSE, override);
// }
// /**
// * Log message at level VERBOSE
// *
// * @param message the message to log
// */
// public static void v(Object message) { // Log verbose message
// LogKitten.logMessage(message, KittenLevel.VERBOSE, false);
// }
// /**
// * Log message at VERBOSE (INFO links to verbose) allowing override
// *
// * @param message
// * @param override
// */
// public static void i(Object message, boolean override) {
// LogKitten.logMessage(message, KittenLevel.VERBOSE, override);
// }
// /**
// * Log message at VERBOSE (INFO links to verbose)
// *
// * @param message
// */
// public static void i(Object message) {
// LogKitten.logMessage(message, KittenLevel.VERBOSE, false);
// }
// /**
// * Log message at level DEBUG allowing override
// *
// * @param message
// * @param override
// */
// public static void d(Object message, boolean override) {
// LogKitten.logMessage(message, KittenLevel.DEBUG, override);
// }
// /**
// * Log message at level DEBUG
// *
// * @param message the message to log
// */
// public static void d(Object message) { // Log debug message
// LogKitten.logMessage(message, KittenLevel.DEBUG, false);
// }
// /**
// * Log exception at level ERROR allowing override
// *
// * @param ex the exception to log
// * @param override whether or not to override
// */
// public static void ex(Exception ex, boolean override) {
// StringWriter stackTraceString = new StringWriter();
// ex.printStackTrace(new PrintWriter(stackTraceString));
// LogKitten.logMessage(stackTraceString.toString(), KittenLevel.ERROR, override);
// }
// /**
// * Log exception at level ERROR
// *
// * @param ex the exception to log
// */
// public static void ex(Exception ex) {
// LogKitten.ex(ex, false);
// }
// /**
// * Tries to close the logfile stream
// */
// public static synchronized void clean() {
// try {
// if (LogKitten.fileOutput != null) {
// LogKitten.fileOutput.close();
// }
// } catch (IOException ioe) {
// System.out.println("Could not close logfile output. This should never happen");
// ioe.printStackTrace();
// }
// }
// /**
// * Get a timestamp for the current datetime - me-wow!
// *
// * @return timestamp as string in the format "YEAR-MONTH-DAY_HOUR:MIN:SEC"
// */
// private static synchronized String timestamp() {
// return LogKitten.TIMESTAMP_FORMAT.format(new Date());
// }
// public static enum KittenLevel {
// // Defined in decreasing order of severity. Enum.compareTo uses the definition
// // order to compare enum values.
// WTF, FATAL, ERROR, WARN, VERBOSE, DEBUG;
// /**
// * Get the level severity
// *
// * @return the level severity as an int
// */
// public int getSeverity() {
// // Severity is the same as the ordinal, which increases with the order of the
// // enum values
// return ordinal();
// }
// /**
// * Get the level name
// *
// * @return level name as a string
// */
// public String getName() {
// return name(); // Enum.name() is the Java builtin to get the name of an enum value
// }
// }
// }