
package uk.co.wingpath.util;

/**
* This class provides a simple implementation of the {@link Reporter}
* interface.
* All messages are simply printed to the standard error output, prefixed by the
* time and an indication of the message level ("ERROR:", "WARN:", etc.).
*/

public class StderrReporter
    implements Reporter
{
    private static StderrReporter instance = new StderrReporter ();

    private StderrReporter ()
    {
    }

    public static StderrReporter getInstance ()
    {
        return instance;
    }

    public static void print (String level, String fmt, Object... args)
    {
        String msg = String.format (fmt, args);
        long ms = System.currentTimeMillis ();
        String prefix = String.format ("%1$tH:%1$tM:%1$tS: %2$s: ", ms, level);

        String [] lines = msg.split ("\n");

        for (int i = 0 ; i < lines.length ; i++)
        {
            System.err.println (prefix + lines [i]);
        }
    }

    @Override
    public boolean fatal (String fmt, Object... args)
    {
        print ("FATAL", fmt, args);
        return true;
    }

    @Override
    public boolean fatal (Throwable t, String fmt, Object... args)
    {
        print ("FATAL", fmt, args);
        t.printStackTrace (System.err);
        return true;
    }

    @Override
    public void error (String helpId, String fmt, Object... args)
    {
        print ("ERROR", fmt, args);
    }

    @Override
    public void warn (String helpId, String fmt, Object... args)
    {
        print ("WARN", fmt, args);
    }

    @Override
    public void info (String helpId, String fmt, Object... args)
    {
        print ("INFO", fmt, args);
    }

    @Override
    public void trace (String helpId, String fmt, Object... args)
    {
        print ("TRACE", fmt, args);
    }

    @Override
    public boolean debug (String fmt, Object... args)
    {
        print ("DEBUG", fmt, args);
        return true;
    }

    @Override
    public void clear ()
    {
    }
}

