/*
 * DateTimeFormatter.java  $Revision: 1.2 $  $Date: 2007/10/03 04:39:39 $
 *
 * Copyright (C) 2007 Meldcraft Corporation
 *
 * This file is part of Scaffold.
 * 
 * Scaffold is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 * 
 * Scaffold is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 * 
 * You should have received a copy of the GNU Lesser General Public License
 * along with Scaffold.  If not, see <http://www.gnu.org/licenses/>.
 */

package com.meldcraft.util.logging;

import java.io.*;
import java.text.*;
import java.util.Date;
import java.util.logging.Formatter;
import java.util.logging.LogRecord;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * Print a summary of the LogRecord with each line preceded by a time
 * stamp.  This is largely based on java.util.logging.SimpleFormatter.
 *
 * @version $Revision: 1.2 $ $Date: 2007/10/03 04:39:39 $
 * @author Rob Richards
 */
public class DateTimeFormatter extends Formatter
{
// Static variables
// Member variables
    private Date    mDate = new Date();
    private final static String mFormat = "{0,date,yyyy/MM/dd HH:mm:ss zzz} ";
    private MessageFormat mFormatter;
    private Object args[] = new Object[1];
    private String mLineSeparator = System.getProperties().getProperty("line.separator", "\n");
    private Pattern mLinePrefixPattern = Pattern.compile("^", Pattern.MULTILINE);

// Constructors and initialization
// Interface implemenations
// Public methods
    /**
     * Format the given LogRecord.
     * @param record the log record to be formatted.
     * @return a formatted log record
     */
    public synchronized String format(LogRecord record) {
        StringBuffer sb = new StringBuffer();

        // Minimize memory allocations here.
        mDate.setTime(record.getMillis());
        args[0] = mDate;
        StringBuffer dateText = new StringBuffer();
        if (mFormatter == null) {
            mFormatter = new MessageFormat(mFormat);
        }
        mFormatter.format(args, dateText, null);

        String linePrefix = dateText.toString() +
                            //record.getLevel().getLocalizedName() +
                            record.getLevel().toString() +
                            ": ";

        String message = formatMessage(record);
        sb.append(message);
        sb.append(mLineSeparator);

        if (record.getThrown() != null) {
            try {
                StringWriter sw = new StringWriter();
                PrintWriter pw = new PrintWriter(sw);
                record.getThrown().printStackTrace(pw);
                pw.close();
                sb.append(sw.toString());
            } catch (Exception ex) {
            }
        }

        Matcher matcher = mLinePrefixPattern.matcher(sb);
        return matcher.replaceAll(linePrefix);
    }

// Protected methods
// Private methods
// Inner classes
}
