// $Id: logger.js 1798 2008-10-17 01:19:30Z blaine $
/* This script depends on:
 *     webtoolkit.sprintf.js, available from
 *         http://www.webtoolkit.info/demo/javascript/spf/webtoolkit.sprintf.js
 *     data.format.js, available from
 *         http://www.javascripttoolbox.com
 */

var Logger = Class.create({
    displayLevel: false,  // Full firebug uses colors to indicate level
                          // Firebug Lite and other consoles will not
    setFilterLevel: function(level) { // Use this instead of filterLevel prop.
        // Set value to "DISABLE" to shut up logger completely
        if (level != "DEBUG" && level != "INFO" && level != "WARN"
                && level != "ERROR" && level != "DISABLE")
            this.error("Attempted to set filter level to Unsupported value: "
                + level);
        else
            this.filterLevel = level;
    },
    log: function(level, msg) {
        if (typeof(console) == "undefined") return; // No possible recovery
        if (!msg) return log("DEBUG", level);
        if (level == "DEBUG") {
            if (this.filterLevel != null && this.filterLevel != "DEBUG") return;
            if (this.displayLevel)
                console.debug(sprintf("%s %-5s %s", logDate(), level, msg));
            else
                console.debug(sprintf("%s %s", logDate(), msg));
        } else if (level == "INFO") {
            if (this.filterLevel != null && this.filterLevel != "DEBUG"
                    && this.filterLevel != "INFO") return;
            if (this.displayLevel)
                console.info(sprintf("%s %-5s %s", logDate(), level, msg));
            else
                console.info(sprintf("%s %s", logDate(), msg));
        } else if (level == "WARN") {
            if (this.filterLevel != null && this.filterLevel != "DEBUG"
                    && this.filterLevel != "INFO" && this.filterLevel != "WARN")
                return;
            if (this.displayLevel)
                console.warn(sprintf("%s %-5s %s", logDate(), level, msg));
            else
                console.warn(sprintf("%s %s", logDate(), msg));
        } else if (level == "ERROR") {
            if (this.filterLevel != null && this.filterLevel == "DISABLE")
                return;
            if (this.displayLevel)
                console.error(sprintf("%s %-5s %s", logDate(), level, msg));
            else
                console.error(sprintf("%s %s", logDate(), msg));
        } else {
            if (this.filterLevel != null && this.filterLevel == "DISABLE")
                return;
            if (this.displayLevel) {
                console.error(sprintf("%s %-5s %s", logDate(), "ERROR",
                        "Unsupported logging level: " + level));
                console.error(sprintf("%s %-5s %s", logDate(), "ERROR", msg));
            } else {
                console.error(sprintf("%s %s", logDate(),
                        "Unsupported logging level: " + level));
                console.error(sprintf("%s %s", logDate(), msg));
            }
        }
    },
    debug: function(msg) { this.log("DEBUG", msg); },
    info: function(msg) { this.log("INFO", msg); },
    warn: function(msg) { this.log("WARN", msg); },
    error: function(msg) { this.log("ERROR", msg); },
    test: function() {
        var origFilter = this.filterLevel;
        var origDisplay = this.displayLevel;
        this.filterLevel = null;
        this.displayLevel = false;

        this.debug("A test debug message");
        this.info("A test info message");
        this.warn("A test warning message");
        this.error("A test error message");
        this.displayLevel = true;
        this.debug("A test debug message");
        this.info("A test info message");
        this.warn("A test warning message");
        this.error("A test error message");
        this.displayLevel = false;
        this.error("NOW REPEATING WITH FILTER LEVEL of 'WARN':");
        this.setFilterLevel("WARN");
        this.debug("A test debug message");
        this.info("A test info message");
        this.warn("A test warning message");
        this.error("A test error message");
        this.displayLevel = true;
        this.debug("A test debug message");
        this.info("A test info message");
        this.warn("A test warning message");
        this.error("A test error message");

        // Restore Logger instance state
        this.filterLevel = origFilter;
        this.displayLevel = origDisplay;
    }
});

function logDate(inDate) {
    if (!inDate) inDate = new Date();
    return dateFormat(inDate, "isoDateTime") + "." + inDate.format("L");
}

var log = new Logger();
