/*  AutoTester.java
 *  Geoff Oakham (oakhamg@cs)
 *  Nov 5, 2001
 */

import java.util.*;
import java.io.*;
import java.lang.*;

/** Responsible for displaying test results.  */
class BasicLogger implements Logger {

    /** Creates a new logger */
    public BasicLogger() {
    }

    public void printDebug(int testCase, String type, String text) {
        System.out.println("dbg " + type + "[" + testCase + "]: " + text);
    }

    /** Logs the offical results of a test case.  */
    public void logResult(int testCase, boolean result) {
        if (!result) {
            logMessage(testCase, "Test case failed.");
        }
    }

    /** Logs the offical results of a test case, one that ended in an unhandled
     * expcetion.
     */
    public void logResult(int testCase, java.lang.Throwable e) {
        String name = e.getClass().getName();
        logMessage(testCase, "Exception " + name);
        
        if (name.equals("java.lang.NoClassDefFoundError")) {
            logDetails(testCase, "  Class not found: " + e.getMessage());
        } else if (name.equals("java.lang.NoSuchMethodError")) {
            logDetails(testCase, "  Method not found: " + e.toString());
        } else if (name.equals("java.lang.StackOverflowError")) {
            // Do nothing--stack overflows exception traces are too long
            // to be useful in a concise report.
        } else {
            /* extract the stack trace into a string.. and log it. */
            CharArrayWriter caw = new CharArrayWriter();
            PrintWriter pw = new PrintWriter(caw);
            e.printStackTrace(pw);
            logDetails(testCase, caw.toString());
        }
    }

    /** Logs the offical results of a test case, one that didn't end.  */
    public void logResultTimeLimit(int testCase) {
        logMessage(testCase, "Infinite loop");
    }

    /** Log a message with a test case.  Messages are canned comments or errors
     * such as "testcase failed" or "infinite loop" or "exception foobar".
     * Messages are printed out once with a list of test cases they apply to
     * beside them.
     */
    public void logMessage(int testCase, String message) {
        System.out.println("[" + testCase + "] " + message);
    }

    /** Log a detail regarding a testcase.  Details are bits of information
     * that will only apply to this test case, for example a reason a test case
     * failed.  This is a chance to give detailed data back such as: "we were
     * looking for X but found Y".
     */
    public void logDetails(int testCase, String message) {
        System.out.println("[" + testCase + "] details:");
        System.out.println(message);
        System.out.println("end-of-details");
    }
}


