1 /*******************************************************************************
2 * Copyright (c) 2007, 2018 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;
18 import org.slf4j.Logger;
19 import org.slf4j.LoggerFactory;
22 * ErrorLogger sends error messages to "Error log" view.
24 * @author Toni Kalajainen
25 * @author Jani Simomaa
27 public class ErrorLogger {
29 private static final Logger LOGGER = LoggerFactory.getLogger(ErrorLogger.class);
30 private final Plugin plugin;
31 private String pluginID;
34 public ErrorLogger(Plugin plugin) {
38 private String getPluginID() {
40 pluginID = plugin.getBundle().getSymbolicName();
45 * Log a warning event.
47 * @param message message of the error
48 * @param exception the exception, or <code>null</code>
50 public void logWarning(String message, Throwable exception) {
51 log(IStatus.WARNING, IStatus.OK, message, exception);
52 LOGGER.warn(message, exception);
58 * @param message message of the error
59 * @param exception the exception, or <code>null</code>
61 public void logMessage(String message, Throwable exception) {
62 log(IStatus.INFO, IStatus.OK, message, exception);
63 LOGGER.info(message, exception);
69 * @param message message of the error
70 * @param exception the exception, or <code>null</code>
72 public void logError(String message, Throwable exception) {
73 // Errors are much more useful with a stack trace!
74 if (exception == null) {
75 exception = new RuntimeException();
77 log(IStatus.ERROR, IStatus.OK, message, exception);
78 LOGGER.error(message, exception);
81 public void log(int severity, int code, String message, Throwable exception) {
82 IStatus status = new Status(severity, getPluginID(), code, message, exception);
86 public void log(IStatus status) {
87 plugin.getLog().log(status);
90 public static ErrorLogger getDefault() {
91 return Activator.getDefault().getErrorLogger();
94 public static void defaultLogError(String message, Throwable exception) {
95 getDefault().logError(message, exception);
98 public static void defaultLogError(Throwable exception) {
99 getDefault().logError(getUIFriendErrorMessage(exception), exception);
102 public static void defaultLogWarning(String message, Throwable exception) {
103 getDefault().logWarning(message, exception);
106 public static void defaultLogWarning(Throwable exception) {
107 getDefault().logWarning(getUIFriendErrorMessage(exception), exception);
110 public static void defaultLog(IStatus status) {
111 getDefault().log(status);
115 * This call makes verbose error message that is suitable for
116 * UI Dialgos. The full cause hierarchy is shown with the tree.
117 * In this case verbose includes class names.
122 public static String getUIFriendErrorMessageVerbose(Throwable e) {
125 while (e!=null && pe!=e) {
126 result += e.getClass().getName()+": "+e.getMessage()+"\n";
134 * This call makes error message that is suitable for
135 * UI Dialogs. The full cause hierarchy is shown with the tree.
136 * In this case verbose includes class names.
141 public static String getUIFriendErrorMessage(Throwable e) {
144 while (e!=null && pe!=e) {
145 result += e.getMessage()+"\n";