001    package util;
002    
003    /**
004     * Debug management class.
005     *
006     * <p>You can use calls to {@link #print Debug.print} for any debug message you want to print
007     * in your program.</p>
008     *
009     * @author Steffen Zschaler
010     * @version 1.0
011     * @since v1.0
012     */
013    public class Debug extends Object {
014    
015        /**
016         * The current debug level.
017         */
018        private static int s_nDebugLevel = 10;
019        /**
020         * Show system messages (level == -1)?
021         */
022        private static boolean s_fSysMsg = false;
023    
024        /**
025         * Set the maximum debug level.
026         *
027         * <p>When printing messages with {@link #print}, only messages with a debug level less or
028         * equal the maximum debug level will be printed.</p>
029         *
030         * @param nLevel the maximum debug level. Defaults to 10.
031         *
032         * @return the former maximum debug level.
033         */
034        public static synchronized int setMaxDebugLevel(int nLevel) {
035            int nOld = s_nDebugLevel;
036    
037            s_nDebugLevel = nLevel;
038    
039            return nOld;
040        }
041    
042        /**
043         * Switch on printing of framework internal debug messages.
044         */
045        public static void traceSystemMessages() {
046            traceSystemMessages(true);
047        }
048    
049        /**
050         * Switch off printing of framework internal debug messages.
051         */
052        public static void untraceSystemMessages() {
053            traceSystemMessages(false);
054        }
055    
056        /**
057         * Switch printing of framework internal debug messages.
058         *
059         * @param fSwitch if true, framework internal debug messages will henceforward be printed.
060         */
061        public static synchronized void traceSystemMessages(boolean fSwitch) {
062            s_fSysMsg = fSwitch;
063        }
064    
065        /**
066         * Print a message to STDERR.
067         *
068         * <p>Prints <code>s</code> to {@link java.lang.System#err}, if <code>nLevel</code> is less
069         * or equal the maximum debug level.</p>
070         *
071         * @param s the debug message.
072         * @param nLevel the message's debug level. Must be greater or equal zero.
073         */
074        public static synchronized void print(String s, int nLevel) {
075            if (((nLevel == -1) && (s_fSysMsg)) || ((nLevel >= 0) && (nLevel <= s_nDebugLevel))) {
076                System.err.println(s);
077            }
078        }
079    }