Wednesday, 29 May 2013

jQuery.log

To avoid checks if window.console is available:

/****************************************
* jQuery extensions
****************************************/
jQuery.extend({
    "log": window.console ? window.console : { _isStubObject: true }
});
if (!jQuery.log._init) {
    jQuery.log._init = function (logLevel) {
        if (!jQuery.log._logLevels) {
            jQuery.log._logLevels = {
                All: 0,
                Debug: 1,
                Info: 2,
                Warn: 3,
                Error: 4
            };
        }
        if (!logLevel) logLevel = jQuery.log._logLevels.All;

        if (!jQuery.log._unsupported_message) jQuery.log._unsupported_message = function (functionName) {
            if (window && window.console) {
                if (window.console.warn)
                    window.console.warn("console." + functionName + "() is not supported!");
                else if (window.console.log)
                    window.console.log("WARNING: console." + functionName + "() is not supported!");
            }
        };

        if (!jQuery.log.memory) jQuery.log.memory = { _isStubObject: true };
        if (!jQuery.log.memory.jsHeapSizeLimit) jQuery.log.memory.jsHeapSizeLimit = -1;
        if (!jQuery.log.memory.totalJSHeapSize) jQuery.log.memory.totalJSHeapSize = -1;
        if (!jQuery.log.memory.usedJSHeapSize) jQuery.log.memory.usedJSHeapSize = -1;

        if (!jQuery.log.assert) jQuery.log.assert = function () { jQuery.log._unsupported_message("assert"); };
        if (!jQuery.log.clear) jQuery.log.clear = function () { jQuery.log._unsupported_message("clear"); };
        if (!jQuery.log.count) jQuery.log.count = function () { jQuery.log._unsupported_message("count"); };
        if (!jQuery.log.debug) {
            jQuery.log._unsupported_message("debug");
            jQuery.log.debug = window.console.log;
        };
        if (!jQuery.log.dir) jQuery.log.dir = function () { jQuery.log._unsupported_message("dir"); };
        if (!jQuery.log.dirxml) jQuery.log.dirxml = function () { jQuery.log._unsupported_message("dirxml"); };
        if (!jQuery.log.error) {
            jQuery.log._unsupported_message("error");
            jQuery.log.error = window.console.log;
        };
        if (!jQuery.log.exception) jQuery.log.exception = function () { jQuery.log._unsupported_message("exception"); };
        if (!jQuery.log.group) jQuery.log.group = function () { jQuery.log._unsupported_message("group"); };
        if (!jQuery.log.groupCollapsed) jQuery.log.groupCollapsed = function () { jQuery.log._unsupported_message("groupCollapsed"); };
        if (!jQuery.log.groupEnd) jQuery.log.groupEnd = function () { jQuery.log._unsupported_message("groupEnd"); };
        if (!jQuery.log.info) {
            jQuery.log._unsupported_message("info");
            jQuery.log.info = window.console.log;
        };
        //if (!jQuery.log.log) jQuery.log.log = function () { jQuery.log._unsupported_message("log"); };
        if (!jQuery.log.markTimeline) jQuery.log.markTimeline = function () { jQuery.log._unsupported_message("markTimeline"); };
        if (!jQuery.log.profile) jQuery.log.profile = function () { jQuery.log._unsupported_message("profile"); };
        if (!jQuery.log.profileEnd) jQuery.log.profileEnd = function () { jQuery.log._unsupported_message("profileEnd"); };
        if (!jQuery.log.table) jQuery.log.table = function () { jQuery.log._unsupported_message("table"); };
        if (!jQuery.log.time) jQuery.log.time = function () { jQuery.log._unsupported_message("time"); };
        if (!jQuery.log.timeEnd) jQuery.log.timeEnd = function () { jQuery.log._unsupported_message("timeEnd"); };
        if (!jQuery.log.timeStamp) jQuery.log.timeStamp = function () { jQuery.log._unsupported_message("timeStamp"); };
        if (!jQuery.log.trace) jQuery.log.trace = function () { jQuery.log._unsupported_message("trace"); };
        if (!jQuery.log.warn) {
            jQuery.log._unsupported_message("warn");
            jQuery.log.warn = window.console.log;
        };

        jQuery.log.info("INFO: jQuery.log is initialized.");
    };
}
jQuery.log._init();