/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

/**********************************************************************
 * --------------------------- W A R N I N G --------------------------
 *
 * This file is the same as Debug_ship.jnot, except the static final
 * constants have been set to enable debugging and tracing.  You must
 * double-edit any changes in this file into Debug_ship.jnot, and 
 * vice-versa.
 **********************************************************************/

package org.mozilla.jss.util;

/**
 * Controls debug-mode operation of JSS classes, and allows for printing
 * trace statements to standard output.
 *
 * @see org.mozilla.jss.util.Assert
 * @version $Revision$ $Date$
 */
public class Debug
{
    /**
     * Controls debug mode for JSS.  If DEBUG==true, debugging
     * code will be enabled.  If DEBUG==false, debugging code will not be
     * executed.  This variable does not influence the printing of trace
     * statements; their execution depends on the debug level, which can
     * be accessed through setLevel and getLevel.
     *
     * @see org.mozilla.jss.util.Assert
     */
    public static final boolean DEBUG = true;

    public static final int OBNOXIOUS = 10;
    public static final int VERBOSE = 5;
    public static final int ERROR = 1;
    public static final int QUIET = 0;

    /**
     * The debug level of the application. This gives the level of detail
     * trace messages will contain.  A level of 0 means no debugging 
     * statements will be printed.
     *
     * !!If you change this, change it in the native code too!!
     */
	private static int mDebugLevel = VERBOSE;

    /**
     * Print a trace statement to standard output.
     *
     * @param level The detail level of the statement.
     *  The level must be greater than 0.
     * @param str The trace statement.
     */
    public synchronized static void trace(int level, String str)
    {
        // validate arguments in debug mode
        if(DEBUG && (level < 0) ) {
            throw new AssertionException("invalid debugging level "+level+
                " in trace");
        }
		if (level <= mDebugLevel)
		{
			System.out.println(Thread.currentThread().getName() + ": " + str);
			System.out.flush();
		}
    }

    /**
     * Print a trace statement to standard output.
     * Uses the VERBOSE detail level.
     *
     * @param str The trace statement.
     */
    public synchronized static void trace(String str)
	{
            trace(VERBOSE, str);
	}

    /**
     * Set the debugging level of the application.
     * The level must not be negative.
     */
    public synchronized static void setLevel(int level)
	{
        // In debugging mode, validate argument
        if( DEBUG && (level < 0) ) {
            throw new AssertionException("invalid debugging level set");
        }

		mDebugLevel = level;
        setNativeLevel(level);
	}
    private static native void setNativeLevel(int level);

    /**
     * Get debugging level of the application.
     *
     * @return The current debugging level of the application.
     */
    public synchronized static int getLevel() {
		return mDebugLevel;
	}

    public synchronized static String getLevelStr() {
        switch(mDebugLevel) {
        case QUIET: return "QUIET";
        case ERROR: return "ERROR";
        case VERBOSE: return "VERBOSE";
        case OBNOXIOUS: return "OBNOXIOUS";
        default:
            return String.valueOf(mDebugLevel);
        }
    }
}

