]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/ErrorLogger.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.browsing.ui.common / src / org / simantics / browsing / ui / common / ErrorLogger.java
1 /*******************************************************************************\r
2  * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
3  * in Industry THTH ry.\r
4  * All rights reserved. This program and the accompanying materials\r
5  * are made available under the terms of the Eclipse Public License v1.0\r
6  * which accompanies this distribution, and is available at\r
7  * http://www.eclipse.org/legal/epl-v10.html\r
8  *\r
9  * Contributors:\r
10  *     VTT Technical Research Centre of Finland - initial API and implementation\r
11  *******************************************************************************/\r
12 package org.simantics.browsing.ui.common;\r
13 \r
14 import org.eclipse.core.runtime.IStatus;\r
15 import org.eclipse.core.runtime.Plugin;\r
16 import org.eclipse.core.runtime.Status;\r
17 import org.simantics.db.layer0.exception.InvalidVariableException;\r
18 \r
19 \r
20 /**\r
21  * ErrorLogger sends error messages to "Error log" view.\r
22  * \r
23  * @author Toni Kalajainen\r
24  */\r
25 public class ErrorLogger {\r
26 \r
27     public static final boolean ECHO = true;\r
28 \r
29     private final Plugin plugin;\r
30     private String pluginID;\r
31 \r
32     public ErrorLogger(Plugin plugin) {\r
33         this.plugin = plugin;\r
34     }\r
35 \r
36     private String getPluginID() {\r
37         if (pluginID==null)\r
38             pluginID = plugin.getBundle().getSymbolicName();\r
39         return pluginID;\r
40     }\r
41 \r
42     /**\r
43      * Log a warning event.\r
44      * \r
45      * @param message message of the error\r
46      * @param exception the exception, or <code>null</code>\r
47      */\r
48     public void logWarning(String message, Throwable exception) {\r
49         log(IStatus.WARNING, IStatus.OK, message, exception);\r
50     }\r
51 \r
52     /**\r
53      * Log a message.\r
54      * \r
55      * @param message message of the error\r
56      * @param exception the exception, or <code>null</code>\r
57      */\r
58     public void logMessage(String message, Throwable exception) {\r
59         log(IStatus.INFO, IStatus.OK, message, exception);\r
60     }\r
61 \r
62     /**\r
63      * Log an error event.\r
64      * \r
65      * @param message message of the error\r
66      * @param exception the exception, or <code>null</code>\r
67      */\r
68     public void logError(String message, Throwable exception) {\r
69         // Errors are much more useful with a stack trace!\r
70         if (exception == null) {\r
71             exception = new RuntimeException();\r
72         }\r
73         log(IStatus.ERROR, IStatus.OK, message, exception);\r
74     }\r
75 \r
76     public void log(int severity, int code, String message, Throwable exception) {\r
77         IStatus status = new Status(severity, getPluginID(), code, message, exception);\r
78         log(status);\r
79     }\r
80 \r
81     public void log(IStatus status) {\r
82         plugin.getLog().log(status);\r
83     }\r
84 \r
85     public static ErrorLogger getDefault() {\r
86         return Activator.getDefault().getErrorLogger();\r
87     }\r
88 \r
89     public static void defaultLogError(String message, Throwable exception) {\r
90         if (filterException(exception))\r
91             return;\r
92         getDefault().logError(message, exception);\r
93         if(ECHO && exception != null) exception.printStackTrace();\r
94     }\r
95 \r
96     public static void defaultLogError(Throwable exception) {\r
97         if (filterException(exception))\r
98             return;\r
99         getDefault().logError(getUIFriendErrorMessage(exception), exception);\r
100         if(ECHO && exception != null) exception.printStackTrace();\r
101     }\r
102 \r
103     public static void defaultLogWarning(String message, Throwable exception) {\r
104         if (filterException(exception))\r
105             return;\r
106         getDefault().logWarning(message, exception);\r
107         if(ECHO && exception != null) exception.printStackTrace();\r
108     }\r
109 \r
110     public static void defaultLogWarning(Throwable exception) {\r
111         if (filterException(exception))\r
112             return;\r
113         getDefault().logWarning(getUIFriendErrorMessage(exception), exception);\r
114         if(ECHO && exception != null) exception.printStackTrace();\r
115     }\r
116 \r
117     public static void defaultLog(IStatus status) {\r
118         getDefault().log(status);\r
119         if(ECHO) System.out.println(status);\r
120     }\r
121 \r
122     /**\r
123      * This call makes verbose error message that is suitable for\r
124      * UI Dialgos. The full cause hierarchy is shown with the tree.\r
125      * In this case verbose includes class names.\r
126      * \r
127      * @param e exception\r
128      * @return message\r
129      */\r
130     public static String getUIFriendErrorMessageVerbose(Throwable e) {\r
131         String result = "";\r
132         Throwable pe = null;\r
133         while (e!=null && pe!=e) {\r
134             result += e.getClass().getName()+": "+e.getMessage()+"\n";\r
135             pe = e;\r
136             e = e.getCause();\r
137         }\r
138         return result;\r
139     }\r
140 \r
141     /**\r
142      * This call makes error message that is suitable for\r
143      * UI Dialogs. The full cause hierarchy is shown with the tree.\r
144      * In this case verbose includes class names.\r
145      * \r
146      * @param e exception\r
147      * @return message\r
148      */\r
149     public static String getUIFriendErrorMessage(Throwable e) {\r
150         String result = "";\r
151         Throwable pe = null;\r
152         while (e!=null && pe!=e) {\r
153             result += e.getMessage()+"\n";\r
154             pe = e;\r
155             e = e.getCause();\r
156         }\r
157         return result;\r
158     }\r
159 \r
160     public static boolean filterException(Throwable t) {\r
161         if (t instanceof InvalidVariableException)\r
162             return true;\r
163         return false;\r
164     }\r
165 \r
166 }\r