/***********************************************************************

GECCO, a platform for command and control gaming

Copyright (C) 2001 Henrik Bäärnhielm, Andreas Enblom, Niklas
Hallenfur, Karl Hasselström, Henrik Hägerström, Oskar Linde, Jing Fu
Zi and Jon Åslund

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or (at
your option) any later version.

This program 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
General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
USA.

***********************************************************************/

package gecco.server.core;
import java.io.*;
import java.text.SimpleDateFormat;

import java.util.Date;

/**
 *  This class manages the log, and exports methods that make it possible to
 *  write to the log.
 *
 * @author     Henrik Bäärnhielm, Karl Hasselström, Henrik Hägerström.
 * @created    April 30, 2001
 */
public class LogManager {

    /**
     *  The stream of the log file.
     */
    protected PrintWriter stream;

    /**
     *  The formatter for the dates in the log.
     */
    protected SimpleDateFormat formatter;


    /**
     *  Constructor for the LogManager object
     *
     * @param  filename         The name of the log file.
     * @param  autoFlush        Flag to indicate if the log should be flushed
     *      after each line.
     * @exception  IOException  Throws an IOException if the log file could not
     *      be opened.
     */
    public LogManager(String filename, boolean autoFlush) throws IOException {
        try {
            stream = new PrintWriter(new BufferedWriter(new FileWriter(filename, true)),
                    autoFlush);
        } catch (IOException e) {
            System.out.println("WARNING: LogManager couldn't open logfile. Logging disabled.");
        }
        formatter = new SimpleDateFormat("yyyy-MM-dd HH.mm.ss.SSS");
        log("LogManager started!");
        log("\n-----------------------------------------------------------------------------------------\n\n");
    }


    /**
     *  Constructor for the LogManager object. This does not use autoflushing
     *  after each line.
     *
     * @param  filename         The name of the log file.
     * @exception  IOException  Throws an IOException if the log file could not
     *      be opened.
     */
    public LogManager(String filename) throws IOException {
        this(filename, false);
    }


    /**
     *  Outputs a log message to the log file.
     *
     * @param  message  The log message.
     */
    public void log(String message) {
        printMessage("LOG", message);
    }


    /**
     *  Outputs a debug message to the log file.
     *
     * @param  message  The debug message.
     */
    public void debug(String message) {
        printMessage("DEBUG", message);
    }


    /**
     *  Flushes the log file. This should be called periodically if autoflush is
     *  not used, else there might be risk of losing data.
     */
    public void flush() {
        if (stream != null) {
            stream.flush();
        }
    }


    /**
     *  Prints a message to the log file, with a header and the current date and
     *  time.
     *
     * @param  header   Header of the message.
     * @param  message  Message text.
     */
    protected void printMessage(String header, String message) {
        if (stream != null) {
            stream.println(header + "\t: " + formatter.format(new Date()) +
                    "\t: " + message);
        }
    }

}

