(Eclipse RCP)如何将输出重定向到Console View?

时间:2010-01-24 16:15:27

标签: eclipse eclipse-rcp

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

由于

2 个答案:

答案 0 :(得分:1)

所以问题How to write a hyperlink to an eclipse console from a pluginwriting to the eclipse console提供了重定向到控制台的示例。

博文Displaying the console in your RCP application

alt text

我们仍然需要创建OuputStream并打开新的Console,或将控制台的MessageStreamstdout adn stderr相关联(例如我的想法) previous answer

答案 1 :(得分:1)

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

截图:

enter image description here