/*
 * Copyright 2003-2007 the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.codehaus.groovy.tools.shell.util;

import org.codehaus.groovy.tools.shell.IO;

/**
 * Provides a very, very basic logging API.
 *
 * @version $Id$
 * @author <a href="mailto:jason@planet57.com">Jason Dillon</a>
 */
public final class Logger
{
    public static IO io;

    public final String name;

    private Logger(final String name) {
        assert name != null;

        this.name = name;
    }
    
    private void log(final String level, final Object msg, final Throwable cause) throws Exception {
        assert level != null;
        assert msg != null;
        
        if (io == null) {
            io = new IO();
        }
        
        io.out.print(ANSI.Renderer.encode(level, ANSI.Code.BOLD));
        io.out.print(" [");
        io.out.print(name);
        io.out.print("] ");
        io.out.println(msg);
        
        if (cause != null) {
            cause.printStackTrace(io.out);
        }
        
        io.flush();
    }
    
    //
    // Level helpers
    //
    
    private static final String DEBUG = "DEBUG";
    
    public static boolean debug = false;
    
    public boolean isDebugEnabled() {
        return debug;
    }
    
    public void debug(final Object msg) throws Exception {
        if (debug) {
            if (msg instanceof Throwable) {
                Throwable cause = (Throwable) msg;
                log(DEBUG, cause.getMessage(), cause);
            }
            else {
                log(DEBUG, msg, null);
            }
        }
    }
    
    public void debug(final Object msg, final Throwable cause) throws Exception {
        if (debug) {
            log(DEBUG, msg, cause);
        }
    }
    
    //
    // Factory access
    //
    
    public static Logger create(final Class type) {
        return new Logger(type.getName());
    }

    public static Logger create(final Class type, final String suffix) {
        return new Logger(type.getName() + "." + suffix);
    }
}

@@ -1073,22 +1073,16 @@
 g level,
- final
  Object 
@@ -1077,38 +1077,32 @@
 vel, Object msg,
- final
  Throwable cause
@@ -1248,32 +1248,33 @@
 ;%0A        %7D%0A
+%0A
         
 %0A        io.
@@ -1265,96 +1265,542 @@
     
-%0A        io.out.print(ANSI.Renderer.encode(level, ANSI.Code.BOLD));%0A        io.out.print
+// Allow the msg to be a Throwable, and handle it properly if no cause is given%0A        if (cause == null) %7B%0A            if (msg instanceof Throwable) %7B%0A                cause = (Throwable) msg;%0A                msg = cause.getMessage();%0A            %7D%0A        %7D%0A%0A        StringBuffer buff = new StringBuffer();%0A%0A        int color = ANSI.Code.BOLD;%0A        if (WARN.equals(level) %7C%7C ERROR.equals(level)) %7B%0A            color = ANSI.Code.RED;%0A        %7D%0A%0A        buff.append(ANSI.Renderer.encode(level, color));%0A        %0A        buff.append
 (%22 %5B
@@ -1811,28 +1811,27 @@
         
-io.out.print
+buff.append
 (name);%0A
@@ -1838,28 +1838,27 @@
         
-io.out.print
+buff.append
 (%22%5D %22);%0A
@@ -1869,37 +1869,35 @@
     
-io.out.println
+buff.append
 (msg);%0A
+%0A
         
 %0A   
@@ -1884,32 +1884,54 @@
 (msg);%0A%0A        
+io.out.println(buff);%0A
 %0A        if (cau
@@ -2344,95 +2344,155 @@
     
-if (msg instanceof Throwable) %7B
+log(DEBUG, msg, null);%0A        %7D
 %0A    
+%7D%0A
     
+%0A
     
-    Throwable cause = (Throwable) msg;%0A    
+public void debug(final Object msg, final Throwable cause) throws Exception %7B%0A        if (debug) %7B%0A
     
@@ -2510,34 +2510,19 @@
 (DEBUG, 
-cause.getMessage()
+msg
 , cause)
@@ -2535,41 +2535,123 @@
     
+%7D%0A
     %7D%0A
+%0A
     
-        else %7B%0A        
+private static final String WARN = %22WARN%22;%0A%0A    public void warn(final Object msg) throws Exception %7B%0A
     
@@ -2650,37 +2650,36 @@
 n %7B%0A        log(
-DEBUG
+WARN
 , msg, null);%0A  
@@ -2684,60 +2684,302 @@
     
+%7D%0A%0A
     
-    %7D%0A        %7D%0A    %7D%0A    %0A    public void debug
+public void warn(final Object msg, final Throwable cause) throws Exception %7B%0A        log(WARN, msg, cause);%0A    %7D%0A    %0A    private static final String ERROR = %22ERROR%22;%0A%0A    public void error(final Object msg) throws Exception %7B%0A        log(ERROR, msg, null);%0A    %7D%0A%0A    public void error
 (fin
@@ -3047,42 +3047,17 @@
     
-if (debug) %7B%0A            log(DEBUG
+log(ERROR
 , ms
@@ -3071,32 +3071,18 @@
 e);%0A    
-    %7D%0A    %7D%0A    
+%7D%0A
 %0A    //%0A

Add error() and warn() bits, make them render the level in red