Commits

sk  committed 4ae6443

Added tracing.

  • Participants
  • Parent commits a977056

Comments (0)

Files changed (3)

File src/org/lispdev/editor/EditorPlugin.java

   }
 
 
+  // === some code for tracing
+  private PrintStream traceStream;
+
+  /**
+   * Sets stream for tracing done by EtTrace enums.
+   */
+  public void setTraceStream(PrintStream pr)
+  {
+    traceStream = pr;
+  }
+
+  /**
+   * Returns stream which receives tracing from EtTrace enums.
+   */
+  public PrintStream getTraceStream()
+  {
+    if( traceStream == null )
+    {
+      return System.out;
+    }
+    else
+    {
+      return traceStream;
+    }
+  }
+
 
 }

File src/org/lispdev/editor/Log.java

     sb.append(message);
     EditorPlugin.get().getLog().log(
         new Status(severity, EditorPlugin.ID, 0, sb.toString(), e));
+    Trace.DEBUG.trace("Logged lvl "+severity+": "+sb.toString());
   }
 
 

File src/org/lispdev/editor/Trace.java

+package org.lispdev.editor;
+
+import java.io.PrintStream;
+
+import org.eclipse.core.runtime.Platform;
+
+/**
+ * <h1>Tracing Facility</h1>
+ *
+ * <h2>Tracing in code</h2>
+ * Before sending a message to current trace stream check if tracing is enabled
+ * using {@link Trace#enabled}. Then send a message to the trace stream using
+ * {@link Trace#trace}.
+ * Example:
+ * <br> <code>if(Trace.DEBUG.enabled) Trace.DEBUG.trace("message");</code>
+ * <p> If the tracing is not enabled on application start, its penalty
+ * is minimal, so it is encouraged to use tracing heavily.
+ * <p> By default the tracing will be sent to <code>stdout</code>, but it
+ * is possible to change it by calling {@link Core#setTraceStream}.
+ *
+ * <h2>Enable tracing during development from Eclipse</h2>
+ * Open "Run configurations..." dialog, select launch configuration for
+ * and select "Tracing" tab. Switch on "Enable tracing" box,
+ * and select plugins that you want to trace and which tracing tags to enable.
+ *
+ * <h2>Enable tracing in stand-alone Eclipse running the plugin</h2>
+ * Start Eclipse from command line with the <code>-debug .options</code> arguments:
+ * <code>eclipse.exe -debug .options</code>. Where .options is a file in eclipse root
+ * folder containing tracing options. For example:<br>
+ * <code>org.lispdev.editor/trace/debug=true<br>org.eclipse.help/debug=true</code>
+ *
+ * <h2>Adding new tracing tags</h2>
+ * To add new tracing tag:
+ * <ul>
+ * <li>add new enum value to {@link Trace} with
+ * corresponding tag name</li>
+ * <li>open <code>.options</code> file in root of <code>org.lispdev.editor</code>
+ * plugin and add a line corresponding to the tag to the options. Eclipse needs
+ * restart for the option to be visible in "Tracing" tab of the
+ * launch configuration.
+ */
+public enum Trace
+{
+  DEBUG("debug"),
+  CONTRACTS_VANILLA("contracts/vanilla");
+
+  public final boolean enabled;
+  private final String tag;
+
+  private Trace(String tag)
+  {
+    final String val = Platform.getDebugOption(EditorPlugin.ID + "/trace/" + tag);
+    enabled = (val != null && val.equalsIgnoreCase("true"));
+    this.tag = tag;
+  }
+
+  private static final String indent = "  ";
+
+  public void trace(String msg)
+  {
+    if( !enabled ) return;
+    StringBuilder sb = new StringBuilder();
+    Log.appendCallInformation(sb, 3, tag);
+    sb.append("\n");
+    if( msg != null )
+    {
+      String[] lines = msg.split("\n");
+      for( String s : lines )
+      {
+        sb.append(indent);
+        sb.append(s);
+        sb.append("\n");
+      }
+    }
+    else
+    {
+      sb.append("\n");
+    }
+    PrintStream out = EditorPlugin.get().getTraceStream();
+    if( out == null )
+    {
+      out = System.out;
+    }
+    out.print(sb.toString());
+  }
+}