
package uk.co.wingpath.util;

import org.apache.log4j.*;

/**
* This class implements the {@link Reporter} interface using a log4j
* {@link Logger}.
*/

public class Log4jReporter
    implements Reporter
{
    private final Logger logger;

    /** Replaces any newlines in a message by ": ".
    * @param msg the message string that may contain newlines.
    * @return the message string with newlines replaced.
    */
    private String joinLines (String msg)
    {
        String [] lines = msg.split ("\n");
        msg = "";

        for (int i = 0 ; i < lines.length ; i++)
        {
            if (i != 0)
                msg += ": ";
            msg += lines [i];
        }

        return msg;
    }

    /**
    * Creates a Log4jReporter using the supplied logger.
    * @param logger the logger to which messages should be forwarded.
    */
    public Log4jReporter (Logger logger)
    {
        this.logger = logger;
    }

    public void fatal (String msg)
    {
        logger.fatal (joinLines (msg));
    }

    public void fatal (String msg, Throwable t)
    {
        logger.fatal (joinLines (msg), t);
    }

    public void error (String helpId, String msg)
    {
        logger.error (joinLines (msg));
    }

    public void warning (String helpId, String msg)
    {
        logger.warn (joinLines (msg));
    }

    public void info (String helpId, String msg)
    {
        logger.info (joinLines (msg));
    }

    public void debug (String msg)
    {
        logger.debug (joinLines (msg));
    }

    public void debug (String msg, Throwable t)
    {
        logger.debug (joinLines (msg), t);
    }

    public void trace (String msg)
    {
        logger.trace (joinLines (msg));
    }

    public void clear ()
    {
    }
}

