2012-07-19 34 views
0

我想在Eclipse RCP應用程序中顯示一些日誌記錄信息。爲此,我在一個單獨的插件(單例)中創建了一個Eclipse視圖。這裏是我的代碼,我走到這一步:Eclipse RCP從其他插件添加日誌語句到日誌插件

public class Console extends ViewPart {  
    private StyledText text; 

    public Console() {} 

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

    @Override 
    public void setFocus() { 
     this.text.setFocus(); 
    } 

    public void log(String message){ 
     this.text.append(message); 
    } 
} 

而且配置:

<?xml version="1.0" encoding="UTF-8"?> 
<?eclipse version="3.4"?> 
<plugin> 
    <extension 
     point="org.eclipse.ui.views"> 
    <view 
      category="org.myApp.ui.category.myApp" 
      class="org.myApp.ui.log.Console" 
      icon="icons/log.png" 
      id="org.myApp.ui.view.console" 
      name="Console" 
      restorable="true"> 
    </view> 
    <category 
      id="org.myApp.ui.category.myApp" 
      name="myApp"> 
    </category> 
    </extension> 
</plugin> 

現在,我想記錄來自其他插件的消息到StyledText實例。什麼是最簡單的方法來做到這一點?

我試過這個approach,它很方便,但真的很慢。我非常感謝你的幫助:)謝謝!

回答

0

Here是一篇關於登錄OSGI的系列文章。

+0

謝謝你的鏈接。我看了一下,我不確定這是否符合我的需求。我只是想在某個視圖中爲用戶顯示一些信息。我個人認爲傳統日誌記錄是將具有關卡的消息作爲開發者信息附加到日誌文件中。 – kon 2012-07-20 08:59:50

+0

我沒有使用它自己,因此鏈接。您可以使用org.eclipse.core.runtime.ILogListener接口掛接到eclipse日誌記錄。 – jopa 2012-07-20 12:55:40

+0

我檢查了這一點,我的錯誤日誌視圖包含一些我不想向用戶顯示的消息。沒有一種簡單的方法將'String'傳遞給某個視圖實例嗎? – kon 2012-07-20 14:01:26

0

這是我的控制檯部分的後構建方法。基本上它設置一個新的System.out對象並監聽它。

@PostConstruct 
public void postConstruct(Composite parent) { 

    System.out.println("[Console Part] ** Post Construct **"); 

    txtConsole = new Text(parent, SWT.READ_ONLY | SWT.MULTI | SWT.V_SCROLL | SWT.H_SCROLL | SWT.BORDER); 

    out = new OutputStream() { 

     @Override 
     public void write(int b) throws IOException { 
      if(txtConsole.isDisposed()) 
       return; 
      txtConsole.append(String.valueOf((char) b)); 
     } 
    }; 

    // keep the old output stream 
    final PrintStream oldOut = System.out; 

    System.setOut(new PrintStream(out)); 
    txtConsole.addDisposeListener(new DisposeListener() { 
     public void widgetDisposed(DisposeEvent e) { 
      System.setOut(oldOut); 
     } 
    }); 
} 
+0

** ps **我正在使用Eclipse e4。 – 2014-01-15 17:42:17