1 /*******************************************************************************
2 * Copyright (c) 2007, 2010 Association for Decentralized Information Management
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
10 * VTT Technical Research Centre of Finland - initial API and implementation
11 *******************************************************************************/
12 package org.simantics.utils.ui;
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;
21 * ErrorLogger sends error messages to "Error log" view.
23 * @author Toni Kalajainen
25 public class ErrorLogger {
27 public static final boolean ECHO = true;
29 private final Plugin plugin;
30 private String pluginID;
33 public ErrorLogger(Plugin plugin) {
37 private String getPluginID() {
39 pluginID = plugin.getBundle().getSymbolicName();
44 * Log a warning event.
46 * @param message message of the error
47 * @param exception the exception, or <code>null</code>
49 public void logWarning(String message, Throwable exception) {
50 log(IStatus.WARNING, IStatus.OK, message, exception);
56 * @param message message of the error
57 * @param exception the exception, or <code>null</code>
59 public void logMessage(String message, Throwable exception) {
60 log(IStatus.INFO, IStatus.OK, message, exception);
66 * @param message message of the error
67 * @param exception the exception, or <code>null</code>
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();
74 log(IStatus.ERROR, IStatus.OK, message, exception);
77 public void log(int severity, int code, String message, Throwable exception) {
78 IStatus status = new Status(severity, getPluginID(), code, message, exception);
82 public void log(IStatus status) {
83 plugin.getLog().log(status);
86 public static ErrorLogger getDefault() {
87 return Activator.getDefault().getErrorLogger();
90 public static void defaultLogError(String message, Throwable exception) {
91 getDefault().logError(message, exception);
92 if(ECHO && exception != null) exception.printStackTrace();
95 public static void defaultLogError(Throwable exception) {
96 getDefault().logError(getUIFriendErrorMessage(exception), exception);
97 if(ECHO && exception != null) exception.printStackTrace();
100 public static void defaultLogWarning(String message, Throwable exception) {
101 getDefault().logWarning(message, exception);
102 if(ECHO && exception != null) exception.printStackTrace();
105 public static void defaultLogWarning(Throwable exception) {
106 getDefault().logWarning(getUIFriendErrorMessage(exception), exception);
107 if(ECHO && exception != null) exception.printStackTrace();
110 public static void defaultLog(IStatus status) {
111 getDefault().log(status);
112 if(ECHO) System.out.println(status);
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.
123 public static String getUIFriendErrorMessageVerbose(Throwable e) {
126 while (e!=null && pe!=e) {
127 result += e.getClass().getName()+": "+e.getMessage()+"\n";
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.
142 public static String getUIFriendErrorMessage(Throwable e) {
145 while (e!=null && pe!=e) {
146 result += e.getMessage()+"\n";