2012-04-18 77 views
0
Display disp = Display.getCurrent();  
    disp.asyncExec(new Runnable() { 
     public void run(){ 
      try { 

       PipedOutputStream pos = new PipedOutputStream(); 
       System.setErr(new PrintStream(pos, true)); 
       System.setOut(new PrintStream(pos, true)); 

       PipedInputStream pis = new PipedInputStream(pos); 
       BufferedReader reader = new BufferedReader(new InputStreamReader(pis)); 

       String line = null; 

       while (true){ 

        line = reader.readLine(); // != null) 

        console.append(line); 
        System.out.println("moo" + line); 
        parent.layout(true,true); 
       } 

      } catch (IOException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 
     } 
    }); 

我想捕獲System.err的內容在我的應用程序的調試窗口中打印出來。我可以運行上面的代碼,但System.err中的內容沒有顯示在我的應用程序中,它只是被打印到控制檯,任何想法爲什麼?將stdout重定向到swt。

回答

0

我不熟悉swt編程。但我記得重定向stdout的方法是將輸出流設置爲實現PrintStream的自定義對象,就像您在調用System.setErr時一樣。此對象的print方法將知道如何更新圖形用戶界面

0

問題是您無限地回顯至System.out。你可能想要做的是將舊的System.out存儲在run()的開頭,並且在你的讀取循環中回顯那裏,而不是新的System.out

PrintStream oldOut = System.out; 

// ... set out and err 

while(true) { 
    ... 
    oldOut.println("moo" + line); 
} 

編輯:

而且,你要開始一個新的線程,而不是調用asyncExec(),它運行在EDT,因此將導致您的GUI掛起。

+0

好的,有道理,但是我做了這個改變後,輸出仍然打印到控制檯。也許它與在單獨的線程中調用setOut有關? – 2012-04-18 11:51:54

相關問題