简体   繁体   English

Eclipse RCP将其他插件的日志语句添加到日志插件

[英]Eclipse RCP add log statements from other plug-ins to log plug-in

I would like to show some logging information in my Eclipse RCP application. 我想在我的Eclipse RCP应用程序中显示一些日志记录信息。 For this I created a Eclipse view in a separate Plug-in (singleton). 为此,我在单独的插件(单个)中创建了一个Eclipse视图。 Here is my code that I got so far: 这是到目前为止我得到的代码:

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);
    } 
}

And the configuration: 并配置:

<?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>

Now, I would like to log messages from other plug-ins to the StyledText instance. 现在,我想将消息从其他插件记录到StyledText实例。 What is the most convenient way to do this? 什么是最便捷的方式做到这一点?

I tried this approach and it's convenient but really really slow. 我尝试了这种方法 ,它很方便,但确实很慢。 I really appreciate your help :) Thank you! 非常感谢您的帮助:)谢谢!

是有关登录OSGI的大量文章。

Here is the post-construct method of my console part. 这是控制台部分的post-construct方法。 Basically it sets a new System.out object and listens to it. 基本上,它会设置一个新的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);
        }
    });
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM