-
Notifications
You must be signed in to change notification settings - Fork 1
/
log.js
202 lines (176 loc) · 5.4 KB
/
log.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
define(function(){
// summary:
// Allows the ability to turn the console on and off. Use debug=true
// in a script before this loads, or in the page url, like:
// http://mypage/index.html?debug=true
// Without debug=true in one of these two places, the console is turned off
// to prevent throwing errors in users' browsers that do not have Firebug
// installed.
//
// Also allows for module-specific turning on of logging, like MOD and
// API in the following example:
// http://mypage/index.html?debug=false&MOD=true&API=true
//
// Fixes some of the annoyances with the IE8 console:
// - clears the logs on reload
// - adds spaces between logged arguments
// - adds stubs for Firebug commands
// Fixes WebKit Mobile Debuggers:
// - concatenates all arguments into a string to get
// around the one argument-logged silliness.
// - Does not log objects.
// - Only log, info, debug and warn are supported.
// returns: Function
// The return is a log factory function that accepts two arguments:
// logName: String
// enabled: Boolean (or falsey)
// example:
// var log = logger('API', 1);
// log('a shortcut to avoid typic console!');
// This will provide you with a log function that can be turned on and
// off. This allows you to leave your logs in place and toggle them
// on and off during development. And during deployment, if you have
// them off, they can still be turned on by adding the logName to the
// URL.
//
var ua = window.navigator.userAgent;
// uncommented for dev
//window.dojoConfig = { debug:1 };
var fixConsole = function(){
if(window.dojoConfig === undefined){
window.dojoConfig = {};
}
var dbg = window.debug || dojoConfig.debug || /debug=true/.test(document.location.href) || false;
dojoConfig.loglimit = dojoConfig.loglimit || 299;
var count = dojoConfig.loglimit;
var common = "info,error,log,warn";
var more = "debug,time,timeEnd,assert,count,trace,dir,dirxml,group,groupEnd,groupCollapsed,exception";
var supportedBrowser = /Android/.test(ua) || /iPhone/.test(ua);
if(dojoConfig.pageDebug || (supportedBrowser && dojoConfig.androidDebug)){
var loaded = false;
if(!/Firefox/.test(ua)) window.console = {};
var c = window.console;
var node;
var list = [];
var cache = function(type, args){
list.push({
type:type,
args:args
});
}
var flush = function(){
list.forEach(function(o){
var div = document.createElement('div');
div.className = 'djLog';
div.innerHTML = o.type+":"+o.args;
node.appendChild(div);
});
list = [];
}
common.split(",").forEach(function(n){
(function(){
var type = n;
c[n] = function(){
cache(type, Array.prototype.slice.call(arguments).join(" "));
}
})();
});
more.split(",").forEach(function(n){
c[n] = function(){}
});
var ready = function(){
node = document.getElementById("djDbg");
flush();
};
var h1 = setInterval(function(){
if(!!document.body){
clearInterval(h1);
ready();
setInterval(function(){
if(list.length) flush();
}, 100);
}
}, 100);
}
if(!window.console) {
console = {};
dbg = false;
}
var fixIE = function(){
var calls = common.split(",");
for(var i=0;i<calls.length;i++){
var m = calls[i];
var n = "_"+calls[i]
console[n] = console[m];
console[m] = (function(){
var type = n;
return function(){
count--;
if(count == 0){
console.clear();
count = dojoConfig.loglimit;
}//console._log("***LOG LIMIT OF "+dojoConfig.loglimit+" HAS BEEN REACHED***");
if(count < 1) return;
try{
console[type](Array.prototype.slice.call(arguments).join(" "));
}catch(e){
throw new Error("Error **consoleFix** Failed on log type "+type);
}
}
})();
}
// clear the console on load. This is more than a convenience - too many logs crashes it.
// (If closed it throws an error)
try{ console.clear(); }catch(e){}
}
var fixMobile = function(){
// iPad and iPhone use the crappy old Safari debugger.
console._log = console.log;
console.log = console.debug = console.info = console.warn = console.error = function(){
var a = [];
for(var i=0;i<arguments.length;i++){
a.push(arguments[i])
}
console._log(a.join(" "));
}
}
var hideCalls = function(str){
var calls = str.split(",");
for(var i=0;i<calls.length;i++){
console[calls[i]] = function(){};
}
}
if(dbg && /Trident/.test(ua)){
fixIE();
hideCalls(more);
}else if(dbg && /iPad|iPhone/.test(ua)){
fixMobile();
}else if((/IE/.test(ua) && !/Trident/.test(ua)) || !dbg || !window.console){
hideCalls(more+","+common);
}
//console.log("test log")
}
fixConsole();
var hash = document.location.search+"#"+document.location.hash;
var logit = /IE/.test(ua) ?
function(args){
console.log(Array.prototype.slice.call(args).join(" "));
} :
function(args){
console.log.apply(console, args);
};
return function(name, enabled){
var r = new RegExp(name);
if(!r.test(hash) && (enabled === false || enabled === 0)) return function(){};
return function(){
var args = Array.prototype.slice.call(arguments);
args.forEach(function(a,i){
if(a && typeof a==="object" && a.tagName){
args[i]=a.tagName.toLowerCase()+"#"+a.id;
}
}, this);
if(name) args.unshift(" ["+name+"] ");
logit(args);
};
};
});