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