import java.io.OutputStreamWriter;
import java.io.PrintStream;
import org.apache.log4j.ConsoleAppender;
import org.apache.log4j.Logger;
import org.apache.log4j.PatternLayout;
import org.eclipse.core.commands.AbstractHandler;
import org.eclipse.core.commands.ExecutionEvent;
import org.eclipse.core.commands.ExecutionException;
import org.eclipse.core.commands.IHandler;
import org.eclipse.swt.graphics.Color;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.IWorkbenchPage;
import org.eclipse.ui.PartInitException;
import org.eclipse.ui.PlatformUI;
import org.eclipse.ui.console.ConsolePlugin;
import org.eclipse.ui.console.IConsole;
import org.eclipse.ui.console.IConsoleConstants;
import org.eclipse.ui.console.IConsoleManager;
import org.eclipse.ui.console.IConsoleView;
import org.eclipse.ui.console.MessageConsole;
import org.eclipse.ui.console.MessageConsoleStream;
public class ZeigeConsole extends AbstractHandler implements IHandler{
public Object execute(ExecutionEvent event) throws ExecutionException {
Logger logger = Logger.getLogger(StatusServerClient.class);
ConsolePlugin plugin = ConsolePlugin.getDefault();
IConsoleManager conMan = plugin.getConsoleManager();
String systemConsoleName = "System.out";
IConsole[] existing = conMan.getConsoles();
for (int i = 0; i < existing.length; i++)
if (systemConsoleName.equals(existing[i].getName()))
return (MessageConsole) existing[i];
MessageConsole systemConsole = new MessageConsole(systemConsoleName,
null);
conMan.addConsoles(new IConsole[] { systemConsole });
MessageConsoleStream defaultMessageOutputStream = systemConsole.newMessageStream();
PrintStream defaultMessagePrintStream = new PrintStream(defaultMessageOutputStream);
// MessageConsoleStream warningMessageOutputStream = systemConsole.newMessageStream();
// warningMessageOutputStream.setColor(new Color(Display.getCurrent(), 0,
// 0, 255));
// PrintStream warningMessagePrintStream = new PrintStream(warningMessageOutputStream);
MessageConsoleStream errorMessageOutputStream = systemConsole.newMessageStream();
errorMessageOutputStream.setColor(new Color(Display.getCurrent(), 255,
0, 0));
PrintStream errorMessagePrintStream = new PrintStream(errorMessageOutputStream);
System.setOut(defaultMessagePrintStream);
System.setErr(errorMessagePrintStream);
//Den ConsoleAppender auf die neue Console lenken
ConsoleAppender systemOut = new ConsoleAppender();
systemOut.setName("System.Out");
systemOut.setWriter(new OutputStreamWriter(System.out));
PatternLayout outLayout = new PatternLayout();
outLayout.setConversionPattern("%d{[yyyy-MM-dd / HH:mm:ss:SSS]} - %-5p - %m ---> (%C.%M line:%L) %n");
systemOut.setLayout(outLayout);
logger.addAppender(systemOut);
//-----------------------------------------------
try {
IWorkbenchPage page = PlatformUI.getWorkbench()
.getActiveWorkbenchWindow().getActivePage();
String id = IConsoleConstants.ID_CONSOLE_VIEW;
IConsoleView view = (IConsoleView) page.showView(id);
view.display(systemConsole);
} catch (PartInitException e) {
e.printStackTrace();
}
return null;
}
}