Skip to content

Commit 2956ed0

Browse files
committed
Log file format update, add spacing
Log file format update, add spacing
1 parent 6944180 commit 2956ed0

1 file changed

Lines changed: 64 additions & 57 deletions

File tree

  • src/main/java/com/neuronrobotics/sdk/common

src/main/java/com/neuronrobotics/sdk/common/Log.java

Lines changed: 64 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,9 @@
3636
*
3737
*/
3838
public class Log {
39-
40-
41-
39+
4240
/** The Constant LOG. */
43-
public static final int LOG =-1;
41+
public static final int LOG = -1;
4442

4543
/** The Constant INFO. */
4644
public static final int INFO = 0;
@@ -63,7 +61,7 @@ public class Log {
6361

6462
/** The date format. */
6563
private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SS");
66-
64+
6765
/** The minprintlevel. */
6866
private int minprintlevel = WARNING;
6967

@@ -75,20 +73,25 @@ public class Log {
7573

7674
/** The out stream. */
7775
private static PrintStream outStream = System.out;
76+
7877
/** The out stream. */
7978
private static PrintStream errStream = System.err;
79+
8080
/** The out stream. */
8181
private static PrintStream mirrorStream = System.out;
82+
8283
/** The use colored prints. */
83-
private boolean useColoredPrints=false;
84+
private boolean useColoredPrints = false;
8485

8586
private Thread logFileThread = null;
8687

87-
private File log=null;
88+
private File log = null;
8889

8990
private ByteList incomingErr;
9091
private ByteList incomingOut;
9192

93+
private String lastCallingClass = "";
94+
9295
/**
9396
* Instantiates a new log.
9497
*/
@@ -159,23 +162,16 @@ public static void add(String message) {
159162
*/
160163
private void add(String message, int importance) {
161164

162-
if( importance < minprintlevel) {
165+
if (importance < minprintlevel)
163166
return;
164-
}
165-
if(m==null)
167+
168+
if (m == null)
166169
m = new Message(message, importance);
167-
else{
170+
else
168171
m.init(message, importance);
169-
}
170-
//messages.add(m);
171172

172-
173-
if( systemprint) {
173+
if (systemprint)
174174
outStream.println(m.toString());
175-
}
176-
177-
178-
179175
}
180176

181177
/**
@@ -190,11 +186,11 @@ public static void enableSystemPrint(boolean systemprint) {
190186
/**
191187
* Enable printing of debug output.
192188
*/
193-
194189
public static void enableDebugPrint() {
195190
Log.enableSystemPrint(true);
196191
Log.setMinimumPrintLevel(DEBUG);
197192
}
193+
198194
public static void disablePrint() {
199195
Log.enableSystemPrint(false);
200196
}
@@ -204,7 +200,6 @@ public static void disablePrint() {
204200
*
205201
* @param flag the flag
206202
*/
207-
208203
public static void enableDebugPrint(boolean flag) {
209204
Log.enableSystemPrint(flag);
210205
Log.setMinimumPrintLevel(DEBUG);
@@ -213,7 +208,6 @@ public static void enableDebugPrint(boolean flag) {
213208
/**
214209
* Enable printing of debug output.
215210
*/
216-
217211
public static void enableInfoPrint() {
218212
Log.enableSystemPrint(true);
219213
Log.setMinimumPrintLevel(INFO);
@@ -222,7 +216,6 @@ public static void enableInfoPrint() {
222216
/**
223217
* Enable printing of debug output.
224218
*/
225-
226219
public static void enableWarningPrint() {
227220
Log.enableSystemPrint(true);
228221
Log.setMinimumPrintLevel(WARNING);
@@ -231,13 +224,11 @@ public static void enableWarningPrint() {
231224
/**
232225
* Enable printing of debug output.
233226
*/
234-
235227
public static void enableErrorPrint() {
236228
Log.enableSystemPrint(true);
237229
Log.setMinimumPrintLevel(ERROR);
238230
}
239231

240-
241232
/**
242233
* Set the minimum level of importance to dsplay.
243234
* Messages below this wont be displayed.
@@ -263,7 +254,7 @@ public static int getMinimumPrintLevel() {
263254
* @return The log instance.
264255
*/
265256
public static Log instance() {
266-
if(instance == null) {
257+
if (instance == null) {
267258
instance = new Log();
268259
}
269260
return instance;
@@ -290,14 +281,15 @@ public String getImportance(int importance) {
290281
return "Log";
291282
}
292283
}
284+
293285
/**
294286
* Get a string describing the given importance level.
295287
*
296288
* @param importance The given importance level.
297289
* @return the importance
298290
*/
299291
public String getImportanceColor(int importance) {
300-
if(isUseColoredPrints()){
292+
if (isUseColoredPrints()) {
301293
switch(importance) {
302294
case INFO:
303295
return "\033[92m";// green
@@ -314,9 +306,7 @@ public String getImportanceColor(int importance) {
314306
}
315307
return "";
316308
}
317-
318309

319-
320310
/**
321311
* Get the current output PrintStream.
322312
*
@@ -370,26 +360,41 @@ public Message(String message, int importance) {
370360
* @param message the message
371361
* @param importance the importance
372362
*/
373-
public void init(String message, int importance){
363+
public void init(String message, int importance) {
374364
this.message = message;
375365
this.importance = importance;
376366
datetime = new Date();
377-
try
378-
{
379-
throw new Exception("Who called me?");
380-
}
381-
catch( Exception e )
382-
{
383-
callingClass= e.getStackTrace()[3].getClassName()+":"+e.getStackTrace()[3].getMethodName();
384-
}
367+
368+
try
369+
{
370+
throw new Exception("Who called me?");
371+
}
372+
catch( Exception e )
373+
{
374+
callingClass = e.getStackTrace()[3].getClassName() + ":" + e.getStackTrace()[3].getMethodName();
375+
}
385376
}
386377

387378
/* (non-Javadoc)
388379
* @see java.lang.Object#toString()
389380
*/
390381
public String toString() {
391-
//return "\t\t\t\t[" + dateFormat.format(datetime) + "] " + " " + getImportance(importance) +" "+callingClass+ " :\n"+ message;
392-
return getImportanceColor(importance)+"[" + dateFormat.format(datetime) + "] " + " " + getImportance(importance) +" "+callingClass+ " :\n\t"+ message+getColorNormalizationCode();
382+
383+
// First logfile line
384+
if (lastCallingClass.isEmpty()) {
385+
386+
lastCallingClass = "\n======== [" + dateFormat.format(datetime) + "] " + message + " ========";
387+
dateFormat = new SimpleDateFormat("HH:mm:ss.SS");
388+
389+
return lastCallingClass;
390+
}
391+
392+
if (lastCallingClass.equals(getImportance(importance) + " " + callingClass))
393+
return getImportanceColor(importance) + " [" + dateFormat.format(datetime) + "] " + getColorNormalizationCode() + message;
394+
395+
lastCallingClass = getImportance(importance) + " " + callingClass;
396+
397+
return "\n" + getImportanceColor(importance) + lastCallingClass + ":\n [" + dateFormat.format(datetime) + "] " + getColorNormalizationCode() + message;
393398
}
394399
}
395400

@@ -398,18 +403,16 @@ public String toString() {
398403
*
399404
* @return the color normalization code
400405
*/
401-
private String getColorNormalizationCode(){
402-
if(isUseColoredPrints())
403-
return "\033[39m";
404-
return "";
406+
private String getColorNormalizationCode() {
407+
return isUseColoredPrints() ? "\033[39m" : "";
405408
}
406409

407410
/**
408411
* Checks if is use colored prints.
409412
*
410413
* @return true, if is use colored prints
411414
*/
412-
public static boolean isUseColoredPrints() {
415+
public static boolean isUseColoredPrints() {
413416
return instance().useColoredPrints;
414417
}
415418

@@ -441,9 +444,9 @@ public static void error(Throwable ex) {
441444
}
442445

443446
public static void setFile(File logfile) {
444-
instance().systemprint=true;
445-
instance.log=null;
446-
if(instance.logFileThread!=null) {
447+
instance().systemprint = true;
448+
instance.log = null;
449+
if (instance.logFileThread != null) {
447450
instance.logFileThread.interrupt();
448451
try {
449452
instance.logFileThread.join();
@@ -453,19 +456,19 @@ public static void setFile(File logfile) {
453456
}
454457
}
455458

456-
instance.log=logfile;
457-
instance.logFileThread=new Thread(()->{
459+
instance.log = logfile;
460+
instance.logFileThread = new Thread(()->{
458461
instance.incomingErr = new ByteList();
459462

460-
OutputStream streamErr = new OutputStream() {
463+
OutputStream streamErr = new OutputStream() {
461464
@Override
462465
public void write(int b) throws IOException {
463466
instance.incomingErr.add(b);
464467
}
465468
};
466469
instance.incomingOut = new ByteList();
467470

468-
OutputStream streamOut = new OutputStream() {
471+
OutputStream streamOut = new OutputStream() {
469472
@Override
470473
public void write(int b) throws IOException {
471474
instance.incomingOut.add(b);
@@ -474,17 +477,19 @@ public void write(int b) throws IOException {
474477
System.setOut(new PrintStream(streamOut));
475478
System.setErr(new PrintStream(streamErr));
476479
setOutStream(new PrintStream(streamErr));
477-
while (instance.log!=null) {
480+
481+
while (instance.log != null) {
478482
try {
479483
Thread.sleep(149);
480484
} catch (InterruptedException e) {
481485
return;
482486
}
487+
483488
if (instance.incomingOut.size() > 0)
484489
try {
485490
String text = instance.incomingOut.asString();
486491
instance.incomingOut.clear();
487-
if (text != null && text.length() > 0){
492+
if ((text != null) && (text.length() > 0)) {
488493
//Files.writeString(logfile.toPath(), text, StandardCharsets.UTF_8, StandardOpenOption.APPEND); // java 11+
489494
Files.write(logfile.toPath(), text.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
490495
mirrorStream.println(text);
@@ -493,11 +498,12 @@ public void write(int b) throws IOException {
493498
} catch (Exception e) {
494499
e.printStackTrace();
495500
}
501+
496502
if (instance.incomingErr.size() > 0)
497503
try {
498504
String text = instance.incomingErr.asString();
499505
instance.incomingErr.clear();
500-
if (text != null && text.length() > 0){
506+
if ((text != null) && (text.length() > 0)) {
501507
//Files.writeString(logfile.toPath(), text, StandardCharsets.UTF_8, StandardOpenOption.APPEND); // java 11+
502508
Files.write(logfile.toPath(), text.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
503509
errStream.println(text);
@@ -510,12 +516,13 @@ public void write(int b) throws IOException {
510516
});
511517
instance.logFileThread.start();
512518
}
519+
513520
public static void flush() {
514521
setOutStream(outStream);
515522
System.setOut(outStream);
516523
System.setErr(outStream);
517-
instance.log=null;
518-
while(instance.incomingOut.size() > 0 ||instance.incomingErr.size() > 0 ) {
524+
instance.log = null;
525+
while ((instance.incomingOut.size() > 0) || (instance.incomingErr.size() > 0 )) {
519526
try {
520527
Thread.sleep(10);
521528
} catch (InterruptedException e) {

0 commit comments

Comments
 (0)