Skip to content

Commit 2aea952

Browse files
committed
Change log file format, categorize same caller, add line spacing
Change log file format, categorize same caller, add line spacing
1 parent 6944180 commit 2aea952

1 file changed

Lines changed: 51 additions & 43 deletions

File tree

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

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

Lines changed: 51 additions & 43 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

@@ -80,15 +78,16 @@ public class Log {
8078
/** The out stream. */
8179
private static PrintStream mirrorStream = System.out;
8280
/** The use colored prints. */
83-
private boolean useColoredPrints=false;
81+
private boolean useColoredPrints = false;
8482

8583
private Thread logFileThread = null;
8684

87-
private File log=null;
85+
private File log = null;
8886

8987
private ByteList incomingErr;
9088
private ByteList incomingOut;
9189

90+
private String lastCallingClass = "";
9291
/**
9392
* Instantiates a new log.
9493
*/
@@ -159,23 +158,20 @@ public static void add(String message) {
159158
*/
160159
private void add(String message, int importance) {
161160

162-
if( importance < minprintlevel) {
161+
if (importance < minprintlevel) {
163162
return;
164163
}
165-
if(m==null)
164+
if (m == null)
166165
m = new Message(message, importance);
167166
else{
168167
m.init(message, importance);
169168
}
170169
//messages.add(m);
171170

172171

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

181177
/**
@@ -263,7 +259,7 @@ public static int getMinimumPrintLevel() {
263259
* @return The log instance.
264260
*/
265261
public static Log instance() {
266-
if(instance == null) {
262+
if (instance == null) {
267263
instance = new Log();
268264
}
269265
return instance;
@@ -297,7 +293,7 @@ public String getImportance(int importance) {
297293
* @return the importance
298294
*/
299295
public String getImportanceColor(int importance) {
300-
if(isUseColoredPrints()){
296+
if (isUseColoredPrints()) {
301297
switch(importance) {
302298
case INFO:
303299
return "\033[92m";// green
@@ -370,26 +366,40 @@ public Message(String message, int importance) {
370366
* @param message the message
371367
* @param importance the importance
372368
*/
373-
public void init(String message, int importance){
369+
public void init(String message, int importance) {
374370
this.message = message;
375371
this.importance = importance;
376372
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-
}
373+
374+
try
375+
{
376+
throw new Exception("Who called me?");
377+
}
378+
catch( Exception e )
379+
{
380+
callingClass = e.getStackTrace()[3].getClassName() + ":" + e.getStackTrace()[3].getMethodName();
381+
}
385382
}
386383

387384
/* (non-Javadoc)
388385
* @see java.lang.Object#toString()
389386
*/
390387
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();
388+
389+
// First logfile line
390+
if (lastCallingClass.isEmpty()) {
391+
lastCallingClass = "\n[" + dateFormat.format(datetime) + "] ======== Log file opened ========";
392+
dateFormat = new SimpleDateFormat("HH:mm:ss.SS");
393+
394+
return lastCallingClass.toString();
395+
}
396+
397+
if (lastCallingClass.equals(getImportance(importance) + " " + callingClass))
398+
return getImportanceColor(importance) + " [" + dateFormat.format(datetime) + "] " + message + getColorNormalizationCode();
399+
400+
lastCallingClass = getImportance(importance) + " " + callingClass;
401+
402+
return "\n" + getImportanceColor(importance) + lastCallingClass + ":\n [" + dateFormat.format(datetime) + "] " + message + getColorNormalizationCode();
393403
}
394404
}
395405

@@ -398,18 +408,16 @@ public String toString() {
398408
*
399409
* @return the color normalization code
400410
*/
401-
private String getColorNormalizationCode(){
402-
if(isUseColoredPrints())
403-
return "\033[39m";
404-
return "";
411+
private String getColorNormalizationCode() {
412+
return isUseColoredPrints() ? "\033[39m" : "";
405413
}
406414

407415
/**
408416
* Checks if is use colored prints.
409417
*
410418
* @return true, if is use colored prints
411419
*/
412-
public static boolean isUseColoredPrints() {
420+
public static boolean isUseColoredPrints() {
413421
return instance().useColoredPrints;
414422
}
415423

@@ -441,9 +449,9 @@ public static void error(Throwable ex) {
441449
}
442450

443451
public static void setFile(File logfile) {
444-
instance().systemprint=true;
445-
instance.log=null;
446-
if(instance.logFileThread!=null) {
452+
instance().systemprint = true;
453+
instance.log = null;
454+
if (instance.logFileThread != null) {
447455
instance.logFileThread.interrupt();
448456
try {
449457
instance.logFileThread.join();
@@ -453,19 +461,19 @@ public static void setFile(File logfile) {
453461
}
454462
}
455463

456-
instance.log=logfile;
457-
instance.logFileThread=new Thread(()->{
464+
instance.log = logfile;
465+
instance.logFileThread = new Thread(()->{
458466
instance.incomingErr = new ByteList();
459467

460-
OutputStream streamErr = new OutputStream() {
468+
OutputStream streamErr = new OutputStream() {
461469
@Override
462470
public void write(int b) throws IOException {
463471
instance.incomingErr.add(b);
464472
}
465473
};
466474
instance.incomingOut = new ByteList();
467475

468-
OutputStream streamOut = new OutputStream() {
476+
OutputStream streamOut = new OutputStream() {
469477
@Override
470478
public void write(int b) throws IOException {
471479
instance.incomingOut.add(b);
@@ -474,7 +482,7 @@ public void write(int b) throws IOException {
474482
System.setOut(new PrintStream(streamOut));
475483
System.setErr(new PrintStream(streamErr));
476484
setOutStream(new PrintStream(streamErr));
477-
while (instance.log!=null) {
485+
while (instance.log != null) {
478486
try {
479487
Thread.sleep(149);
480488
} catch (InterruptedException e) {
@@ -484,7 +492,7 @@ public void write(int b) throws IOException {
484492
try {
485493
String text = instance.incomingOut.asString();
486494
instance.incomingOut.clear();
487-
if (text != null && text.length() > 0){
495+
if ((text != null) && (text.length() > 0)) {
488496
//Files.writeString(logfile.toPath(), text, StandardCharsets.UTF_8, StandardOpenOption.APPEND); // java 11+
489497
Files.write(logfile.toPath(), text.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
490498
mirrorStream.println(text);
@@ -497,7 +505,7 @@ public void write(int b) throws IOException {
497505
try {
498506
String text = instance.incomingErr.asString();
499507
instance.incomingErr.clear();
500-
if (text != null && text.length() > 0){
508+
if ((text != null) && (text.length() > 0)) {
501509
//Files.writeString(logfile.toPath(), text, StandardCharsets.UTF_8, StandardOpenOption.APPEND); // java 11+
502510
Files.write(logfile.toPath(), text.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
503511
errStream.println(text);
@@ -514,8 +522,8 @@ public static void flush() {
514522
setOutStream(outStream);
515523
System.setOut(outStream);
516524
System.setErr(outStream);
517-
instance.log=null;
518-
while(instance.incomingOut.size() > 0 ||instance.incomingErr.size() > 0 ) {
525+
instance.log = null;
526+
while((instance.incomingOut.size() > 0) || (instance.incomingErr.size() > 0 )) {
519527
try {
520528
Thread.sleep(10);
521529
} catch (InterruptedException e) {

0 commit comments

Comments
 (0)