瀏覽代碼

swing log update

shjung 2 年之前
父節點
當前提交
b2df51d832

+ 0 - 1
conf/vds-comm-server.pid

@@ -1 +0,0 @@
-24904

+ 43 - 19
src/main/java/com/its/vds/ui/JTextAreaOutputStream.java

@@ -1,7 +1,6 @@
 package com.its.vds.ui;
 
 import javax.swing.*;
-import javax.swing.text.BadLocationException;
 import java.io.IOException;
 import java.io.OutputStream;
 
@@ -9,6 +8,7 @@ public class JTextAreaOutputStream extends OutputStream {
 
     private final JTextArea logArea;
     public static boolean isLoggingPause = false;
+    private final StringBuilder sb = new StringBuilder();
 
     public JTextAreaOutputStream(JTextArea logArea) {
         if (logArea == null)
@@ -16,23 +16,23 @@ public class JTextAreaOutputStream extends OutputStream {
         this.logArea = logArea;
     }
 
-    public synchronized void logWrite(String text) {
-        Runnable  runnable = new Runnable() {
-            public void run() {
-                logArea.append(text);
-                if (logArea.getDocument().getLength() >
-                        50000) {
-                    try {
-                        logArea.getDocument().remove(0, 5000);
-                    } catch (BadLocationException e) {
-                        //log.error("Can't clean log", e);
-                    }
-                }
-                logArea.setCaretPosition(logArea.getDocument().getLength());
-            }
-        };
-        SwingUtilities.invokeLater(runnable);
-    }
+//    public synchronized void logWrite(String text) {
+//        Runnable  runnable = new Runnable() {
+//            public void run() {
+//                logArea.append(text);
+//                if (logArea.getDocument().getLength() >
+//                        50000) {
+//                    try {
+//                        logArea.getDocument().remove(0, 5000);
+//                    } catch (BadLocationException e) {
+//                        //log.error("Can't clean log", e);
+//                    }
+//                }
+//                logArea.setCaretPosition(logArea.getDocument().getLength());
+//            }
+//        };
+//        SwingUtilities.invokeLater(runnable);
+//    }
 
     @Override
     public void write(byte[] buffer, int offset, int length) throws IOException
@@ -61,6 +61,30 @@ public class JTextAreaOutputStream extends OutputStream {
 
     @Override
     public void write(int b) throws IOException {
-        write (new byte [] {(byte)b}, 0, 1);
+//        if (b == '\r')
+//            return;
+
+        if (b == '\n') {
+            final String text = sb.toString() + "\n";
+            SwingUtilities.invokeLater(new Runnable ()
+            {
+                @Override
+                public void run()
+                {
+                    synchronized (logArea) {
+                        if (logArea.getLineCount() > 2000) {
+                            logArea.setText(null);
+                        }
+                        logArea.append(text);
+                        logArea.setCaretPosition(logArea.getDocument().getLength());
+                        sb.setLength(0);
+                    }
+                }
+            });
+        } else {
+            sb.append((char) b);
+        }
+
+        //write (new byte [] {(byte)b}, 0, 1);
     }
 }

+ 1 - 1
src/main/resources/logback-spring.xml

@@ -34,7 +34,7 @@
     <property name="LOG_PATTERN_CONSOLE"     value="[%d{HH:mm:ss.SSS}] [%5level] %msg%n"/>
 
     <appender name="CONSOLE" class="ch.qos.logback.core.ConsoleAppender">
-        <withJansi>true</withJansi>
+<!--        <withJansi>true</withJansi>-->
         <encoder class="ch.qos.logback.classic.encoder.PatternLayoutEncoder">
             <charset>${LOG_CHARSET}</charset>
             <pattern>${LOG_PATTERN_CONSOLE}</pattern>