2010-01-24 117 views
2

我有兩個查看器,一個有用於用戶輸入的文本,另一個查看器是Eclipse的built_in控制檯視圖。我將根據用戶的輸入運行一個Java程序,並希望在ConsoleView中顯示日誌信息。有誰知道我如何將輸出重定向到控制檯視圖?(Eclipse RCP)如何將輸出重定向到控制檯視圖?

感謝

回答

1

SO問題How to write a hyperlink to an eclipse console from a pluginwriting to the eclipse console給例如重定向到控制檯的。

博客文章Displaying the console in your RCP application

alt text

的想法仍然是創建一個OuputStream並打開一個新Console,或控制檯的MessageStream關聯到stdout ADN stderr(就像我previous answer

+0

我按照你的方法,但不幸的是,日誌信息將在控制檯中顯示,但是從System.out的輸出不能在控制檯顯示。 – zjffdu 2010-01-25 13:24:34

0

重定向輸出RCP控制檯:

import java.io.IOException; 
import java.io.OutputStream; 
import java.io.PrintStream; 

import javax.annotation.PostConstruct; 

import org.eclipse.e4.ui.di.Focus; 
import org.eclipse.swt.SWT; 
import org.eclipse.swt.events.DisposeEvent; 
import org.eclipse.swt.events.DisposeListener; 
import org.eclipse.swt.widgets.Composite; 
import org.eclipse.swt.widgets.Display; 
import org.eclipse.swt.widgets.Text; 

public class ConsoleView { 
    private Text text; 

    @PostConstruct 
    public void createPartControl(Composite parent) { 
     text = new Text(parent, 
       SWT.READ_ONLY | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL); 

     OutputStream out = new OutputStream() { 
      StringBuffer buffer = new StringBuffer(); 

      @Override 
      public void write(final int b) throws IOException { 
       if (text.isDisposed()) 
        return; 
       buffer.append((char) b); 
      } 

      @Override 
      public void write(byte[] b, int off, int len) throws IOException { 
       super.write(b, off, len); 
       flush(); 
      } 

      @Override 
      public void flush() throws IOException { 
       final String newText = buffer.toString(); 
       Display.getDefault().asyncExec(new Runnable() { 
        public void run() { 
         text.append(newText); 
        } 
       }); 
       buffer = new StringBuffer(); 
      } 
     }; 

     System.setOut(new PrintStream(out)); 
     final PrintStream oldOut = System.out; 

     text.addDisposeListener(new DisposeListener() { 
      public void widgetDisposed(DisposeEvent e) { 
       System.setOut(oldOut); 
      } 
     }); 
    } 

    @Focus 
    public void setFocus() { 
     text.setFocus(); 
    } 
} 

的截圖:

enter image description here

相關問題