Search code examples
javaeclipse-plugineclipse-rcpeclipse-pde

Directing all output to MessageConsole


Currently I am using MessageConsole's, MessageConsoleStream to write to console.


My question is, Is it possible to do some configuration such that everything I write to output stream is redirected to MessageConsoleStream.


Example, if I issue the following it should automatically write to the configured console.

  1. System.out.println("txt")
  2. e.printStackTrace()
  3. In case Unchecked exception occurs (should automatically go to the console).

Solution

  • You can redirect all out and err data, System.out.println("txt") and e.printStackTrace() are printed to the out stream while "Unchecked excepctions" are printed to the err stream.
    In this example System.out.println("txt") would write txt to the log.txt file.

    OutputStream out = new FileOutputStream("log.txt");
    PrintStream stream = new PrintStream(out);
    System.setOut(stream);
    System.setErr(stream);
    

    In your case you could do something like this:

    MessageConsoleStream out = ...;
    PrintStream stream = new PrintStream(out);
    System.setOut(stream);
    System.setErr(stream);