debug.js 3.37 KB
/**
 * Created by zhanghong on 14-7-29.
 */

var debugArea = document.createElement("div");
debugArea.setAttribute("style", "position: fixed;max-height:30%;overflow:scroll");
//var info = document.createElement("div");
//info.setAttribute("style", "background: green;max-height:20%;overflow:scroll");
//var warn = document.createElement("div");
//warn.setAttribute("style", "background: yellow;max-height:20%;overflow:scroll");
//var error = document.createElement("div");
//error.setAttribute("style", "background: red;max-height:20%;overflow:scroll");
//debugArea.appendChild(info);
//debugArea.appendChild(warn);
//debugArea.appendChild(error);

var url = window.location.href,
    parts = url.split('?'),
    d = {};
if(parts.length == 2){
    var hash = parts[1].indexOf('#')
    if(hash != -1){
        parts[1] = parts[1].substring(0, hash)
    }

    var pairs = parts[1].split('&')
    pairs.forEach(function(pair){
        var kv = pair.split('=')
        if(kv.length == 2){
            d[kv[0]] = kv[1]
        }
    })
}
var debug = (d.debug && d.debug == 1)?true:false;
var body = null;
if(debug) {
    var init = function () {
        body = document.getElementsByTagName("body")[0];
        if (body) {
            body.appendChild(debugArea);
            if (startUpTimer) {
                clearInterval(startUpTimer);
            }
        }
    }
    var startUpTimer = setInterval(init, 1000);
}

window.wwsDebug = {
    _log : function(type, message){
        var target = null;
        var style = null;
        switch(type){
            case 'info':
//                target = info;
                style = "background: green;";
                break;
            case 'warn':
//                target = warn;
                style = "background: yellow;";
                break;
            case 'error':
//                target = error;
                style = "background: red;";
                break;
        }

        //if(target){
        if(debug) {
            var line = document.createElement("h3");
            line.setAttribute("style", style);
            line.innerHTML = message;
            debugArea.appendChild(line);
        }
//        console.log(message);
        //}
    },
    error : function (message) {
        this._log('error', message);
    },
    warn : function(message){
        this._log('warn', message);
    },
    info : function(message){
        this._log('info', message);
    },
    log : function(message){
        this._log('info', message);
    }
}
//if(debug){
//    console.originalLog = console.log;
//    console.log = function(message){
//        wwsDebug.info(message);
//        console.originalLog(message);
//    }
//}

//function takeOverConsole(){
    var console = window.console
    if(console) {
        function intercept(method) {
            var original = console[method]
            console[method] = function () {
                wwsDebug[method](arguments);
                if (original.apply) {
                    // Do this for normal browsers
                    original.apply(console, arguments)
                } else {
                    // Do this for IE
                    var message = Array.prototype.slice.apply(arguments).join(' ')
                    original(message)
                }
            }
        }

        var methods = ['warn', 'error']
        for (var i = 0; i < methods.length; i++)
            intercept(methods[i])
    }
//}