/* JFox, the OpenSource J2EE Application Server
 *
 * Distributable under GNU LGPL license by gun.org
 * more details please visit http://www.huihoo.org/jfox
 */
package org.jfox.deployment.web;

import org.jfox.framework.logger.Logger;
import org.mortbay.util.Code;
import org.mortbay.util.Frame;
import org.mortbay.util.Log;
import org.mortbay.util.LogSink;


/**
 * @author <a href="mailto:young_yy@hotmail.com">Young Yang</a>
 */

public class JettyLogSink implements LogSink {
    private Logger logger;
    private boolean started = false;

    public JettyLogSink(Logger log) {
        logger = log;
    }

    public void start() {
        started = true;
    }

    public void stop() throws InterruptedException {
        started = false;
        //logger=null;
    }

    public void destroy() {
        logger = null;
    }

    public boolean isStarted() {
        return started;
    }

    public boolean isDestroyed() {
        return (logger == null);
    }

    public void setOptions(String logOptions) {

    }

    public String getOptions() {
        return "";
    }

    /**
     * Log a message.
     * This method formats the log information as a string and calls
     * log(String).  It should only be specialized by a derived
     * implementation if the format of the logged messages is to be changed.
     *
     * @param tag   Tag for type of log
     * @param msg   The message
     * @param frame The frame that generated the message.
     * @param time  The time stamp of the message.
     */
    public void log(String tag, Object msg, Frame frame, long time) {
        boolean debugging = Code.debug();
        msg = msg + (debugging ? ", " + frame : "");
        if(tag.equals(Log.DEBUG)) {
            this.debug((String) msg);
        }
        else if(tag.equals(Log.EVENT)) {
            this.event((String) msg);
        }
        else if(tag.equals(Log.WARN)) {
            this.warn((String) msg);
        }
        else if(tag.equals(Log.ASSERT)) {
            this.assert((String) msg);
        }
        else if(tag.equals(Log.FAIL)) {
            this.fail((String) msg);
        }
        else {
            logger.warn("JettyLogSink doesn't understand tag: '" + tag + "'");
        }
    }

    /**
     * Log a message.
     * The formatted log string is written to the log sink. The default
     * implementation writes the message to a PrintWriter.
     *
     * @param formattedLog
     */
    public synchronized void log(String formattedLog) {
        logger.info(formattedLog);
    }

    private void debug(String s) {
        logger.debug(s);
    }

    private void event(String s) {
        if(s.startsWith("Start") || s.startsWith("Stop")) {
            logger.info(s);
        }
        else {
            logger.debug(s);
        }
    }

    private void warn(String s) {
        logger.warn(s);
    }

    private void assert(String s) {
        logger.error(s);
    }

    private void fail(String s) {
        logger.fatal(s);
    }

}


