]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.platform.ui/src/org/simantics/platform/ui/internal/Activator.java
Fixed Simantics Console activation and formatted message output
[simantics/platform.git] / bundles / org.simantics.platform.ui / src / org / simantics / platform / ui / internal / Activator.java
1 /*******************************************************************************
2  * Copyright (c) 2019 Association for Decentralized Information Management
3  * in Industry THTH ry.
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  *     Semantum Oy - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.platform.ui.internal;
13
14 import java.io.PrintWriter;
15 import java.io.StringWriter;
16
17 import org.eclipse.ui.PlatformUI;
18 import org.eclipse.ui.plugin.AbstractUIPlugin;
19 import org.osgi.framework.BundleContext;
20 import org.osgi.framework.ServiceReference;
21 import org.simantics.UnhandledExceptionService;
22 import org.simantics.platform.ui.SimanticsConsole;
23 import org.slf4j.Logger;
24 import org.slf4j.LoggerFactory;
25
26 public class Activator extends AbstractUIPlugin {
27
28     private BundleContext context;
29
30     public Activator() {
31     }
32
33     @Override
34     public void start(BundleContext context) throws Exception {
35         super.start(context);
36         this.context = context;
37
38         if (PlatformUI.isWorkbenchRunning())
39             PlatformUI.getWorkbench().getDisplay().asyncExec(this::initConsole);
40     }
41
42     @Override
43     public void stop(BundleContext context) throws Exception {
44         this.context = null;
45         super.stop(context);
46     }
47
48     private void initConsole() {
49         if (PlatformUI.getWorkbench().getDisplay().isDisposed())
50              return;
51
52         SimanticsConsole console = SimanticsConsole.findConsole();
53         if (console != null) {
54             ServiceReference<?> ref = context.getServiceReference(UnhandledExceptionService.class.getName());
55             if (ref != null) {
56                 UnhandledExceptionService service = (UnhandledExceptionService) context.getService(ref);
57                 service.registerHandler(t -> {
58                     StringWriter sw = new StringWriter();
59                     PrintWriter pw = new PrintWriter(sw);
60                     t.printStackTrace(pw);
61                     console.write(sw.toString());
62                 });
63             }
64             ConsoleAppender ca = new ConsoleAppender(console);
65             ch.qos.logback.classic.Logger logbackLogger =
66                     (ch.qos.logback.classic.Logger) LoggerFactory.getLogger(Logger.ROOT_LOGGER_NAME);
67             logbackLogger.addAppender(ca);
68             ca.start();
69         }
70     }
71
72 }