Skip to content

Commit 267fac7

Browse files
committed
Don't change log format here
Don't change log format here
1 parent b9a5be7 commit 267fac7

1 file changed

Lines changed: 57 additions & 64 deletions

File tree

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

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

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

4345
/** The Constant INFO. */
4446
public static final int INFO = 0;
@@ -61,7 +63,7 @@ public class Log {
6163

6264
/** The date format. */
6365
private DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss:SS");
64-
66+
6567
/** The minprintlevel. */
6668
private int minprintlevel = WARNING;
6769

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

7476
/** The out stream. */
7577
private static PrintStream outStream = System.out;
76-
7778
/** The out stream. */
7879
private static PrintStream errStream = System.err;
79-
8080
/** The out stream. */
8181
private static PrintStream mirrorStream = System.out;
82-
8382
/** The use colored prints. */
84-
private boolean useColoredPrints = false;
83+
private boolean useColoredPrints=false;
8584

8685
private Thread logFileThread = null;
8786

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

9089
private ByteList incomingErr;
9190
private ByteList incomingOut;
9291

93-
private String lastCallingClass = "";
94-
9592
/**
9693
* Instantiates a new log.
9794
*/
@@ -162,16 +159,23 @@ public static void add(String message) {
162159
*/
163160
private void add(String message, int importance) {
164161

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

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

177181
/**
@@ -186,11 +190,11 @@ public static void enableSystemPrint(boolean systemprint) {
186190
/**
187191
* Enable printing of debug output.
188192
*/
193+
189194
public static void enableDebugPrint() {
190195
Log.enableSystemPrint(true);
191196
Log.setMinimumPrintLevel(DEBUG);
192197
}
193-
194198
public static void disablePrint() {
195199
Log.enableSystemPrint(false);
196200
}
@@ -200,6 +204,7 @@ public static void disablePrint() {
200204
*
201205
* @param flag the flag
202206
*/
207+
203208
public static void enableDebugPrint(boolean flag) {
204209
Log.enableSystemPrint(flag);
205210
Log.setMinimumPrintLevel(DEBUG);
@@ -208,6 +213,7 @@ public static void enableDebugPrint(boolean flag) {
208213
/**
209214
* Enable printing of debug output.
210215
*/
216+
211217
public static void enableInfoPrint() {
212218
Log.enableSystemPrint(true);
213219
Log.setMinimumPrintLevel(INFO);
@@ -216,6 +222,7 @@ public static void enableInfoPrint() {
216222
/**
217223
* Enable printing of debug output.
218224
*/
225+
219226
public static void enableWarningPrint() {
220227
Log.enableSystemPrint(true);
221228
Log.setMinimumPrintLevel(WARNING);
@@ -224,11 +231,13 @@ public static void enableWarningPrint() {
224231
/**
225232
* Enable printing of debug output.
226233
*/
234+
227235
public static void enableErrorPrint() {
228236
Log.enableSystemPrint(true);
229237
Log.setMinimumPrintLevel(ERROR);
230238
}
231239

240+
232241
/**
233242
* Set the minimum level of importance to dsplay.
234243
* Messages below this wont be displayed.
@@ -254,7 +263,7 @@ public static int getMinimumPrintLevel() {
254263
* @return The log instance.
255264
*/
256265
public static Log instance() {
257-
if (instance == null) {
266+
if(instance == null) {
258267
instance = new Log();
259268
}
260269
return instance;
@@ -281,15 +290,14 @@ public String getImportance(int importance) {
281290
return "Log";
282291
}
283292
}
284-
285293
/**
286294
* Get a string describing the given importance level.
287295
*
288296
* @param importance The given importance level.
289297
* @return the importance
290298
*/
291299
public String getImportanceColor(int importance) {
292-
if (isUseColoredPrints()) {
300+
if(isUseColoredPrints()){
293301
switch(importance) {
294302
case INFO:
295303
return "\033[92m";// green
@@ -306,7 +314,9 @@ public String getImportanceColor(int importance) {
306314
}
307315
return "";
308316
}
317+
309318

319+
310320
/**
311321
* Get the current output PrintStream.
312322
*
@@ -360,41 +370,26 @@ public Message(String message, int importance) {
360370
* @param message the message
361371
* @param importance the importance
362372
*/
363-
public void init(String message, int importance) {
373+
public void init(String message, int importance){
364374
this.message = message;
365375
this.importance = importance;
366376
datetime = new Date();
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-
}
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+
}
376385
}
377386

378387
/* (non-Javadoc)
379388
* @see java.lang.Object#toString()
380389
*/
381390
public String toString() {
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;
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();
398393
}
399394
}
400395

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

410407
/**
411408
* Checks if is use colored prints.
412409
*
413410
* @return true, if is use colored prints
414411
*/
415-
public static boolean isUseColoredPrints() {
412+
public static boolean isUseColoredPrints() {
416413
return instance().useColoredPrints;
417414
}
418415

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

446443
public static void setFile(File logfile) {
447-
instance().systemprint = true;
448-
instance.log = null;
449-
if (instance.logFileThread != null) {
444+
instance().systemprint=true;
445+
instance.log=null;
446+
if(instance.logFileThread!=null) {
450447
instance.logFileThread.interrupt();
451448
try {
452449
instance.logFileThread.join();
@@ -456,19 +453,19 @@ public static void setFile(File logfile) {
456453
}
457454
}
458455

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

463-
OutputStream streamErr = new OutputStream() {
460+
OutputStream streamErr = new OutputStream() {
464461
@Override
465462
public void write(int b) throws IOException {
466463
instance.incomingErr.add(b);
467464
}
468465
};
469466
instance.incomingOut = new ByteList();
470467

471-
OutputStream streamOut = new OutputStream() {
468+
OutputStream streamOut = new OutputStream() {
472469
@Override
473470
public void write(int b) throws IOException {
474471
instance.incomingOut.add(b);
@@ -477,19 +474,17 @@ public void write(int b) throws IOException {
477474
System.setOut(new PrintStream(streamOut));
478475
System.setErr(new PrintStream(streamErr));
479476
setOutStream(new PrintStream(streamErr));
480-
481-
while (instance.log != null) {
477+
while (instance.log!=null) {
482478
try {
483479
Thread.sleep(149);
484480
} catch (InterruptedException e) {
485481
return;
486482
}
487-
488483
if (instance.incomingOut.size() > 0)
489484
try {
490485
String text = instance.incomingOut.asString();
491486
instance.incomingOut.clear();
492-
if ((text != null) && (text.length() > 0)) {
487+
if (text != null && text.length() > 0){
493488
//Files.writeString(logfile.toPath(), text, StandardCharsets.UTF_8, StandardOpenOption.APPEND); // java 11+
494489
Files.write(logfile.toPath(), text.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
495490
mirrorStream.println(text);
@@ -498,12 +493,11 @@ public void write(int b) throws IOException {
498493
} catch (Exception e) {
499494
e.printStackTrace();
500495
}
501-
502496
if (instance.incomingErr.size() > 0)
503497
try {
504498
String text = instance.incomingErr.asString();
505499
instance.incomingErr.clear();
506-
if ((text != null) && (text.length() > 0)) {
500+
if (text != null && text.length() > 0){
507501
//Files.writeString(logfile.toPath(), text, StandardCharsets.UTF_8, StandardOpenOption.APPEND); // java 11+
508502
Files.write(logfile.toPath(), text.getBytes(StandardCharsets.UTF_8), StandardOpenOption.APPEND);
509503
errStream.println(text);
@@ -516,13 +510,12 @@ public void write(int b) throws IOException {
516510
});
517511
instance.logFileThread.start();
518512
}
519-
520513
public static void flush() {
521514
setOutStream(outStream);
522515
System.setOut(outStream);
523516
System.setErr(outStream);
524-
instance.log = null;
525-
while ((instance.incomingOut.size() > 0) || (instance.incomingErr.size() > 0 )) {
517+
instance.log=null;
518+
while(instance.incomingOut.size() > 0 ||instance.incomingErr.size() > 0 ) {
526519
try {
527520
Thread.sleep(10);
528521
} catch (InterruptedException e) {

0 commit comments

Comments
 (0)