【问题标题】:(Eclipse RCP) How to redirect the output to Console View?(Eclipse RCP) 如何将输出重定向到控制台视图?
【发布时间】:2016-12-06 18:42:32
【问题描述】:

我有两个查看器,一个有一个用于用户输入的文本,另一个查看器是 Eclipse 的内置控制台视图。我会根据用户的输入运行一个java程序,并希望在ConsoleView中显示日志信息。有谁知道如何将输出重定向到控制台视图?

谢谢

【问题讨论】:

    标签: eclipse 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

    想法仍然是创建一个OuputStream 并打开一个新的Console,或者将控制台的MessageStream 关联到stdoutstderr(就像我的previous answer

    【讨论】:

    • 我按照你的方法,不幸的是日志消息会显示在控制台中,但是 System.out 的输出不能显示在控制台中。
    【解决方案2】:

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

    截图:

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-07-11
      • 1970-01-01
      • 2010-09-20
      • 2013-11-14
      • 1970-01-01
      • 2019-07-25
      • 1970-01-01
      • 2013-12-07
      相关资源
      最近更新 更多