【问题标题】:Eclipse RCP add log statements from other plug-ins to log plug-inEclipse RCP 将其他插件的日志语句添加到日志插件
【发布时间】:2012-07-18 21:37:43
【问题描述】:

我想在我的 Eclipse RCP 应用程序中显示一些日志信息。为此,我在一个单独的插件(单例)中创建了一个 Eclipse 视图。这是我到目前为止得到的代码:

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,它很方便,但真的很慢。非常感谢您的帮助:) 谢谢!

【问题讨论】:

    标签: eclipse logging view rcp


    【解决方案1】:

    Here 是一系列关于登录 OSGI 的精彩文章。

    【讨论】:

    • 感谢您的链接。我看了看,我不确定这是否符合我的需要。我只是想在某个视图中为用户显示一些信息。我个人认为传统的日志记录将带有级别的消息作为开发者信息附加到日志文件中。
    • 我自己没用过,所以链接。您可以使用 org.eclipse.core.runtime.ILogListener 接口连接到 Eclipse 日志记录。
    • 我检查了这个,我的错误日志视图包含一些我不想向用户显示的消息。难道没有一种简单的方法可以将String 传递给某个视图实例吗?
    【解决方案2】:

    这是我的控制台部分的构建后方法。基本上它设置一个新的 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);
            }
        });
    }
    

    【讨论】:

    • ps 我正在使用 Eclipse e4。
    猜你喜欢
    • 2017-08-28
    • 1970-01-01
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 2014-01-20
    • 2011-03-10
    • 2017-02-28
    • 1970-01-01
    相关资源
    最近更新 更多