/*
 * Copyright (c) 2002-2012, the original author or authors.
 *
 * This software is distributable under the BSD license. See the terms of the
 * BSD license in the documentation provided with this software.
 *
 * http://www.opensource.org/licenses/bsd-license.php
 */
package jline.internal;

import java.io.PrintStream;

import static jline.internal.Preconditions.checkNotNull;

/**
 * Internal logger.
 *
 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
 * @since 2.0
 */
public final class Log
{
    ///CLOVER:OFF

    public static enum Level
    {
        TRACE,
        DEBUG,
        INFO,
        WARN,
        ERROR
    }

    public static final boolean TRACE = Boolean.getBoolean(Log.class.getName() + ".trace");

    public static final boolean DEBUG = TRACE || Boolean.getBoolean(Log.class.getName() + ".debug");

    private static PrintStream output = System.err;

    public static PrintStream getOutput() {
        return output;
    }

    public static void setOutput(final PrintStream out) {
        output = checkNotNull(out);
    }

    /**
     * Helper to support rendering messages.
     */
    @TestAccessible
    static void render(final PrintStream out, final Object message) {
        if (message.getClass().isArray()) {
            Object[] array = (Object[]) message;

            out.print("[");
            for (int i = 0; i < array.length; i++) {
                out.print(array[i]);
                if (i + 1 < array.length) {
                    out.print(",");
                }
            }
            out.print("]");
        }
        else {
            out.print(message);
        }
    }

    @TestAccessible
    static void log(final Level level, final Object... messages) {
        //noinspection SynchronizeOnNonFinalField
        synchronized (output) {
            output.format("[%s] ", level);

            for (int i=0; i<messages.length; i++) {
                // Special handling for the last message if its a throwable, render its stack on the next line
                if (i + 1 == messages.length && messages[i] instanceof Throwable) {
                    output.println();
                    ((Throwable)messages[i]).printStackTrace(output);
                }
                else {
                    render(output, messages[i]);
                }
            }

            output.println();
            output.flush();
        }
    }

    public static void trace(final Object... messages) {
        if (TRACE) {
            log(Level.TRACE, messages);
        }
    }

    public static void debug(final Object... messages) {
        if (TRACE || DEBUG) {
            log(Level.DEBUG, messages);
        }
    }

    /**
     * @since 2.7
     */
    public static void info(final Object... messages) {
        log(Level.INFO, messages);
    }

    public static void warn(final Object... messages) {
        log(Level.WARN, messages);
    }

    public static void error(final Object... messages) {
        log(Level.ERROR, messages);
    }
}
@@ -303,27 +303,134 @@
 java.io.
-PrintStream
+ByteArrayOutputStream;%0Aimport java.io.PrintStream;%0Aimport java.util.logging.LogRecord;%0Aimport java.util.logging.Logger
 ;%0A%0Aimpor
@@ -573,16 +573,81 @@
 lon%3C/a%3E%0A
+ * @author %3Ca href=%22mailto:gnodet@gmail.com%22%3EGuillaume Nodet%3C/a%3E%0A
  * @sinc
@@ -854,22 +854,28 @@
 TRACE = 
-Boolea
+Configuratio
 n.getBoo
@@ -962,22 +962,28 @@
 RACE %7C%7C 
-Boolea
+Configuratio
 n.getBoo
@@ -1074,16 +1074,96 @@
 m.err;%0A%0A
+    private static boolean useJul = Configuration.getBoolean(%22jline.log.jul%22);%0A%0A
     publ
@@ -1985,32 +1985,125 @@
 ... messages) %7B%0A
+        if (useJul) %7B%0A            logWithJul(level, messages);%0A            return;%0A        %7D%0A
         //noinsp
@@ -2757,32 +2757,1404 @@
        %7D%0A    %7D%0A%0A
+    static void logWithJul(Level level, Object... messages) %7B%0A        Logger logger = Logger.getLogger(%22jline%22);%0A        Throwable cause = null;%0A        ByteArrayOutputStream baos = new ByteArrayOutputStream();%0A        PrintStream ps = new PrintStream(baos);%0A        for (int i = 0; i %3C messages.length; i++) %7B%0A            // Special handling for the last message if its a throwable, render its stack on the next line%0A            if (i + 1 == messages.length && messages%5Bi%5D instanceof Throwable) %7B%0A                cause = (Throwable) messages%5Bi%5D;%0A            %7D%0A            else %7B%0A                render(ps, messages%5Bi%5D);%0A            %7D%0A        %7D%0A        ps.close();%0A        LogRecord r = new LogRecord(toJulLevel(level), baos.toString());%0A        r.setThrown(cause);%0A        logger.log(r);%0A    %7D%0A%0A    private static java.util.logging.Level toJulLevel(Level level) %7B%0A        switch (level) %7B%0A            case TRACE:%0A                return java.util.logging.Level.FINEST;%0A            case DEBUG:%0A                return java.util.logging.Level.FINE;%0A            case INFO:%0A                return java.util.logging.Level.INFO;%0A            case WARN:%0A                return java.util.logging.Level.WARNING;%0A            case ERROR:%0A                return java.util.logging.Level.SEVERE;%0A            default:%0A                throw new IllegalArgumentException();%0A        %7D%0A    %7D%0A%0A
     public stati

Support JUL as a logging mechanism Fixes #93