/*
 * @author Ranides Atterwim <ranides@gmail.com>
 * @copyright Ranides Atterwim
 * @license WTFPL
 * @url http://ranides.net/projects/assira.junit
 */
package org.slf4j.impl;


import net.ranides.assira.junit.LogObserver;
import org.slf4j.helpers.FormattingTuple;
import org.slf4j.helpers.MarkerIgnoringBase;
import org.slf4j.helpers.MessageFormatter;
import static org.slf4j.spi.LocationAwareLogger.*;

/**
 * 
 * @author Ranides Atterwim <ranides@gmail.com>
 */
public final class ObserveLogger extends MarkerIgnoringBase {

    private static final long serialVersionUID = 7L;

    ObserveLogger(String name) {
        this.name = name;
    }

    private void log(int level, String message, Throwable t) {
        StringBuilder buf = new StringBuilder(32);
        buf.append('[').append(Thread.currentThread().getName()).append("] ");

        switch (level) {
            case TRACE_INT:
                buf.append("TRACE");
                break;
            case DEBUG_INT:
                buf.append("DEBUG");
                break;
            case INFO_INT:
                buf.append("INFO");
                break;
            case WARN_INT:
                buf.append("WARN");
                break;
            case ERROR_INT:
                buf.append("ERROR");
                break;
            default:
                throw new AssertionError("Unknown level: " + level);
        }
        buf.append(' ').append(String.valueOf(name)).append(" - ").append(message);
        LogObserver.append(level, buf.toString(), t);

    }

    private void formatAndLog(int level, String format, Object... arguments) {
        FormattingTuple tp = MessageFormatter.arrayFormat(format, arguments);
        log(level, tp.getMessage(), tp.getThrowable());
    }

    @Override
    public boolean isTraceEnabled() {
        return true;
    }

    @Override
    public void trace(String msg) {
        log(TRACE_INT, msg, null);
    }

    @Override
    public void trace(String format, Object param1) {
        formatAndLog(TRACE_INT, format, param1, null);
    }

    @Override
    public void trace(String format, Object param1, Object param2) {
        formatAndLog(TRACE_INT, format, param1, param2);
    }

    @Override
    public void trace(String format, Object... argArray) {
        formatAndLog(TRACE_INT, format, argArray);
    }

    @Override
    public void trace(String msg, Throwable t) {
        log(TRACE_INT, msg, t);
    }

    @Override
    public boolean isDebugEnabled() {
        return true;
    }

    @Override
    public void debug(String msg) {
        log(DEBUG_INT, msg, null);
    }

    @Override
    public void debug(String format, Object param1) {
        formatAndLog(DEBUG_INT, format, param1, null);
    }

    @Override
    public void debug(String format, Object param1, Object param2) {
        formatAndLog(DEBUG_INT, format, param1, param2);
    }

    @Override
    public void debug(String format, Object... argArray) {
        formatAndLog(DEBUG_INT, format, argArray);
    }

    @Override
    public void debug(String msg, Throwable t) {
        log(DEBUG_INT, msg, t);
    }

    @Override
    public boolean isInfoEnabled() {
        return true;
    }

    @Override
    public void info(String msg) {
        log(INFO_INT, msg, null);
    }

    @Override
    public void info(String format, Object arg) {
        formatAndLog(INFO_INT, format, arg, null);
    }

    @Override
    public void info(String format, Object arg1, Object arg2) {
        formatAndLog(INFO_INT, format, arg1, arg2);
    }

    @Override
    public void info(String format, Object... argArray) {
        formatAndLog(INFO_INT, format, argArray);
    }

    @Override
    public void info(String msg, Throwable t) {
        log(INFO_INT, msg, t);
    }

    @Override
    public boolean isWarnEnabled() {
        return true;
    }

    @Override
    public void warn(String msg) {
        log(WARN_INT, msg, null);
    }

    @Override
    public void warn(String format, Object arg) {
        formatAndLog(WARN_INT, format, arg, null);
    }

    @Override
    public void warn(String format, Object arg1, Object arg2) {
        formatAndLog(WARN_INT, format, arg1, arg2);
    }

    @Override
    public void warn(String format, Object... argArray) {
        formatAndLog(WARN_INT, format, argArray);
    }

    @Override
    public void warn(String msg, Throwable t) {
        log(WARN_INT, msg, t);
    }

    @Override
    public boolean isErrorEnabled() {
        return true;
    }

    @Override
    public void error(String msg) {
        log(ERROR_INT, msg, null);
    }

    @Override
    public void error(String format, Object arg) {
        formatAndLog(ERROR_INT, format, arg, null);
    }

    @Override
    public void error(String format, Object arg1, Object arg2) {
        formatAndLog(ERROR_INT, format, arg1, arg2);
    }

    @Override
    public void error(String format, Object... argArray) {
        formatAndLog(ERROR_INT, format, argArray);
    }

    @Override
    public void error(String msg, Throwable t) {
        log(ERROR_INT, msg, t);
    }
}
