// Decompiled by Jad v1.5.8e. Copyright 2001 Pavel Kouznetsov.
// Jad home page: http://www.geocities.com/kpdus/jad.html
// Decompiler options: packimports(3) deadcode 
// Source File Name:   Debug.java

package com.symantec.itools.lang;

import com.symantec.itools.util.Properties;
import java.io.*;
import java.net.URL;
import java.text.DateFormat;
import java.util.Calendar;

// Referenced classes of package com.symantec.itools.lang:
//            AssertionError, ExecutionContext, IllegalInstantiationError

public class Debug
{

    protected Debug()
    {
        throw new IllegalInstantiationError(com.symantec.itools.lang.Debug.class);
    }

    public static void setWriter(String s)
        throws IOException
    {
        if(writer == null)
        {
            if(s.equals("System.out"))
            {
                setWriter(((OutputStream) (System.out)));
                return;
            }
            if(s.equals("System.err"))
            {
                setWriter(((OutputStream) (System.err)));
                return;
            }
            setWriter(((Writer) (new FileWriter(s))));
        }
    }

    public static void setWriter(Writer writer1)
    {
        if(writer == null)
            writer = new PrintWriter(new BufferedWriter(writer1), true);
    }

    public static void setWriter(OutputStream outputstream)
    {
        if(writer == null)
            setWriter(((Writer) (new OutputStreamWriter(outputstream))));
    }

    public static void setWriter(File file)
        throws IOException
    {
        if(writer == null)
            setWriter(((Writer) (new FileWriter(file))));
    }

    public static void setWriter(URL url)
        throws IOException
    {
        if(writer == null)
        {
            if(url.getProtocol().equals("file"))
            {
                setWriter(((Writer) (new FileWriter(url.getFile()))));
                return;
            } else
            {
                throw new IOException("URL is not a local file");
            }
        } else
        {
            return;
        }
    }

    public static void setWriter(FileDescriptor filedescriptor)
    {
        if(writer == null)
            setWriter(((Writer) (new FileWriter(filedescriptor))));
    }

    public static void setInformationOff(boolean flag)
    {
        informationOff = flag;
    }

    public static void setWarningOff(boolean flag)
    {
        warningOff = flag;
    }

    public static void setErrorOff(boolean flag)
    {
        errorOff = flag;
    }

    public static void setExceptionOff(boolean flag)
    {
        exceptionOff = flag;
    }

    public static void setTraceOff(boolean flag)
    {
        traceOff = flag;
    }

    public static void setAssertOff(boolean flag)
    {
        assertOff = flag;
    }

    public static boolean isInformationOff()
    {
        return informationOff;
    }

    public static boolean isWarningOff()
    {
        return warningOff;
    }

    public static boolean isErrorOff()
    {
        return errorOff;
    }

    public static boolean isExceptionOff()
    {
        return exceptionOff;
    }

    public static boolean isTraceOff()
    {
        return traceOff;
    }

    public static boolean isAssertOff()
    {
        return assertOff;
    }

    public static void startLog(Class class1, String s)
    {
        if(writer != null)
        {
            writer.println("Platform Information:");
            writer.println("   - OS          : " + System.getProperty("os.name"));
            writer.println("   - Version     : " + System.getProperty("os.version"));
            writer.println("   - Platform    : " + System.getProperty("os.arch"));
            writer.println("   - JDK Version : " + System.getProperty("java.version"));
            writer.println("   - JDK Vendor  : " + System.getProperty("java.vendor"));
            writer.println();
            writer.println("Program Information:");
            writer.println("   - Main Class : " + class1.getName());
            writer.println("   - Version    : " + s);
            writer.println();
            writer.println("Start of debugging log - " + DateFormat.getDateTimeInstance(0, 0).format(Calendar.getInstance().getTime()));
        }
    }

