Subversion Repositories display

Rev

Rev 416 | Blame | Compare with Previous | Last modification | View Log | RSS feed

/*
 * Copyright (C) 2011.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License version 3 as
 * published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful, but
 * WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * General Public License for more details.
 */

package test.display.check;

/**
 * Printing routines.
 *
 * @author Steve Ratcliffe
 */

public class Log {
        private static boolean verbose = true;
        private static boolean trace = true;
        private static boolean error;
        private static int errorCount;
        private static boolean saveVerbose;
        private static boolean saveTrace;

        public static void error(String fmt, Object... args) {
                error = true;
                errorCount++;
                System.out.printf("ERROR: " + ensureNewLine(fmt), args);
        }

        public static void test(String fmt, Object... args) {
                System.out.printf("TEST: " + ensureNewLine(fmt), args);
        }

        public static void info(String fmt, Object... args) {
                if (verbose)
                        System.out.format(ensureNewLine(fmt), args);
        }

        public static void trace(String fmt, Object... args) {
                if (trace)
                        System.out.format(ensureNewLine(fmt), args);
        }

        public static void checkNotNull(Object obj, String msg, Object... args) {
                if (obj == null)
                        error(msg, args);
        }

        private static String ensureNewLine(String msg) {
                if (msg.charAt(msg.length() - 1) == '\n') return msg;
                else return msg + '\n';
        }

        public static void checkEqual(Object expected, Object found, String fmt, Object ... args) {
                if (expected == null) {
                        if (found != null)
                                error(fmt + " expected null", args);
                } else if (found == null) {
                        error(fmt + " was null", args);
                } else if (!expected.equals(found)) {
                        error(fmt + " was " + found + ", expected " + expected, args);
                }
        }

        public static void checkNotZero(int value, String fmt, Object... args) {
                if (value == 0)
                        error(fmt, args);
        }

        public static void setShowLogs(boolean b) {
                if (b) {
                        saveVerbose = verbose;
                        saveTrace = trace;
                } else {
                        verbose = saveVerbose;
                        trace = saveTrace;
                }
                verbose = b;
                trace = b;
        }

        public static boolean isError() {
                return error;
        }

        public static int getErrorCount() {
                return errorCount;
        }

        public static void setVerbose(boolean verbose) {
                Log.verbose = saveVerbose = verbose;
        }

        public static void setTrace(boolean trace) {
                Log.trace = saveTrace = trace;
        }

        public static boolean isTrace() {
                return trace;
        }
}