1414 ******************************************************************************/
1515package com .neuronrobotics .sdk .common ;
1616
17+ import java .io .File ;
18+ import java .io .IOException ;
19+ import java .io .OutputStream ;
1720import java .io .PrintStream ;
1821import java .io .PrintWriter ;
1922import java .io .StringWriter ;
23+ import java .nio .charset .StandardCharsets ;
24+ import java .nio .file .Files ;
25+ import java .nio .file .StandardOpenOption ;
2026import java .text .DateFormat ;
2127import java .text .SimpleDateFormat ;
2228import java .util .Date ;
2329
2430import com .neuronrobotics .sdk .config .SDKBuildInfo ;
31+ import com .neuronrobotics .sdk .util .ThreadUtil ;
2532// Auto-generated Javadoc
2633/**
2734 * This class is the Logging Class for the NRsdk.
@@ -69,12 +76,13 @@ public class Log {
6976 /** The out stream. */
7077 private static PrintStream outStream = System .out ;
7178
72- /** The err stream. */
73- private static PrintStream errStream = System .err ;
74-
7579 /** The use colored prints. */
7680 private boolean useColoredPrints =false ;
7781
82+ private Thread logFileThread = null ;
83+
84+ private File log =null ;
85+
7886
7987 /**
8088 * Instantiates a new log.
@@ -155,15 +163,10 @@ private void add(String message, int importance) {
155163 m .init (message , importance );
156164 }
157165 //messages.add(m);
166+
158167
159- if (isPrinting () && importance >= minprintlevel && systemprint ) {
160- errStream .println (m );
161- if (errStream != System .err )
162- System .err .println (m );
163- }
164-
165- if (debugprint && systemprint ) {
166- outStream .println ("# " + message );
168+ if ( systemprint ) {
169+ outStream .println (m .toString ());
167170 if (outStream != System .out )
168171 System .err .println (m );
169172 }
@@ -188,6 +191,9 @@ public static void enableDebugPrint() {
188191 Log .enableSystemPrint (true );
189192 Log .setMinimumPrintLevel (DEBUG );
190193 }
194+ public static void disablePrint () {
195+ Log .enableSystemPrint (false );
196+ }
191197
192198 /**
193199 * Enable printing of debug output.
@@ -305,23 +311,7 @@ public String getImportanceColor(int importance) {
305311 return "" ;
306312 }
307313
308- /**
309- * get the current error PrintStream.
310- *
311- * @return the current Error PrintStream
312- */
313- public static PrintStream getErrStream () {
314- return errStream ;
315- }
316-
317- /**
318- * set the current error PrintStream.
319- *
320- * @param newerrStream the new err stream
321- */
322- public static void setErrStream (PrintStream newerrStream ) {
323- errStream = newerrStream ;
324- }
314+
325315
326316 /**
327317 * Get the current output PrintStream.
@@ -395,7 +385,7 @@ public void init(String message, int importance){
395385 */
396386 public String toString () {
397387 //return "\t\t\t\t[" + dateFormat.format(datetime) + "] " + " " + getImportance(importance) +" "+callingClass+ " :\n"+ message;
398- return getImportanceColor (importance )+"\t \t \t \t [" + dateFormat .format (datetime ) + "] " + " " + getImportance (importance ) +" " +callingClass + " :\n " + message +getColorNormalizationCode ();
388+ return getImportanceColor (importance )+"[" + dateFormat .format (datetime ) + "] " + " " + getImportance (importance ) +" " +callingClass + " :\t \t \t \t " + message +getColorNormalizationCode ();
399389 }
400390 }
401391
@@ -445,5 +435,47 @@ public static void error(Throwable ex) {
445435 String sStackTrace = sw .toString (); // stack trace as a string
446436 Log .error (sStackTrace );
447437 }
438+
439+ public static void setFile (File logfile ) {
440+ instance ().systemprint =true ;
441+ instance .log =null ;
442+ if (instance .logFileThread !=null ) {
443+ instance .logFileThread .interrupt ();
444+ try {
445+ instance .logFileThread .join ();
446+ } catch (InterruptedException e ) {
447+ // TODO Auto-generated catch block
448+ e .printStackTrace ();
449+ }
450+ }
451+
452+ instance .log =logfile ;
453+ instance .logFileThread =new Thread (()->{
454+ ByteList incoming = new ByteList ();
455+ OutputStream stream = new OutputStream () {
456+ @ Override
457+ public void write (int b ) throws IOException {
458+ incoming .add (b );
459+ }
460+ };
461+ setOutStream (new PrintStream (stream ));
462+ while (instance .log !=null ) {
463+ ThreadUtil .wait (150 );
464+ if (incoming .size () > 0 )
465+ try {
466+ String text = incoming .asString ();
467+ incoming .clear ();
468+ if (text != null && text .length () > 0 ){
469+ Files .writeString (instance .log .toPath (), text , StandardCharsets .UTF_8 ,
470+ StandardOpenOption .CREATE , StandardOpenOption .APPEND );
471+ }
472+ text = null ;
473+ } catch (Exception e ) {
474+ e .printStackTrace ();
475+ }
476+ }
477+ });
478+ instance .logFileThread .start ();
479+ }
448480
449481}
0 commit comments