// Decompiled by DJ v2.6.6.51 Copyright 2000 Atanas Neshkov  Date: 6/15/2001 17:43:20
// Home Page : http://members.fortunecity.com/neshkov/dj.html  - Check often for new version!
// Decompiler options: packimports(3) 
// Source File Name:   com/macromedia/generator/app/GenLog

package com.macromedia.generator.app;

import com.macromedia.generator.utils.Comparer;
import com.macromedia.generator.utils.GenUtils;
import java.io.*;
import java.util.Arrays;
import java.util.Date;

// Referenced classes of package com.macromedia.generator.app:
//            GenLogAlphaComparator, GenLogAlphaComparator2, GenLogFile, LogFileFilter

public class GenLog
{

    public GenLog(int i)
    {
        F2 = 0;
        F3 = null;
        F4 = 0L;
        F5 = 0L;
        F6 = 10;
        F8 = false;
        setLevel(i);
        F5 = System.currentTimeMillis();
    }

    private synchronized boolean F2()
    {
        return F6();
    }

    private String F3()
    {
        Date date = new Date();
        return date.toString() + ": ";
    }

    public long getCycle()
    {
        return F4 / 60000L;
    }

    public int getLevel()
    {
        return F2;
    }

    public int getMaxLogFiles()
    {
        return F6;
    }

    private boolean F4(File file)
    {
        GenLogFile genlogfile = new GenLogFile(file);
        if(genlogfile == null || !genlogfile.verify())
            return false;
        if(F6 > 0 && genlogfile.getIndex() >= F6)
        {
            try
            {
                if(file.exists() && file.isFile())
                    file.delete();
            }
            catch(Exception _ex)
            {
                return true;
            }
            return true;
        }
        File file1 = genlogfile.increment();
        if(file1 == null)
            return false;
        return file.renameTo(file1);
    }

    public void logMessage(String s)
    {
        logMessages(s, null, true);
    }

    public void logMessage(String s, boolean flag)
    {
        logMessages(s, null, flag);
    }

    public void logMessages(String s, String s1)
    {
        logMessages(s, s1, true);
    }

    public void logMessages(String s, String s1, boolean flag)
    {
        if(s == null)
            return;
        String as[] = new String[2];
        if(as == null)
            return;
        as[0] = "";
        if(flag)
            as[0] = F3();
        as[0] += s + System.getProperty("line.separator", "\r\n");
        as[1] = s1;
        logMessages(as);
    }

    public void logMessages(String as[])
    {
        if(as == null)
            return;
        if(!F5())
            return;
        try
        {
            F9(as);
        }
        catch(Exception exception)
        {
            System.out.println("Log file error: " + exception);
            System.out.println(as[0]);
        }
    }

    private boolean F5()
    {
        if(F2 <= 0 && !F8)
            return false;
        if(F3 == null)
            return false;
        if(F8())
            F2();
        return true;
    }

    private boolean F6()
    {
        GenLogFile genlogfile = new GenLogFile(F3);
        if(genlogfile == null || !genlogfile.verify())
            return false;
        File file = genlogfile.getParentFile();
        if(file == null)
            return false;
        LogFileFilter logfilefilter = new LogFileFilter(genlogfile);
        if(logfilefilter == null)
            return false;
        File afile[] = null;
        if(GenUtils.isJava2())
        {
            afile = file.listFiles(logfilefilter);
            if(afile == null)
                return false;
            GenLogAlphaComparator2 genlogalphacomparator2 = new GenLogAlphaComparator2();
            Arrays.sort(afile, genlogalphacomparator2);
        } else
        {
            String as[] = file.list(logfilefilter);
            if(as == null || as.length == 0)
                return false;
            afile = GenUtils.makeFileArray(file, as);
            if(afile == null || afile.length == 0)
                return false;
            GenLogAlphaComparator genlogalphacomparator = new GenLogAlphaComparator();
            genlogalphacomparator.Sort(afile);
        }
        for(int i = 0; i < afile.length; i++)
            if(!F4(afile[i]))
                return false;

        return true;
    }

    public void setCycle(long l, int i)
    {
        if(l < 0L)
            l = 0L;
        F4 = l * 60L * 1000L;
        if(i < 0)
            i = 0;
        F6 = i;
    }

    public void setForce(boolean flag)
    {
        F8 = flag;
    }

    public void setLevel(int i)
    {
        F2 = i;
    }

    public void setLogFile(File file)
    {
        if(file != null)
            F3 = file;
    }

    public void setLogFilePath(String s)
    {
        if(s != null)
            F3 = new File(s);
    }

    private synchronized boolean F8()
    {
        if(F4 <= 0L)
            return false;
        long l = System.currentTimeMillis();
        if(l > F5 + F4)
        {
            F5 = l;
            return true;
        } else
        {
            return false;
        }
    }

    private synchronized void F9(String as[])
        throws Exception
    {
        RandomAccessFile randomaccessfile = null;
        try
        {
            randomaccessfile = new RandomAccessFile(F3, "rw");
            if(randomaccessfile != null)
            {
                randomaccessfile.seek(randomaccessfile.length());
                int i = as.length;
                for(int j = 0; j < i; j++)
                    if(as[j] != null)
                        randomaccessfile.write(as[j].getBytes());

                randomaccessfile.close();
            }
        }
        catch(Exception exception)
        {
            if(randomaccessfile != null)
                randomaccessfile.close();
            throw exception;
        }
    }

    private final String C7 = "\r\n";
    private int F2;
    private File F3;
    private long F4;
    private long F5;
    private int F6;
    private boolean F8;
}