changeset 14755:3128becfec95

add Debug.logAndIndent with enabled-flag
author Erik Eckstein <erik.eckstein@oracle.com>
date Wed, 26 Mar 2014 10:06:25 +0100
parents 164903a50a9a
children 3e1e83287128
files graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java
diffstat 1 files changed, 67 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java	Wed Mar 26 10:04:37 2014 +0100
+++ b/graal/com.oracle.graal.debug/src/com/oracle/graal/debug/Debug.java	Wed Mar 26 10:06:25 2014 +0100
@@ -456,6 +456,73 @@
         return noLoggerInstance;
     }
 
+    /**
+     * A convenience function which combines enabling/disabling of logging and
+     * {@link #logAndIndent(String, Object...)}. Note: Use this method with care because it
+     * overrules the -G:Log option.
+     * 
+     * @param enabled Flag for enabling or disabling logging
+     * @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#logAndIndent
+     */
+    public static Indent logAndIndent(boolean enabled, String msg, Object... args) {
+        if (ENABLED) {
+            Collection<DebugDumpHandler> dumpHandlers;
+            PrintStream output;
+            DebugConfig currentConfig = DebugScope.getConfig();
+            if (currentConfig != null) {
+                dumpHandlers = currentConfig.dumpHandlers();
+                output = currentConfig.output();
+            } else {
+                dumpHandlers = Collections.<DebugDumpHandler> emptyList();
+                output = System.out;
+            }
+            DebugConfigScope configScope = new DebugConfigScope(Debug.fixedConfig(enabled, Debug.isDumpEnabled(), false, false, dumpHandlers, output));
+            return new IndentWithEnable(Debug.logAndIndent(msg, args), configScope);
+        }
+        return noLoggerInstance;
+    }
+
+    private static class IndentWithEnable implements Indent {
+
+        Indent delegate;
+        DebugConfigScope configScope;
+
+        IndentWithEnable(Indent delegate, DebugConfigScope configScope) {
+            this.delegate = delegate;
+            this.configScope = configScope;
+        }
+
+        @Override
+        public void log(String msg, Object... args) {
+            delegate.log(msg, args);
+        }
+
+        @Override
+        public Indent indent() {
+            return delegate.indent();
+        }
+
+        @Override
+        public Indent logAndIndent(String msg, Object... args) {
+            return delegate.logAndIndent(msg, args);
+        }
+
+        @Override
+        public Indent outdent() {
+            configScope.close();
+            return delegate.outdent();
+        }
+
+        @Override
+        public void close() {
+            configScope.close();
+            delegate.close();
+        }
+    }
+
     public static Iterable<Object> context() {
         if (ENABLED) {
             return DebugScope.getInstance().getCurrentContext();