]> gerrit.simantics Code Review - simantics/platform.git/blobdiff - 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
diff --git a/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/ErrorLogger.java b/bundles/org.simantics.browsing.ui.common/src/org/simantics/browsing/ui/common/ErrorLogger.java
new file mode 100644 (file)
index 0000000..b398fee
--- /dev/null
@@ -0,0 +1,166 @@
+/*******************************************************************************\r
+ * Copyright (c) 2007, 2010 Association for Decentralized Information Management\r
+ * in Industry THTH ry.\r
+ * All rights reserved. This program and the accompanying materials\r
+ * are made available under the terms of the Eclipse Public License v1.0\r
+ * which accompanies this distribution, and is available at\r
+ * http://www.eclipse.org/legal/epl-v10.html\r
+ *\r
+ * Contributors:\r
+ *     VTT Technical Research Centre of Finland - initial API and implementation\r
+ *******************************************************************************/\r
+package org.simantics.browsing.ui.common;\r
+\r
+import org.eclipse.core.runtime.IStatus;\r
+import org.eclipse.core.runtime.Plugin;\r
+import org.eclipse.core.runtime.Status;\r
+import org.simantics.db.layer0.exception.InvalidVariableException;\r
+\r
+\r
+/**\r
+ * ErrorLogger sends error messages to "Error log" view.\r
+ * \r
+ * @author Toni Kalajainen\r
+ */\r
+public class ErrorLogger {\r
+\r
+    public static final boolean ECHO = true;\r
+\r
+    private final Plugin plugin;\r
+    private String pluginID;\r
+\r
+    public ErrorLogger(Plugin plugin) {\r
+        this.plugin = plugin;\r
+    }\r
+\r
+    private String getPluginID() {\r
+        if (pluginID==null)\r
+            pluginID = plugin.getBundle().getSymbolicName();\r
+        return pluginID;\r
+    }\r
+\r
+    /**\r
+     * Log a warning event.\r
+     * \r
+     * @param message message of the error\r
+     * @param exception the exception, or <code>null</code>\r
+     */\r
+    public void logWarning(String message, Throwable exception) {\r
+        log(IStatus.WARNING, IStatus.OK, message, exception);\r
+    }\r
+\r
+    /**\r
+     * Log a message.\r
+     * \r
+     * @param message message of the error\r
+     * @param exception the exception, or <code>null</code>\r
+     */\r
+    public void logMessage(String message, Throwable exception) {\r
+        log(IStatus.INFO, IStatus.OK, message, exception);\r
+    }\r
+\r
+    /**\r
+     * Log an error event.\r
+     * \r
+     * @param message message of the error\r
+     * @param exception the exception, or <code>null</code>\r
+     */\r
+    public void logError(String message, Throwable exception) {\r
+        // Errors are much more useful with a stack trace!\r
+        if (exception == null) {\r
+            exception = new RuntimeException();\r
+        }\r
+        log(IStatus.ERROR, IStatus.OK, message, exception);\r
+    }\r
+\r
+    public void log(int severity, int code, String message, Throwable exception) {\r
+        IStatus status = new Status(severity, getPluginID(), code, message, exception);\r
+        log(status);\r
+    }\r
+\r
+    public void log(IStatus status) {\r
+        plugin.getLog().log(status);\r
+    }\r
+\r
+    public static ErrorLogger getDefault() {\r
+        return Activator.getDefault().getErrorLogger();\r
+    }\r
+\r
+    public static void defaultLogError(String message, Throwable exception) {\r
+        if (filterException(exception))\r
+            return;\r
+        getDefault().logError(message, exception);\r
+        if(ECHO && exception != null) exception.printStackTrace();\r
+    }\r
+\r
+    public static void defaultLogError(Throwable exception) {\r
+        if (filterException(exception))\r
+            return;\r
+        getDefault().logError(getUIFriendErrorMessage(exception), exception);\r
+        if(ECHO && exception != null) exception.printStackTrace();\r
+    }\r
+\r
+    public static void defaultLogWarning(String message, Throwable exception) {\r
+        if (filterException(exception))\r
+            return;\r
+        getDefault().logWarning(message, exception);\r
+        if(ECHO && exception != null) exception.printStackTrace();\r
+    }\r
+\r
+    public static void defaultLogWarning(Throwable exception) {\r
+        if (filterException(exception))\r
+            return;\r
+        getDefault().logWarning(getUIFriendErrorMessage(exception), exception);\r
+        if(ECHO && exception != null) exception.printStackTrace();\r
+    }\r
+\r
+    public static void defaultLog(IStatus status) {\r
+        getDefault().log(status);\r
+        if(ECHO) System.out.println(status);\r
+    }\r
+\r
+    /**\r
+     * This call makes verbose error message that is suitable for\r
+     * UI Dialgos. The full cause hierarchy is shown with the tree.\r
+     * In this case verbose includes class names.\r
+     * \r
+     * @param e exception\r
+     * @return message\r
+     */\r
+    public static String getUIFriendErrorMessageVerbose(Throwable e) {\r
+        String result = "";\r
+        Throwable pe = null;\r
+        while (e!=null && pe!=e) {\r
+            result += e.getClass().getName()+": "+e.getMessage()+"\n";\r
+            pe = e;\r
+            e = e.getCause();\r
+        }\r
+        return result;\r
+    }\r
+\r
+    /**\r
+     * This call makes error message that is suitable for\r
+     * UI Dialogs. The full cause hierarchy is shown with the tree.\r
+     * In this case verbose includes class names.\r
+     * \r
+     * @param e exception\r
+     * @return message\r
+     */\r
+    public static String getUIFriendErrorMessage(Throwable e) {\r
+        String result = "";\r
+        Throwable pe = null;\r
+        while (e!=null && pe!=e) {\r
+            result += e.getMessage()+"\n";\r
+            pe = e;\r
+            e = e.getCause();\r
+        }\r
+        return result;\r
+    }\r
+\r
+    public static boolean filterException(Throwable t) {\r
+        if (t instanceof InvalidVariableException)\r
+            return true;\r
+        return false;\r
+    }\r
+\r
+}\r