0
Display disp = Display.getCurrent();    
    disp.asyncExec(new Runnable() {
        public void run(){
            try {

                PipedOutputStream pos = new PipedOutputStream();
                System.setErr( new PrintStream(pos, true) );
                System.setOut( new PrintStream(pos, true) );

                PipedInputStream pis = new PipedInputStream( pos );
                BufferedReader reader = new BufferedReader( new InputStreamReader(pis) );

                String line = null;

                while (true){

                    line = reader.readLine(); // != null)

                    console.append(line);
                    System.out.println("moo" + line);
                    parent.layout(true,true);
                }

            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
        }
    });

我正在尝试捕获 System.err 的内容以在我的应用程序的调试窗口中打印出来。我可以运行上面的代码,但是 System.err 的内容没有显示在我的应用程序中,它只是被打印到控制台,有什么想法吗?

4

2 回答 2

0

问题是你无限地回响到System.out. 您可能想要做的是将旧的存储System.out在 的开头run(),并在此处回显,而不是System.out在读取循环中的新的。

PrintStream oldOut = System.out;

// ... set out and err

while(true) {
    ...
    oldOut.println("moo" + line);
}

编辑:

此外,您想启动一个新线程而不是调用asyncExec(),它在 EDT 上运行,因此会导致您的 GUI 挂起。

于 2012-04-18T11:01:36.977 回答
0

我不熟悉 swt 编程。但我记得重定向标准输出的方法是设置输出流,就像你在调用 System.setErr 时所做的那样,为实现 PrintStream 的自定义对象。该对象的print方法将知道如何更新 GUI

于 2012-04-18T10:52:10.820 回答