/*
 * $Id: TraceLogger.java,v 1.5 2007/10/30 12:29:29 marbobo Exp $
 *
 * Copyright (c) Siemens AG 2008 
 *           (c) Universität Paderborn 2008 
 * Licensed under EUPL 1.1. 
 *
 * $Log: TraceLogger.java,v $
 * Revision 1.5  2007/10/30 12:29:29  marbobo
 * + removed unnecessary statements
 *
 * Revision 1.4  2004/11/10 15:02:46  koppy
 * Projektweite Überarbeitung von Error-Logs, Stacktraces,..
 *
 * Revision 1.3  2004/07/20 08:46:27  jonyp
 * added copyright info + cvs log
 *
 *
 */

package vkc.util;

import java.io.File;
import java.io.FileOutputStream;
import java.io.PrintStream;
import java.util.Date;

import vkc.misc.VKCLogManager;

public class TraceLogger extends Object {
    private boolean toConsole = false;
    private File logFile = null;

    private TraceLogger() {
    }

    public TraceLogger(File logFile) {
        this();

        this.logFile = logFile;

        FileOutputStream fos = null;
        PrintStream printStream = null;

        try {
            logFile.getParentFile().mkdirs();
            logFile.getParentFile().mkdir();

            // Trace-Datei ggfs. anlegen
            fos = new FileOutputStream(logFile.getAbsolutePath(), true);
            printStream = new PrintStream(fos);
        } catch (Exception e) {
            VKCLogManager.errorLog(this, "", e.getMessage(), e);

            return;
        } finally {
            FileUtils.closeOutputStream(fos);
            FileUtils.closeOutputStream(printStream);
            printStream = null;
            fos = null;
        }
    }

    public synchronized void logStackTrace(Throwable e) {
        logStackTrace(e, null);
    }

    public synchronized void logStackTrace(Throwable e, String errorId) {
        if (e == null) {
            return;
        }

        if (toConsole) {
            if (errorId != null && errorId.length() > 0) {
                System.err.print(errorId + ", ");
            }

            VKCLogManager.errorLog(this, "", e.getMessage(), e);
        }

        FileOutputStream fos = null;
        PrintStream printStream = null;
        Date logDate = new Date(System.currentTimeMillis());

        try {
            fos = new FileOutputStream(logFile.getAbsolutePath(), true);
            printStream = new PrintStream(fos);

            printStream.print("\n");
            printStream.print(DateUtils.getDD_MM_YYYY(logDate));
            printStream.print(" - ");
            printStream.print(DateUtils.getHH_MM_SS(logDate));

            if (errorId != null && errorId.length() > 0) {
                printStream.print(", " + errorId);
            }

            printStream.print(": \n");
            e.printStackTrace(printStream);
            printStream.flush();
        } catch (Exception ex) {
            VKCLogManager.errorLog(this, "", ex.getMessage(), ex);

            return;
        } finally {
            FileUtils.closeOutputStream(fos);
            FileUtils.closeOutputStream(printStream);
            printStream = null;
            fos = null;

        }
    }

    public void printToConsole(boolean b) {
        toConsole = b;
    }

    public final static String createErrorId() {
        return "errorId." + System.currentTimeMillis();
    }
}