Anonymous avatar Anonymous committed ab1de27

Added log to preferences.

Comments (0)

Files changed (2)

src/org/lispdev/preferences/Log.java

+package org.lispdev.preferences;
+
+import java.lang.management.ManagementFactory;
+
+import org.eclipse.core.runtime.CoreException;
+import org.eclipse.core.runtime.IStatus;
+import org.eclipse.core.runtime.Status;
+
+/**
+ * <h1>Logging Facility</h1>
+ * <h2>How to log in code</h2>
+ * To log a message use one of the four functions: {@link EtLog#logInfo},
+ * {@link EtLog#logWarning}, {@link EtLog#logError},
+ * or {@link EtLog#logException}.
+ */
+public class Log
+{
+  public static void appendCallInformation(StringBuilder sb, int lvl, String tag)
+  {
+    final StackTraceElement ste = Thread.currentThread().getStackTrace()[lvl];
+    sb.append('(');
+    sb.append(ste.getFileName());
+    sb.append(':');
+    sb.append(ste.getLineNumber());
+    sb.append(')');
+    sb.append('<');
+    sb.append(tag);
+    sb.append(':');
+    sb.append(ste.getMethodName());
+    sb.append(':');
+    sb.append(ManagementFactory.getRuntimeMXBean().getUptime());
+    sb.append('>');
+  }
+
+  /**
+   * Throws exception with custom message.
+   * @param message Message that assigned to the exception
+   * @param e a low-level exception or <tt>null</tt> if not applicable
+   * @throws CoreException
+   *
+   * <p>
+   * This is a convenience function to throw an exception with custom
+   * message.
+   */
+  public static void abort(String message, Throwable e) throws CoreException
+  {
+    throw new CoreException(new Status(IStatus.ERROR, Plugin.ID,
+        0, message, e));
+  }
+
+  /**
+   * @param message
+   * @param severity
+   *          Integer constant from {@org.eclipse.core.runtime.IStatus}
+   */
+  private static void log(String message, int severity, Throwable e)
+  {
+    StringBuilder sb = new StringBuilder();
+    appendCallInformation(sb, 4, "");
+    sb.append(" ");
+    sb.append(message);
+    Plugin.get().getLog().log(
+        new Status(severity, Plugin.ID, 0, sb.toString(), e));
+  }
+
+
+  public static void logInfo(String message)
+  {
+    log(message, IStatus.INFO, null);
+  }
+
+  public static void logWarning(String message)
+  {
+    log(message, IStatus.WARNING, null);
+  }
+
+  public static void logError(String message)
+  {
+    log(message, IStatus.ERROR, null);
+  }
+
+  public static void logException(String message, Throwable e)
+  {
+    log(message, IStatus.ERROR, e);
+  }
+
+}

src/org/lispdev/preferences/PreferencesService.java

     }
     catch(BackingStoreException e)
     {
-      System.out.println("PreferencesService."
-          + "setByteArrayPreference(String, boolean):"
-          + "  BackingStoreException");
-      System.out.println("\tkey = " + key + "; value = " + value);
+      Log.logException("\tkey = " + key + "; value = " + value, e);
     }
   }
 
     }
     catch(BackingStoreException e)
     {
-      System.out.println("PreferencesService."
-          + "setByteArrayPreference(String, byte[]):  "
-          + "BackingStoreException");
-      System.out.println("\tkey = " + key + "; value = " + value);
+      Log.logException("\tkey = " + key + "; value = " + value, e);
     }
   }
 
     }
     catch(BackingStoreException e)
     {
-      System.out.println("PreferencesService."
-          + "setDoublePreference(String, double):  " + "BackingStoreException");
-      System.out.println("\tkey = " + key + "; value = " + value);
+      Log.logException("\tkey = " + key + "; value = " + value, e);
     }
   }
 
     }
     catch(BackingStoreException e)
     {
-      System.out.println("PreferencesService."
-          + "setFloatPreference(String, float):  " + "BackingStoreException");
-      System.out.println("\tkey = " + key + "; value = " + value);
+      Log.logException("\tkey = " + key + "; value = " + value, e);
     }
   }
 
     }
     catch(BackingStoreException e)
     {
-      System.out.println("PreferencesService."
-          + "setIntPreference(String, int):  " + "BackingStoreException");
-      System.out.println("\tkey = " + key + "; value = " + value);
+      Log.logException("\tkey = " + key + "; value = " + value, e);
     }
   }
 
     }
     catch(BackingStoreException e)
     {
-      System.out.println("PreferencesService.set."
-          + "LongPreference(String, long):  BackingStoreException");
-      System.out.println("\tkey = " + key + "; value = " + value);
+      Log.logException("\tkey = " + key + "; value = " + value, e);
     }
   }
 
     }
     catch(BackingStoreException e)
     {
-      System.out.println("PreferencesService."
-          + "setStringPreference(String, String):  " + "BackingStoreException");
-      System.out.println("\tkey = " + key + "; value = " + value);
+      Log.logException("\tkey = " + key + "; value = " + value, e);
     }
   }
 
     }
     catch(BackingStoreException e)
     {
-      System.out.println("PreferencesService.setPreferences():  "
-          + "BackingStoreException clearing existing preferences;"
-          + " attempting to add new preferences anyway");
+      Log.logException("Clearing existing preferences;" +
+      		" attempting to add new preferences anyway", e);
     }
 
     return node;
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.