]> gerrit.simantics Code Review - simantics/platform.git/blob - bundles/org.simantics.utils.ui/src/org/simantics/utils/ui/ErrorLogger.java
Migrated source code from Simantics SVN
[simantics/platform.git] / bundles / org.simantics.utils.ui / src / org / simantics / utils / ui / 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.utils.ui;\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.utils.ui.internal.Activator;\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 \r
33     public ErrorLogger(Plugin plugin) {\r
34         this.plugin = plugin;\r
35     }\r
36 \r
37     private String getPluginID() {\r
38         if (pluginID==null)\r
39             pluginID = plugin.getBundle().getSymbolicName();\r
40         return pluginID;\r
41     }\r
42 \r
43     /**\r
44      * Log a warning event.\r
45      * \r
46      * @param message message of the error\r
47      * @param exception the exception, or <code>null</code>\r
48      */\r
49     public void logWarning(String message, Throwable exception) {\r
50         log(IStatus.WARNING, IStatus.OK, message, exception);\r
51     }\r
52 \r
53     /**\r
54      * Log a message.\r
55      * \r
56      * @param message message of the error\r
57      * @param exception the exception, or <code>null</code>\r
58      */\r
59     public void logMessage(String message, Throwable exception) {\r
60         log(IStatus.INFO, IStatus.OK, message, exception);\r
61     }\r
62 \r
63     /**\r
64      * Log an error event.\r
65      * \r
66      * @param message message of the error\r
67      * @param exception the exception, or <code>null</code>\r
68      */\r
69     public void logError(String message, Throwable exception) {\r
70         // Errors are much more useful with a stack trace!\r
71         if (exception == null) {\r
72             exception = new RuntimeException();\r
73         }\r
74         log(IStatus.ERROR, IStatus.OK, message, exception);\r
75     }\r
76 \r
77     public void log(int severity, int code, String message, Throwable exception) {\r
78         IStatus status = new Status(severity, getPluginID(), code, message, exception);\r
79         log(status);\r
80     }\r
81 \r
82     public void log(IStatus status) {\r
83         plugin.getLog().log(status);\r
84     }\r
85 \r
86     public static ErrorLogger getDefault() {\r
87         return Activator.getDefault().getErrorLogger();\r
88     }\r
89 \r
90     public static void defaultLogError(String message, Throwable exception) {\r
91         getDefault().logError(message, exception);\r
92         if(ECHO && exception != null) exception.printStackTrace();\r
93     }\r
94 \r
95     public static void defaultLogError(Throwable exception) {\r
96         getDefault().logError(getUIFriendErrorMessage(exception), exception);\r
97         if(ECHO && exception != null) exception.printStackTrace();\r
98     }\r
99 \r
100     public static void defaultLogWarning(String message, Throwable exception) {\r
101         getDefault().logWarning(message, exception);\r
102         if(ECHO && exception != null) exception.printStackTrace();\r
103     }\r
104 \r
105     public static void defaultLogWarning(Throwable exception) {\r
106         getDefault().logWarning(getUIFriendErrorMessage(exception), exception);\r
107         if(ECHO && exception != null) exception.printStackTrace();\r
108     }\r
109 \r
110     public static void defaultLog(IStatus status) {\r
111         getDefault().log(status);\r
112         if(ECHO) System.out.println(status);\r
113     }\r
114 \r
115     /**\r
116      * This call makes verbose error message that is suitable for\r
117      * UI Dialgos. The full cause hierarchy is shown with the tree.\r
118      * In this case verbose includes class names.\r
119      * \r
120      * @param e exception\r
121      * @return message\r
122      */\r
123     public static String getUIFriendErrorMessageVerbose(Throwable e) {\r
124         String result = "";\r
125         Throwable pe = null;\r
126         while (e!=null && pe!=e) {\r
127             result += e.getClass().getName()+": "+e.getMessage()+"\n";\r
128             pe = e;\r
129             e = e.getCause();\r
130         }\r
131         return result;\r
132     }\r
133 \r
134     /**\r
135      * This call makes error message that is suitable for\r
136      * UI Dialogs. The full cause hierarchy is shown with the tree.\r
137      * In this case verbose includes class names.\r
138      * \r
139      * @param e exception\r
140      * @return message\r
141      */\r
142     public static String getUIFriendErrorMessage(Throwable e) {\r
143         String result = "";\r
144         Throwable pe = null;\r
145         while (e!=null && pe!=e) {\r
146             result += e.getMessage()+"\n";\r
147             pe = e;\r
148             e = e.getCause();\r
149         }\r
150         return result;\r
151     }\r
152 \r
153 }\r