Commits

Anonymous committed a977056

Refactored logging.

Comments (0)

Files changed (2)

src/org/lispdev/editor/EditorPlugin.java

 import java.util.SortedSet;
 import java.util.TreeSet;
 
-import org.eclipse.core.runtime.ILog;
-import org.eclipse.core.runtime.Status;
 import org.eclipse.core.runtime.preferences.DefaultScope;
 import org.eclipse.jface.resource.FontRegistry;
 import org.eclipse.jface.text.IDocument;
     return lispScanner;
   }
 
-
-  //// logging - taken from imp
-
-  private ILog sLog = null;
-  protected boolean fEmitInfoMessages = false;
-
-  public void maybeWriteInfoMsg(String msg)
-  {
-    if( !fEmitInfoMessages ) return;
-    if( msg == null )
-    {
-      msg = "No message given";
-    }
-    writeInfoMsg(msg);
-  }
-
-  public void writeInfoMsg(String msg)
-  {
-    if( msg == null ) msg = "No message given";
-    Status status = new Status(Status.INFO, ID, 0, msg, null);
-
-    if( sLog == null ) sLog = getLog();
-
-    sLog.log(status);
-  }
-
-  public void writeErrorMsg(String msg)
-  {
-    if( msg == null ) msg = "No message given";
-    Status status = new Status(Status.ERROR, ID, 0, msg, null);
-
-    if( sLog == null ) sLog = getLog();
-
-    sLog.log(status);
-  }
-
-  public void logException(String msg, Throwable t)
-  {
-    if( msg == null )
-    {
-      if( t == null || t.getMessage() == null ) msg = "No message given";
-      else msg = t.getMessage();
-    }
-
-    Status status = new Status(Status.ERROR, ID, 0, msg, t);
-
-    if( sLog == null ) sLog = getLog();
-
-    sLog.log(status);
-  }
-
   /// preferences
   /**
    * The unique project-independent preferences service for this plugin.

src/org/lispdev/editor/Log.java

+package org.lispdev.editor;
+
+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, EditorPlugin.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);
+    EditorPlugin.get().getLog().log(
+        new Status(severity, EditorPlugin.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);
+  }
+
+}