    public static void finishLog()
    {
        if(writer != null)
        {
            writer.println();
            writer.println("End of debugging log - " + DateFormat.getDateTimeInstance(0, 0).format(Calendar.getInstance().getTime()));
            writer.close();
            writer = null;
        }
    }

    public static void logInformation(String s)
    {
        if(!isInformationOff())
            log(1, getExecutionContext(1), s);
    }

    public static void logWarning(String s)
    {
        if(!isWarningOff())
            log(2, getExecutionContext(1), s);
    }

    public static void logError(String s)
    {
        if(!isErrorOff())
            log(4, getExecutionContext(1), s);
    }

    public static void logException(Throwable throwable)
    {
        if(!isExceptionOff())
        {
            log(8, getExecutionContext(1), throwable.getClass().getName() + " : " + throwable.getMessage());
            if(writer != null)
            {
                throwable.printStackTrace(writer);
                writer.flush();
            }
        }
    }

    public static void logTrace(String s)
    {
        if(!isTraceOff())
        {
            ExecutionContext executioncontext = getExecutionContext(1);
            log(22, executioncontext, s + " " + executioncontext.getClazz() + " " + executioncontext.getMethod());
        }
    }

    protected static void log(int i, ExecutionContext executioncontext, String s)
    {
        if(writer != null)
        {
            writer.print(typeToString(i) + " - ");
            writer.print(executioncontext + " - ");
            writer.println(s);
            writer.flush();
        }
    }

    protected static String typeToString(int i)
    {
        switch(i)
        {
        case 1: // '\001'
            return "INFORMATION";

        case 2: // '\002'
            return "WARNING";

        case 4: // '\004'
            return "ERROR";

        case 8: // '\b'
            return "EXCEPTION";

        case 22: // '\026'
            return "TRACE";
        }
        return "unknown";
    }

    protected static String getStackTrace()
    {
        Throwable throwable = (new Throwable()).fillInStackTrace();
        StringWriter stringwriter = new StringWriter();
        throwable.printStackTrace(new PrintWriter(stringwriter));
        return stringwriter.getBuffer().toString();
    }

    public static ExecutionContext getExecutionContext(int i)
    {
        return new ExecutionContext(getStackTrace(), i);
    }

    public static void assert(boolean flag, String s)
    {
        if(!isAssertOff() && !flag)
            throw new AssertionError(s);
        else
            return;
    }

    public static final boolean DEBUG_OFF;
    public static final boolean ASSERT_OFF;
    public static final int INFORMATION = 1;
    public static final int WARNING = 2;
    public static final int ERROR = 4;
    public static final int EXCEPTION = 8;
    public static final int TRACE = 22;
    public static final int ASSERT = 50;
    protected static boolean informationOff;
    protected static boolean warningOff;
    protected static boolean errorOff;
    protected static boolean exceptionOff;
    protected static boolean traceOff;
    protected static boolean assertOff;
    protected static PrintWriter writer;

    static 
    {
        Properties properties;
        try
        {
            properties = new Properties("/com/symantec/itools/lang/Debug.properties");
        }
        catch(Throwable _ex)
        {
            properties = new Properties();
        }
        boolean flag;
        try
        {
            flag = properties.getBoolean("debug.off", true);
        }
        catch(Throwable _ex)
        {
            flag = true;
        }
        DEBUG_OFF = flag;
        boolean flag1;
        try
        {
            flag1 = properties.getBoolean("assert.off", true);
        }
        catch(Throwable _ex)
        {
            flag1 = true;
        }
        ASSERT_OFF = flag1;
        setInformationOff(properties.getBoolean("debug.information.off", false));
        setWarningOff(properties.getBoolean("debug.warning.off", false));
        setErrorOff(properties.getBoolean("debug.error.off", false));
        setExceptionOff(properties.getBoolean("debug.excption.off", false));
        setTraceOff(properties.getBoolean("debug.trace.off", true));
        setAssertOff(properties.getBoolean("debug.assert.off", true));
    }
}
