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

import org.apache.log4j.Level;



/**
 *
 * @author ranides
 */
@SuppressWarnings("PMD.TooManyDifferentMethods")
public class AdvLogger extends org.apache.log4j.Logger {
    
    private static final String ADV_FQCN = AdvLogger.class.getName();
    
    private static final String TEXT_FOR_EXCEPTION = "Exception";
    
    protected static final AdvLoggerFactory FACTORY = new AdvLoggerFactory();

    protected AdvLogger(String name) {
        super(name);
    }
    
/* ************************************************************************** */
    
    public void ftrace(String format, Object... args) {
        if( isEnabledFor(Level.TRACE) ) {
            forcedLog(ADV_FQCN, Level.TRACE, String.format(format, args), null);
        }
    }
    
    public void fdebug(String format, Object... args) {
        if( isEnabledFor(Level.DEBUG) ) {
            forcedLog(ADV_FQCN, Level.DEBUG, String.format(format, args), null);
        }
    }
    
    public void finfo(String format, Object... args) {
        if( isEnabledFor(Level.INFO) ) {
            forcedLog(ADV_FQCN, Level.INFO, String.format(format, args), null);
        }
    }
    
    public void ferror(String format, Object... args) {
        if( isEnabledFor(Level.ERROR) ) {
            forcedLog(ADV_FQCN, Level.ERROR, String.format(format, args), null);
        }
    }
    
    public void ffatal(String format, Object... args) {
        if( isEnabledFor(Level.FATAL) ) {
            forcedLog(ADV_FQCN, Level.FATAL, String.format(format, args), null);
        }
    }
    
    public void fwarn(String format, Object... args) {
        if( isEnabledFor(Level.WARN) ) {
            forcedLog(ADV_FQCN, Level.WARN, String.format(format, args), null);
        }
    }
    
/* ************************************************************************** */
    
    public void xtrace(String format, Throwable cause, Object... args) {
        if( isEnabledFor(Level.TRACE) ) {
            forcedLog(ADV_FQCN, Level.TRACE, String.format(format, args), cause);
        }
    }
    
    public void xdebug(String format, Throwable cause, Object... args) {
        if( isEnabledFor(Level.DEBUG) ) {
            forcedLog(ADV_FQCN, Level.DEBUG, String.format(format, args), cause);
        }
    }
    
    public void xinfo(String format, Throwable cause, Object... args) {
        if( isEnabledFor(Level.INFO) ) {
            forcedLog(ADV_FQCN, Level.INFO, String.format(format, args), cause);
        }
    }
    
    public void xerror(String format, Throwable cause, Object... args) {
        if( isEnabledFor(Level.ERROR) ) {
            forcedLog(ADV_FQCN, Level.ERROR, String.format(format, args), cause);
        }
    }
    
    public void xfatal(String format, Throwable cause, Object... args) {
        if( isEnabledFor(Level.FATAL) ) {
            forcedLog(ADV_FQCN, Level.FATAL, String.format(format, args), cause);
        }
    }
    
    public void xwarn(String format, Throwable cause, Object... args) {
        if( isEnabledFor(Level.WARN) ) {
            forcedLog(ADV_FQCN, Level.WARN, String.format(format, args), cause);
        }
    }
    
/* ************************************************************************** */
    
    public void trace(Throwable cause) {
        log(ADV_FQCN, Level.TRACE, TEXT_FOR_EXCEPTION, cause);
    }
    
    @Override
    public void trace(Object message) {
        logex(Level.TRACE, message);
    }
    
    public void info(Throwable cause) {
        log(ADV_FQCN, Level.INFO, TEXT_FOR_EXCEPTION, cause);
    }
    
    @Override
    public void info(Object message) {
        logex(Level.INFO, message);
    }
    
    public void debug(Throwable cause) {
        log(ADV_FQCN, Level.DEBUG, TEXT_FOR_EXCEPTION, cause);
    }
    
    @Override
    public void debug(Object message) {
        logex(Level.DEBUG, message);
    }
    
    public void error(Throwable cause) {
        log(ADV_FQCN, Level.ERROR, TEXT_FOR_EXCEPTION, cause);
    }
    
    @Override
    public void error(Object message) {
        logex(Level.ERROR, message);
    }
    
    public void fatal(Throwable cause) {
        log(ADV_FQCN, Level.FATAL, TEXT_FOR_EXCEPTION, cause);
    }
    
    @Override
    public void fatal(Object message) {
        logex(Level.FATAL, message);
    }
    
    public void warn(Throwable cause) {
        log(ADV_FQCN, Level.WARN, TEXT_FOR_EXCEPTION, cause);
    }
    
    @Override
    public void warn(Object message) {
        logex(Level.WARN, message);
    }
  
    private void logex(Level level, Object message) {
        if( isEnabledFor(level) ) {
            if(message instanceof Throwable) {
                forcedLog(ADV_FQCN, level, TEXT_FOR_EXCEPTION, (Throwable)message);
            } else {
                forcedLog(ADV_FQCN, level, message, null);
            }
        }
    }
    
/* ************************************************************************** */
    
    public void trace() {
        log(ADV_FQCN, Level.TRACE, "trace", null);
    }
    
    public void debug() {
        log(ADV_FQCN, Level.DEBUG, "debug", null);
    }
    
    public void info() {
        log(ADV_FQCN, Level.INFO, "info", null);
    }

}
