class LogCompilation extends DefaultHandler implements ErrorHandler {

    public static void main(String[] args) throws Exception {
        Comparator<LogEvent> sort = LogParser.sortByStart;
        boolean statistics = false;
        boolean printInlining = false;
        boolean cleanup = false;
        boolean trapHistory = false;
        boolean printTimeStamps = false;
        boolean printID = true;
        int index = 0;

        while (args.length > index) {
            String a = args[index];
            if (a.equals("-e")) {
                sort = LogParser.sortByElapsed;
                index++;
            } else if (a.equals("-n")) {
                sort = LogParser.sortByNameAndStart;
                index++;
            } else if (a.equals("-s")) {
                sort = LogParser.sortByStart;
                index++;
            } else if (a.equals("-t")) {
                printTimeStamps = true;
                index++;
            } else if (a.equals("-c")) {
                cleanup = true;
                index++;
            } else if (a.equals("-S")) {
                statistics = true;
                index++;
            } else if (a.equals("-U")) {
                trapHistory = true;
                index++;
            } else if (a.equals("-h")) {
                usage(0);
            } else if (a.equals("-i")) {
                printInlining = true;
                index++;
            } else if (a.equals("-C")) {
                compare = true;
                index++;
            } else if (a.equals("-d")) {
                printID = false;
                index++;
            } else {
                if (a.charAt(0) == '-') {
                    System.out.println("Unknown option '" + a + "', assuming file name.");
                }
                break;
            }
        }

        if (index >= args.length) {
            usage(1);
        }

        if (compare) {
            compareLogs(index, args);
            return;
        }

        while (index < args.length) {
            ArrayList<LogEvent> events = null;
            try {
                events = LogParser.parse(args[index], cleanup);
            } catch (FileNotFoundException fnfe) {
                System.out.println("File not found: " + args[index]);
                System.exit(1);
            }

            Collections.sort(events, sort);

            if (statistics) {
                printStatistics(events, System.out);
            } else if (trapHistory) {
                printTrapHistory(events, System.out);
            } else {
                for (LogEvent c : events) {
                    if (c instanceof NMethod) {
                        // skip these
                        continue;
                    }
                    if (printTimeStamps) {
                        System.out.print(c.getStart() + ": ");
                    }
                    if (c instanceof Compilation) {
                        Compilation comp = (Compilation) c;
                        comp.print(System.out, printID, printInlining);
                    } else {
                        c.print(System.out, printID);
                    }
                }
            }
            index++;
        }
    }

}