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