| /** |
| ******************************************************************************** |
| * Copyright (c) 2020 Robert Bosch GmbH and others. |
| * |
| * This program and the accompanying materials are made |
| * available under the terms of the Eclipse Public License 2.0 |
| * which is available at https://www.eclipse.org/legal/epl-2.0/ |
| * |
| * SPDX-License-Identifier: EPL-2.0 |
| * |
| * Contributors: |
| * Robert Bosch GmbH - initial API and implementation |
| ******************************************************************************** |
| */ |
| package org.eclipse.app4mc.transformation.log4j.configuration; |
| |
| import org.apache.log4j.ConsoleAppender; |
| import org.apache.log4j.Layout; |
| import org.apache.log4j.spi.LoggingEvent; |
| import org.eclipse.ui.IViewPart; |
| 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.MessageConsole; |
| import org.eclipse.ui.console.MessageConsoleStream; |
| |
| public class CustomConsoleAppender extends ConsoleAppender { |
| |
| public CustomConsoleAppender() { |
| } |
| |
| public CustomConsoleAppender(Layout layout) { |
| super(layout, SYSTEM_OUT); |
| } |
| |
| public CustomConsoleAppender(Layout layout, String target) { |
| super.setLayout(layout); |
| super.setTarget(target); |
| super.activateOptions(); |
| } |
| |
| private MessageConsole myConsole; |
| |
| @Override |
| public void append(LoggingEvent event) { |
| if (logMessageOnConsole(event) == false) { |
| myConsole = findConsole("ASF SLG"); |
| logMessageOnConsole(event); |
| } |
| } |
| @Override |
| public synchronized void doAppend(LoggingEvent event) { |
| |
| if (logMessageOnConsole(event) == false) { |
| myConsole = findConsole("ASF SLG"); |
| logMessageOnConsole(event); |
| } |
| } |
| |
| private boolean logMessageOnConsole(LoggingEvent event) { |
| if (myConsole != null) { |
| MessageConsoleStream out = myConsole.newMessageStream(); |
| out.println(event.getMessage().toString()); |
| return true; |
| } |
| return false; |
| } |
| |
| |
| private MessageConsole findConsole(String name) { |
| |
| openConsoleView(); |
| ConsolePlugin plugin = ConsolePlugin.getDefault(); |
| if (plugin != null) { |
| IConsoleManager conMan = plugin.getConsoleManager(); |
| if (conMan != null) { |
| IConsole[] existing = conMan.getConsoles(); |
| for (int i = 0; i < existing.length; i++) |
| if (name.equals(existing[i].getName())) |
| return (MessageConsole) existing[i]; |
| // no console found, so create a new one |
| MessageConsole myConsole = new MessageConsole(name, null); |
| conMan.addConsoles(new IConsole[] { myConsole }); |
| |
| return myConsole; |
| |
| } |
| |
| } |
| return null; |
| } |
| |
| private void openConsoleView() { |
| try { |
| IViewPart findView = PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage() |
| .findView(IConsoleConstants.ID_CONSOLE_VIEW); |
| if (findView == null) { |
| PlatformUI.getWorkbench().getActiveWorkbenchWindow().getActivePage() |
| .showView(IConsoleConstants.ID_CONSOLE_VIEW); |
| } |
| |
| } catch (PartInitException e) { |
| // TODO:Currently not logging any message for this exception |
| } |
| } |
| } |