LogConsole.java 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. package com.leanwo.management.console;
  2. import java.awt.BorderLayout;
  3. import java.awt.event.ActionEvent;
  4. import java.awt.event.ActionListener;
  5. import java.io.IOException;
  6. import java.io.InputStream;
  7. import java.io.PipedInputStream;
  8. import javax.swing.JButton;
  9. import javax.swing.JPanel;
  10. import javax.swing.JScrollPane;
  11. import javax.swing.JTextArea;
  12. public class LogConsole extends JPanel{
  13. private JTextArea textArea;
  14. private JButton clearButton;
  15. private PipedInputStream outPipedInputStream;
  16. private PipedInputStream errorPipedInputStream;
  17. private boolean quit;
  18. private Thread outReaderThread;
  19. private Thread errorReaderThread;
  20. /** 字符串最大的个数 */
  21. private static final int MAX_CHARACTER_COUNT = 100000;
  22. public LogConsole(PipedInputStream outPipedInputStream, PipedInputStream errorPipedInputStream) {
  23. this.outPipedInputStream = outPipedInputStream;
  24. this.errorPipedInputStream = errorPipedInputStream;
  25. initData();
  26. initView();
  27. }
  28. public void setPipedInputStream(PipedInputStream outPipedInputStream, PipedInputStream errorPipedInputStream) {
  29. this.outPipedInputStream = outPipedInputStream;
  30. this.errorPipedInputStream = errorPipedInputStream;
  31. }
  32. /**
  33. * 初始化数据
  34. */
  35. private void initData() {
  36. // signals the Threads that they should exit
  37. quit=false;
  38. // Starting two seperate threads to read from the PipedInputStreams
  39. outReaderThread=new Thread(outRunnable);
  40. outReaderThread.setDaemon(true);
  41. outReaderThread.start();
  42. errorReaderThread=new Thread(errorRunnable);
  43. errorReaderThread.setDaemon(true);
  44. errorReaderThread.start();
  45. }
  46. /**
  47. * 初始化界面
  48. */
  49. private void initView() {
  50. textArea=new JTextArea();
  51. textArea.setEditable(false);
  52. clearButton=new JButton("清空");
  53. setLayout(new BorderLayout());
  54. add(new JScrollPane(textArea),BorderLayout.CENTER);
  55. add(clearButton,BorderLayout.SOUTH);
  56. clearButton.addActionListener(new ActionListener() {
  57. @Override
  58. public void actionPerformed(ActionEvent e) {
  59. textArea.setText(null);
  60. }
  61. });
  62. }
  63. private Runnable outRunnable = new Runnable() {
  64. @Override
  65. public synchronized void run() {
  66. try {
  67. while (!quit) {
  68. try {
  69. this.wait(100);
  70. } catch (InterruptedException ie) {
  71. }
  72. if (outPipedInputStream != null && outPipedInputStream.available() != 0) {
  73. String input = readLine(outPipedInputStream);
  74. textArea.append(input);
  75. }
  76. }
  77. } catch (Exception e) {
  78. textArea.append("\nConsole reports an Internal error.");
  79. textArea.append("The error is: " + e);
  80. }
  81. }
  82. };
  83. private Runnable errorRunnable = new Runnable() {
  84. @Override
  85. public synchronized void run() {
  86. try {
  87. while (!quit) {
  88. try {
  89. this.wait(100);
  90. } catch (InterruptedException ie) {
  91. }
  92. if (errorPipedInputStream != null && errorPipedInputStream.available() != 0) {
  93. String input = readLine(errorPipedInputStream);
  94. textArea.append(input);
  95. }
  96. }
  97. } catch (Exception e) {
  98. textArea.append("\nConsole reports an Internal error.");
  99. textArea.append("The error is: " + e);
  100. }
  101. }
  102. };
  103. private String readLine(InputStream inputStream) throws IOException {
  104. String input = "";
  105. do {
  106. int available = inputStream.available();
  107. if (available == 0)
  108. break;
  109. byte b[] = new byte[available];
  110. inputStream.read(b);
  111. input = input + new String(b, 0, b.length, "GBK");
  112. } while (!input.endsWith("\n") && !input.endsWith("\r\n") && !quit);
  113. return input;
  114. }
  115. /**
  116. * 保留最多的字符串
  117. */
  118. public void reserveMaxCharacter() {
  119. String text = textArea.getText();
  120. if(text.length() > MAX_CHARACTER_COUNT) {
  121. text = text.substring(text.length() - MAX_CHARACTER_COUNT *2/3);
  122. textArea.setText(text);
  123. }
  124. }
  125. }