/* $Id: Trace.java,v 1.10 2006/10/20 20:23:07 m31 Exp $
 *
 * This file is part of the project "Hilbert II" - http://www.qedeq.org
 *
 * Copyright 2000-2006,  Michael Meyling <mime@qedeq.org>.
 *
 * "Hilbert II" 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.
 */

package org.qedeq.kernel.log;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;


/**
 * Developer trace.
 *
 * @version $Revision: 1.10 $
 * @author  Michael Meyling
 */
public final class Trace {

    /**
     * Constructor.
     */
    private Trace() {
        // don't call me
    }

    /**
     * Trace object.
     *
     * @param   tracingObject   Instance that wants to make a trace entry.
     * @param   method          Method of that object.
     * @param   object          Object to trace.
     */
    public static void trace(final Object tracingObject, final String method,
            final Object object) {
        final Log log = LogFactory.getFactory().getInstance(tracingObject.getClass()
            + "." + method);
        log.debug(object);
    }

    /**
     * Trace object.
     *
     * @param   tracingClass    Class that wants to make a trace entry.
     * @param   method          Method of that class.
     * @param   object          Object to trace.
     */
    public static void trace(final Class tracingClass, final String method,
            final Object object) {
        final Log log = LogFactory.getFactory().getInstance(tracingClass
            + "." + method);
        log.debug(method + ":" + object);
    }

    /**
     * Trace throwable.
     *
     * @param   tracingObject   Instance that wants to make a trace entry.
     * @param   method          Method of that object.
     * @param   throwable       Throwable to trace.
     */
    public static void trace(final Object tracingObject, final String method,
            final Throwable throwable) {
        final Log log = LogFactory.getFactory().getInstance(tracingObject.getClass()
            + "." + method);
        log.debug(throwable, throwable);
    }

    /**
     * Trace throwable.
     *
     * @param   tracingClass    Class that wants to make a trace entry.
     * @param   method          Method of that class.
     * @param   throwable       Throwable to trace.
     */
    public static void trace(final Class tracingClass, final String method,
            final Throwable throwable) {
        final Log log = LogFactory.getFactory().getInstance(tracingClass
            + "." + method);
        log.debug(throwable, throwable);
//        printClassMethod(tracingClass, method);
//        out.println(throwable);
//        throwable.printStackTrace(out);
    }

    /**
     * Trace throwable and extra description.
     *
     * @param   tracingObject   Instance that wants to make a trace entry.
     * @param   method          Method of that object.
     * @param   description     Further information.
     * @param   throwable       Throwable to trace.
     */
    public static void trace(final Object tracingObject, final String method,
            final String description, final Throwable throwable) {
        final Log log = LogFactory.getFactory().getInstance(tracingObject.getClass()
            + "." + method);
        log.debug(description, throwable);
    }

    /**
     * Trace throwable and extra description.
     *
     * @param   tracingClass    Class that wants to make a trace entry.
     * @param   method          Method of that class.
     * @param   description     Further information.
     * @param   throwable       Throwable to trace.
     */
    public static void trace(final Class tracingClass, final String method,
            final String description, final Throwable throwable) {
        final Log log = LogFactory.getFactory().getInstance(tracingClass
            + "." + method);
        log.debug(description, throwable);
    }

    /**
     * Trace method begin. Should be followed by an analogous
     * {@link #end(Object, String)} call.
     *
     * @param   tracingObject   Instance that wants to make a trace entry.
     * @param   method          Method of that object.
     */
    public static void begin(final Object tracingObject, final String method) {
        trace(tracingObject, method, "begin");
    }

    /**
     * Trace method begin. Should be followed by an analogous {@link #end(Class, String)} call.
     *
     * @param   tracingClass    Class that wants to make a trace entry.
     * @param   method          Method of that class.
     */
    public static void begin(final Class tracingClass, final String method) {
        trace(tracingClass, method, "begin");
    }

    /**
     * Trace method end.
     *
     * @param   tracingObject   Instance that wants to make a trace entry.
     * @param   method          Method of that object.
     */
    public static void end(final Object tracingObject, final String method) {
        trace(tracingObject, method, "end");
    }

    /**
     * Trace method end.
     *
     * @param   tracingClass    Class that wants to make a trace entry.
     * @param   method          Method of that class.
     */
    public static void end(final Class tracingClass, final String method) {
        trace(tracingClass, method, "end");
    }

    /**
     * Trace parameter.
     *
     * @param   tracingObject   Instance that wants to make a trace entry.
     * @param   method          Method of that object.
     * @param   param           Parameter to trace.
     * @param   value           Value of parameter.
     */
    public static void param(final Object tracingObject, final String method,
            final String param, final Object value) {
        trace(tracingObject, method, param + "=" + value);
    }

    /**
     * Trace parameter.
     *
     * @param   tracingClass    Class that wants to make a trace entry.
     * @param   method          Method of that class.
     * @param   param           Parameter to trace.
     * @param   value           Value of parameter.
     */
    public static void param(final Class tracingClass, final String method,
            final String param, final Object value) {
        trace(tracingClass, method, param + "=" + value);
    }

    /**
     * Trace parameter.
     *
     * @param   tracingObject   Instance that wants to make a trace entry.
     * @param   method          Method of that object.
     * @param   param           Parameter to trace.
     * @param   value           Value of parameter.
     */
    public static void param(final Object tracingObject, final String method,
            final String param, final int value) {
        trace(tracingObject, method, param + "=" + value);
    }

    /**
     * Trace parameter.
     *
     * @param   tracingClass    Class that wants to make a trace entry.
     * @param   method          Method of that class.
     * @param   param           Parameter to trace.
     * @param   value           Value of parameter.
     */
    public static void param(final Class tracingClass, final String method,
            final String param, final int value) {
        trace(tracingClass, method, param + "=" + value);
    }

    /**
     * Write stacktrace into trace.
     *
     * @param   tracingObject   Instance that wants to make a trace entry.
     * @param   method          Method of that object.
     */
    public static void traceStack(final Object tracingObject, final String method) {
        try {
            throw new Exception("Stacktrace");
        } catch (Exception e) {
            trace(tracingObject, method, e);
        }
    }

    /**
     * Write stacktrace into trace.
     *
     * @param   tracingClass    Class that wants to make a trace entry.
     * @param   method          Method of that class.
     */
    public static final void traceStack(final Class tracingClass, final String method) {
        try {
            throw new Exception("Stacktrace");
        } catch (Exception e) {
            trace(tracingClass, method, e);
        }
    }

}
