changeset 13142:401830ff96f4

some improvements in Debug logging
author Erik Eckstein <erik.eckstein@oracle.com>
date Mon, 25 Nov 2013 13:41:18 +0100
parents 333ec6116aa7
children 4e599571ddb2
files graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Indent.java graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugScope.java
diffstat 3 files changed, 34 insertions(+), 12 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java	Mon Nov 25 13:37:24 2013 +0100
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java	Mon Nov 25 13:41:18 2013 +0100
@@ -231,7 +231,7 @@
         }
 
         @Override
-        public Indent logIndent(String msg, Object... args) {
+        public Indent logAndIndent(String msg, Object... args) {
             return this;
         }
 
@@ -240,6 +240,9 @@
             return this;
         }
 
+        @Override
+        public void close() {
+        }
     }
 
     private static final NoLogger noLoggerInstance = new NoLogger();
@@ -281,9 +284,9 @@
      * @param msg The format string of the log message
      * @param args The arguments referenced by the log message string
      * @return The new indentation level
-     * @see Indent#logIndent
+     * @see Indent#logAndIndent
      */
-    public static Indent logIndent(String msg, Object... args) {
+    public static Indent logAndIndent(String msg, Object... args) {
         if (ENABLED) {
             DebugScope scope = DebugScope.getInstance();
             scope.log(msg, args);
@@ -300,7 +303,7 @@
      * @param args The arguments referenced by the log message string
      * @return The new indentation level
      */
-    public static Indent logIndent(boolean enabled, String msg, Object... args) {
+    public static Indent logAndIndent(boolean enabled, String msg, Object... args) {
         if (ENABLED) {
             DebugScope scope = DebugScope.getInstance();
             boolean saveLogEnabled = scope.isLogEnabled();
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Indent.java	Mon Nov 25 13:37:24 2013 +0100
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Indent.java	Mon Nov 25 13:41:18 2013 +0100
@@ -48,8 +48,20 @@
  *          in.outdent();
  *      }
  * </pre>
+ * 
+ * Example usage with try-with-resources:
+ * 
+ * <pre>
+ * 
+ *      try (Indent in = Debug.logIndent("header message")) {
+ *          ...
+ *          in.log("message");
+ *          ...
+ *      }
+ * 
+ * </pre>
  */
-public interface Indent {
+public interface Indent extends AutoCloseable {
 
     /**
      * Prints an indented message to the DebugLevel's logging stream if logging is enabled.
@@ -81,9 +93,9 @@
      * @param msg The format string of the log message
      * @param args The arguments referenced by the log message string
      * @return The new indentation level
-     * @see Debug#logIndent
+     * @see Debug#logAndIndent
      */
-    Indent logIndent(String msg, Object... args);
+    Indent logAndIndent(String msg, Object... args);
 
     /**
      * Restores the previous indent level. Calling this method is important to restore the correct
@@ -92,4 +104,6 @@
      * @return The indent level from which this Indent was created.
      */
     Indent outdent();
+
+    void close();
 }
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugScope.java	Mon Nov 25 13:37:24 2013 +0100
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/internal/DebugScope.java	Mon Nov 25 13:41:18 2013 +0100
@@ -75,7 +75,7 @@
         }
 
         @Override
-        public Indent logIndent(String msg, Object... args) {
+        public Indent logAndIndent(String msg, Object... args) {
             log(msg, args);
             return indent();
         }
@@ -87,6 +87,11 @@
             }
             return lastUsedIndent;
         }
+
+        @Override
+        public void close() {
+            outdent();
+        }
     }
 
     private static ThreadLocal<DebugScope> instanceTL = new ThreadLocal<>();
@@ -231,7 +236,6 @@
     public <T> T scope(String newName, Runnable runnable, Callable<T> callable, boolean sandbox, DebugConfig sandboxConfig, Object[] newContext) {
         DebugScope oldContext = getInstance();
         DebugConfig oldConfig = getConfig();
-        boolean oldLogEnabled = oldContext.isLogEnabled();
         DebugScope newChild = null;
         if (sandbox) {
             newChild = new DebugScope(newName, newName, null, newContext);
@@ -240,6 +244,7 @@
             newChild = oldContext.createChild(newName, newContext);
         }
         instanceTL.set(newChild);
+        newChild.setLogEnabled(oldContext.isLogEnabled());
         newChild.updateFlags();
         try {
             return executeScope(runnable, callable);
@@ -247,7 +252,6 @@
             newChild.context = null;
             instanceTL.set(oldContext);
             setConfig(oldConfig);
-            setLogEnabled(oldLogEnabled);
         }
     }
 
@@ -283,7 +287,6 @@
             meterEnabled = false;
             timeEnabled = false;
             dumpEnabled = false;
-            setLogEnabled(false);
 
             // Be pragmatic: provide a default log stream to prevent a crash if the stream is not
             // set while logging
@@ -293,7 +296,9 @@
             timeEnabled = config.isTimeEnabled();
             dumpEnabled = config.isDumpEnabled();
             output = config.output();
-            setLogEnabled(config.isLogEnabled());
+            if (config.isLogEnabled()) {
+                setLogEnabled(true);
+            }
         }
     }