]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/ErrorLogger.java
Fixed multiple issues causing dangling references to discarded queries
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / ErrorLogger.java
1 /*******************************************************************************
2  * Copyright (c) 2007, 2018 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  *     VTT Technical Research Centre of Finland - initial API and implementation
11  *******************************************************************************/
12 package org.simantics.utils.ui;
13
14 import org.eclipse.core.runtime.IStatus;
15 import org.eclipse.core.runtime.Plugin;
16 import org.eclipse.core.runtime.Status;
17 import org.simantics.utils.ui.internal.Activator;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
20
21 /**
22  * ErrorLogger sends error messages to "Error log" view.
23  * 
24  * @author Toni Kalajainen
25  * @author Jani Simomaa
26  */
27 public class ErrorLogger {
28
29     private static final Logger LOGGER = LoggerFactory.getLogger(ErrorLogger.class);
30     private final Plugin plugin;
31     private String pluginID;
32
33
34     public ErrorLogger(Plugin plugin) {
35         this.plugin = plugin;
36     }
37
38     private String getPluginID() {
39         if (pluginID==null)
40             pluginID = plugin.getBundle().getSymbolicName();
41         return pluginID;
42     }
43
44     /**
45      * Log a warning event.
46      * 
47      * @param message message of the error
48      * @param exception the exception, or <code>null</code>
49      */
50     public void logWarning(String message, Throwable exception) {
51         log(IStatus.WARNING, IStatus.OK, message, exception);
52         LOGGER.warn(message, exception);
53     }
54
55     /**
56      * Log a message.
57      * 
58      * @param message message of the error
59      * @param exception the exception, or <code>null</code>
60      */
61     public void logMessage(String message, Throwable exception) {
62         log(IStatus.INFO, IStatus.OK, message, exception);
63         LOGGER.info(message, exception);
64     }
65
66     /**
67      * Log an error event.
68      * 
69      * @param message message of the error
70      * @param exception the exception, or <code>null</code>
71      */
72     public void logError(String message, Throwable exception) {
73         // Errors are much more useful with a stack trace!
74         if (exception == null) {
75             exception = new RuntimeException();
76         }
77         log(IStatus.ERROR, IStatus.OK, message, exception);
78         LOGGER.error(message, exception);
79     }
80
81     public void log(int severity, int code, String message, Throwable exception) {
82         IStatus status = new Status(severity, getPluginID(), code, message, exception);
83         log(status);
84     }
85
86     public void log(IStatus status) {
87         plugin.getLog().log(status);
88     }
89
90     public static ErrorLogger getDefault() {
91         return Activator.getDefault().getErrorLogger();
92     }
93
94     public static void defaultLogError(String message, Throwable exception) {
95         getDefault().logError(message, exception);
96     }
97
98     public static void defaultLogError(Throwable exception) {
99         getDefault().logError(getUIFriendErrorMessage(exception), exception);
100     }
101
102     public static void defaultLogWarning(String message, Throwable exception) {
103         getDefault().logWarning(message, exception);
104     }
105
106     public static void defaultLogWarning(Throwable exception) {
107         getDefault().logWarning(getUIFriendErrorMessage(exception), exception);
108     }
109
110     public static void defaultLog(IStatus status) {
111         getDefault().log(status);
112     }
113
114     /**
115      * This call makes verbose error message that is suitable for
116      * UI Dialgos. The full cause hierarchy is shown with the tree.
117      * In this case verbose includes class names.
118      * 
119      * @param e exception
120      * @return message
121      */
122     public static String getUIFriendErrorMessageVerbose(Throwable e) {
123         String result = "";
124         Throwable pe = null;
125         while (e!=null && pe!=e) {
126             result += e.getClass().getName()+": "+e.getMessage()+"\n";
127             pe = e;
128             e = e.getCause();
129         }
130         return result;
131     }
132
133     /**
134      * This call makes error message that is suitable for
135      * UI Dialogs. The full cause hierarchy is shown with the tree.
136      * In this case verbose includes class names.
137      * 
138      * @param e exception
139      * @return message
140      */
141     public static String getUIFriendErrorMessage(Throwable e) {
142         String result = "";
143         Throwable pe = null;
144         while (e!=null && pe!=e) {
145             result += e.getMessage()+"\n";
146             pe = e;
147             e = e.getCause();
148         }
149         return result;
150     }
151
152 }