forked from Steve-Fenton/jQuery-Plugins
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSupplyConsole.js
161 lines (160 loc) · 5.12 KB
/
SupplyConsole.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
var SupplyConsole = (function() {
return {
stackTrace : true,
maximumMessages : 50,
timers : {},
getConsole : function () {
var consoleElement = document.getElementById("consoleLogger");
if (consoleElement === null) {
consoleElement = document.createElement("div");
consoleElement.id = "consoleLogger";
document.getElementsByTagName("body")[0].appendChild(consoleElement);
}
return consoleElement;
},
clearConsole : function () {
var i;
var consoleElement = this.getConsole();
var messageElements = consoleElement.getElementsByTagName("p");
if (messageElements.length > this.maximumMessages) {
for (i = this.maximumMessages; i < messageElements.length; i++) {
consoleElement.removeChild(messageElements[i]);
}
}
},
getTime : function () {
var now = new Date();
var day = this.padLeft(now.getDate(), 2, "0");
var month = this.padLeft(now.getMonth() + 1, 2, "0");
var year = now.getFullYear();
var hour = this.padLeft(now.getHours(), 2, "0");
var minute = this.padLeft(now.getMinutes(), 2, "0");
var second = this.padLeft(now.getSeconds(), 2, "0");
return day + "/" + month + "/" + year + " : " +
hour + ":" + minute + ":" + second;
},
getStackTrace : function () {
var i;
var stackTraceOutput = "";
if (this.stackTrace) {
var trace = printStackTrace();
if (trace[0].indexOf("@") > -1 && trace.length > 5) {
for (i = 5; i < trace.length; i++) {
var methodEnd = trace[i].indexOf("@");
var method = trace[i].substring(0, methodEnd);
var lineNumberStart = trace[i].lastIndexOf(":");
var lineNumber = this.padLeft(trace[i].substring(lineNumberStart + 1), 5, " ");
var filePath = trace[i].substring(methodEnd + 1, lineNumberStart);
var fileNamePosition = filePath.lastIndexOf("/");
var fileName = filePath.substring(fileNamePosition + 1);
stackTraceOutput += fileName + " " + lineNumber + " " + method + "<br>";
}
} else if (trace.length > 4) {
for (i = 4; i < trace.length; i++) {
stackTraceOutput += trace[i] + "<br>";
}
}
} else {
stackTraceOutput = "Stack trace not enabled.";
}
return stackTraceOutput;
},
getLineNumber : function () {
var lineNumber = -1;
if (this.stackTrace) {
var trace = printStackTrace();
if (trace[0].indexOf("@") > -1 && trace.length > 5) {
var lineNumberStart = trace[6].lastIndexOf(":");
lineNumber = trace[6].substring(lineNumberStart + 1);
} else {
var lineNumberStart = trace[5].lastIndexOf(":");
lineNumber = trace[5].substring(lineNumberStart + 1);
}
}
return lineNumber;
},
padLeft : function (string, length, padder) {
string = string.toString();
padder = padder.toString();
while (string.length < length) {
string = padder + string;
}
return string;
},
writeMessage : function (message, type) {
var consoleElement = this.getConsole();
var messageElement = document.createElement("p");
var extendedMessage = this.getTime() + " : ";
if (this.stackTrace) {
var lineNumber = this.getLineNumber();
extendedMessage += "Line " + lineNumber + " - "
}
extendedMessage += message;
messageElement.innerHTML = extendedMessage;
messageElement.className = type;
consoleElement.insertBefore(messageElement, consoleElement.firstChild);
this.clearConsole();
},
assert : function (message) {
if (message === true) {
this.writeMessage("Assert passed", "info");
} else {
this.writeMessage("Assert failed", "error");
}
},
debug : function (message) {
this.writeMessage(message, "info");
},
dir : function (message) {
this.notImplemented("dir");
},
dirxml : function (message) {
this.notImplemented("dirxml");
},
error : function (message) {
this.writeMessage("[X] " + message, "error");
},
group : function (message) {
this.notImplemented("group");
},
groupEnd : function (message) {
this.notImplemented("groupEnd");
},
info : function (message) {
this.writeMessage("[i] " + message, "info");
},
log : function (message) {
this.writeMessage(message, "info");
},
profile : function (message) {
this.notImplemented("profile");
},
time : function (timerName) {
this.timers[timerName] = new Date();
this.writeMessage("Timer '" + timerName + "' started.", "info");
},
timeEnd : function (timerName) {
var endTime = new Date();
var duration = (endTime - this.timers[timerName]) / 1000;
if (isNaN(duration)) {
this.error("A timer called '" + timerName + "' was not found.");
} else {
var duration = (endTime - this.timers[timerName]) / 1000;
this.writeMessage("Timer '" + timerName + "' took " + duration + " seconds.", "info");
}
},
trace : function (message) {
var stackTrace = this.getStackTrace();
this.writeMessage("Stack Trace: <br>" + stackTrace, "info");
},
warn : function (message) {
this.writeMessage("[!] " + message, "warning");
},
notImplemented : function (type) {
this.writeMessage("console." + type + " call not implemented", "warning");
}
};
}());
if (typeof(console) === 'undefined') {
var console = SupplyConsole;
}