forked from NeuronRobotics/java-bowler
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLog.java
More file actions
537 lines (465 loc) · 12.3 KB
/
Log.java
File metadata and controls
537 lines (465 loc) · 12.3 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
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
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
/*******************************************************************************
* Copyright 2010 Neuron Robotics, LLC
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
******************************************************************************/
package com.neuronrobotics.sdk.common;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.io.PrintStream;
import java.io.PrintWriter;
import java.io.StringWriter;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.nio.file.StandardOpenOption;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import com.neuronrobotics.sdk.config.SDKBuildInfo;
import com.neuronrobotics.sdk.util.ThreadUtil;
// Auto-generated Javadoc
/**
* This class is the Logging Class for the NRsdk.
* @author rbreznak
*
*/
public class Log {
/** The Constant LOG. */
public static final int LOG =-1;
/** The Constant INFO. */
public static final int INFO = 0;
/** The Constant DEBUG. */
public static final int DEBUG = 1;
/** The Constant WARNING. */
public static final int WARNING = 2;
/** The Constant ERROR. */
public static final int ERROR = 3;
/** The instance. */
private static Log instance;
/** The messages. */
private Message m;
//private ArrayList<Message> messages = new ArrayList<Message>();
/** The date format. */
private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SS");
/** The minprintlevel. */
private int minprintlevel = WARNING;
/** The systemprint. */
private boolean systemprint = false;
/** The debugprint. */
private boolean debugprint = false;
/** The out stream. */
private static PrintStream outStream = System.out;
/** The out stream. */
private static PrintStream errStream = System.err;
/** The out stream. */
private static PrintStream mirrorStream = System.out;
/** The use colored prints. */
private boolean useColoredPrints=false;
private Thread logFileThread = null;
private File log=null;
private ByteList incomingErr;
private ByteList incomingOut;
/**
* Instantiates a new log.
*/
private Log() {
// private for singleton pattern
add(SDKBuildInfo.getSDKVersionString(), INFO);
}
/**
* Log an error message.
*
* @param message the message to log as an error
*/
public static void error(Object message) {
instance().add(message.toString(), ERROR);
}
/**
* Log an warning message.
*
* @param message the message to log as a warning
*/
public static void warning(String message) {
instance().add(message, WARNING);
}
/**
* Log a info message.
*
* @param message the message to log as a piece of information.
*/
public static void info(String message) {
instance().add(message, INFO);
}
/**
* Log a string.
*
* @param message The string to log.
*/
public static void log(String message) {
instance().add(message, LOG);
}
/**
* Log a debug message.
*
* @param message The debug message to log
*/
public static void debug(String message) {
instance().add(message, DEBUG);
}
/**
* Add a line to the log.
*
* @param message The line to add
*/
public static void add(String message) {
instance().add(message, LOG);
}
/**
* Add a string to the log with a specific importance.
*
* @param message the message to add
* @param importance the importance to log it as.
*/
private void add(String message, int importance) {
if( importance < minprintlevel) {
return;
}
if(m==null)
m = new Message(message, importance);
else{
m.init(message, importance);
}
//messages.add(m);
if( systemprint) {
outStream.println(m.toString());
}
}
/**
* Enable printing of output to standard out.
*
* @param systemprint the systemprint
*/
public static void enableSystemPrint(boolean systemprint) {
Log.instance().systemprint = systemprint;
}
/**
* Enable printing of debug output.
*/
public static void enableDebugPrint() {
Log.enableSystemPrint(true);
Log.setMinimumPrintLevel(DEBUG);
}
public static void disablePrint() {
Log.enableSystemPrint(false);
}
/**
* Enable printing of debug output.
*
* @param flag the flag
*/
public static void enableDebugPrint(boolean flag) {
Log.enableSystemPrint(flag);
Log.setMinimumPrintLevel(DEBUG);
}
/**
* Enable printing of debug output.
*/
public static void enableInfoPrint() {
Log.enableSystemPrint(true);
Log.setMinimumPrintLevel(INFO);
}
/**
* Enable printing of debug output.
*/
public static void enableWarningPrint() {
Log.enableSystemPrint(true);
Log.setMinimumPrintLevel(WARNING);
}
/**
* Enable printing of debug output.
*/
public static void enableErrorPrint() {
Log.enableSystemPrint(true);
Log.setMinimumPrintLevel(ERROR);
}
/**
* Set the minimum level of importance to dsplay.
* Messages below this wont be displayed.
* @param level The minimu importance level
*/
public static void setMinimumPrintLevel(int level) {
Log.instance().minprintlevel = level;
}
/**
* Set the minimum level of importance to dsplay.
* Messages below this wont be displayed.
*
* @return the minimum print level
*/
public static int getMinimumPrintLevel() {
return Log.instance().minprintlevel;
}
/**
* Get the current log (singleton) instance.
*
* @return The log instance.
*/
public static Log instance() {
if(instance == null) {
instance = new Log();
}
return instance;
}
/**
* Get a string describing the given importance level.
*
* @param importance The given importance level.
* @return the importance
*/
public String getImportance(int importance) {
switch(importance) {
case INFO:
return "Info";
case WARNING:
return "Warning";
case ERROR:
return "Error";
case DEBUG:
return "Debug";
case LOG:
default:
return "Log";
}
}
/**
* Get a string describing the given importance level.
*
* @param importance The given importance level.
* @return the importance
*/
public String getImportanceColor(int importance) {
if(isUseColoredPrints()){
switch(importance) {
case INFO:
return "\033[92m";// green
case WARNING:
return "\033[93m";// orange
case ERROR:
return "\033[31m";// red
case DEBUG:
return "\033[94m";// blue
case LOG:
default:
return "\033[92m";// green
}
}
return "";
}
/**
* Get the current output PrintStream.
*
* @return The current output PrintStream
*/
public static PrintStream getOutStream() {
return outStream;
}
/**
* Set the current output PrintStream.
*
* @param newoutStream the new out stream
*/
public static void setOutStream(PrintStream newoutStream) {
outStream = newoutStream;
}
/**
* A log message.
*
* @author rbreznak
*/
private class Message {
/** The message. */
private String message;
/** The importance. */
private int importance;
/** The datetime. */
private Date datetime;
/** The calling class. */
private String callingClass;
/**
* Instantiates a new message.
*
* @param message the message
* @param importance the importance
*/
public Message(String message, int importance) {
init(message, importance);
}
/**
* Inits the.
*
* @param message the message
* @param importance the importance
*/
public void init(String message, int importance){
this.message = message;
this.importance = importance;
datetime = new Date();
try
{
throw new Exception("Who called me?");
}
catch( Exception e )
{
callingClass= e.getStackTrace()[3].getClassName()+":"+e.getStackTrace()[3].getMethodName();
}
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
public String toString() {
//return "\t\t\t\t[" + dateFormat.format(datetime) + "] " + " " + getImportance(importance) +" "+callingClass+ " :\n"+ message;
return getImportanceColor(importance)+"[" + dateFormat.format(datetime) + "] " + " " + getImportance(importance) +" "+callingClass+ " :\n\t"+ message+getColorNormalizationCode();
}
}
/**
* Gets the color normalization code.
*
* @return the color normalization code
*/
private String getColorNormalizationCode(){
if(isUseColoredPrints())
return "\033[39m";
return "";
}
/**
* Checks if is use colored prints.
*
* @return true, if is use colored prints
*/
public static boolean isUseColoredPrints() {
return instance().useColoredPrints;
}
/**
* Sets the use colored prints.
*
* @param useColoredPrints the new use colored prints
*/
public static void setUseColoredPrints(boolean useColoredPrints) {
instance().useColoredPrints = useColoredPrints;
}
/**
* Checks if is printing.
*
* @return true, if is printing
*/
public static boolean isPrinting() {
// Auto-generated method stub
return instance().systemprint;
}
public static void error(Throwable ex) {
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
ex.printStackTrace(pw);
String sStackTrace = sw.toString(); // stack trace as a string
Log.error(sStackTrace);
}
public static void setFile(File logfile) {
instance().systemprint=true;
instance.log=null;
if(instance.logFileThread!=null) {
instance.logFileThread.interrupt();
try {
instance.logFileThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
instance.log=logfile;
instance.logFileThread=new Thread(()->{
instance.incomingErr = new ByteList();
OutputStream streamErr = new OutputStream() {
@Override
public void write(int b) throws IOException {
instance.incomingErr.add(b);
}
};
instance.incomingOut = new ByteList();
OutputStream streamOut = new OutputStream() {
@Override
public void write(int b) throws IOException {
instance.incomingOut.add(b);
}
};
System.setOut(new PrintStream(streamOut));
System.setErr(new PrintStream(streamErr));
setOutStream(new PrintStream(streamErr));
while (instance.log!=null) {
ThreadUtil.wait(150);
if (instance.incomingOut.size() > 0)
try {
String text = instance.incomingOut.asString();
instance.incomingOut.clear();
if (text != null && text.length() > 0){
//Files.writeString(logfile.toPath(), text, StandardCharsets.UTF_8, StandardOpenOption.APPEND); // java 11+
Files.write(logfile.toPath(), text.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
mirrorStream.println(text);
}
text = null;
} catch (Exception e) {
e.printStackTrace();
}
if (instance.incomingErr.size() > 0)
try {
String text = instance.incomingErr.asString();
instance.incomingErr.clear();
if (text != null && text.length() > 0){
//Files.writeString(logfile.toPath(), text, StandardCharsets.UTF_8, StandardOpenOption.APPEND); // java 11+
Files.write(logfile.toPath(), text.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
errStream.println(text);
}
text = null;
} catch (Exception e) {
e.printStackTrace();
}
}
});
instance.logFileThread.start();
}
public static void flush() {
setOutStream(outStream);
System.setOut(outStream);
System.setErr(outStream);
instance.log=null;
while(instance.incomingOut.size() > 0 ||instance.incomingErr.size() > 0 ) {
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
try {
instance.logFileThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static PrintStream getMirrorStream() {
return mirrorStream;
}
public static void setMirrorStream(PrintStream mirrorStream) {
Log.mirrorStream = mirrorStream;
}
}