/*
 * (C) Copyright IBM Corp. 2012
 *
 * LICENSE: Eclipse Public License v1.0
 * http://www.eclipse.org/legal/epl-v10.html
 */

package fabric.core.logging;

import java.io.PrintWriter;
import java.io.StringWriter;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;

public class FabricFormatter extends Formatter {

    /** Copyright notice. */
    public static final String copyrightNotice = "(C) Copyright IBM Corp. 2012";

    /*
     * Class fields
     */

    private String title = "unknown";

    private boolean longMessages = false;

    /*
     * Class methods
     */

    public FabricFormatter() {

        super();
    }

    public FabricFormatter(boolean longMessages) {

        super();
        this.longMessages = longMessages;

    }

    public void setTitle(String title) {

        this.title = title;
    }

    private StringBuilder pad(String string, int padTo) {
        StringBuilder buf = new StringBuilder();
        for (int p = padTo - string.length(); p > 0; p--) {
            buf.append(' ');
        }
        return buf;
    }

    @Override
    public String format(LogRecord r) {

        StringBuilder logMessage = new StringBuilder();

        /* Add a timestamp */
        Date d = new Date(r.getMillis());
        SimpleDateFormat sdf = new SimpleDateFormat("dd-MM-yy HH:mm:ss.SSS ");
        String date = sdf.format(new Date(r.getMillis()));
        logMessage.append(date);

        /* Add the title of the logging component */
        logMessage.append('[').append(this.title).append("] ");

        /* Add the thread ID */
        logMessage.append('[');
        logMessage.append(Thread.currentThread().getName());
        logMessage.append(pad(Thread.currentThread().getName(), 20));
        logMessage.append(']');
        logMessage.append(' ');

        /* Add the name of the log level */
        logMessage.append('[');
        logMessage.append(r.getLevel().getName());
        logMessage.append(pad(r.getLevel().getName(), 7));
        logMessage.append(']');
        logMessage.append(' ');

        if (longMessages) {

            /* Add the name of the logger */
            logMessage.append('[').append(r.getLoggerName()).append("] ");

            /* Add the short name of the source class */
            String className = r.getSourceClassName();
            className = className.substring(className.lastIndexOf('.') + 1, className.length());
            ;
            logMessage.append('[').append(className).append(" ");
            logMessage.append(r.getSourceMethodName());
            logMessage.append("] ");

        }

        /* Add the body of the message */
        logMessage.append(": ");
        logMessage.append(formatMessage(r));

        /* If an exception is involved... */
        if (r.getThrown() != null) {

            logMessage.append("Throwable: ");
            Throwable t = r.getThrown();
            PrintWriter pw = null;

            try {

                StringWriter sw = new StringWriter();
                pw = new PrintWriter(sw);
                t.printStackTrace(pw);
                logMessage.append(sw.toString());

            } finally {

                if (pw != null) {
                    try {
                        pw.close();
                    } catch (Exception e) {
                    }
                }
            }
        }

        return logMessage.toString() + "\n";
    }
}

@@ -2488,22 +2488,8 @@
 ));%0A
-            ;%0A
     
@@ -2548,36 +2548,12 @@
 end(
-%22 %22);%0A            logMessage
+'.')
 .app
@@ -2584,32 +2584,8 @@
 e())
-;%0A            logMessage
 .app
@@ -2645,24 +2645,24 @@
  message */%0A
+
         logM
@@ -2680,36 +2680,16 @@
 nd(%22: %22)
-;%0A        logMessage
 .append(

Correct formatting error in long messages.