forked from incompl/boxbox
/
SoftLogger.js
149 lines (131 loc) · 4.23 KB
/
SoftLogger.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
/*
Copyright (C) 2013 Christophe Rosset <tophe@topheman.com>
Released under the MIT license:
https://github.com/topheman/SoftLogger/blob/master/LICENSE
/**
*
* @param {Object} options
* @logMaxNumber {Int}
* @fitToCanvas {Boolean}
* @ctxOptions {Object}
* @x {Int}
* @y {Int}
* @lineHeight {Int}
* @fillStyle {Int}
* @font {Int}
* @x {Int}
* @returns {SoftLogger}
*
*/
function SoftLogger(options) {
if (SoftLogger.caller !== SoftLogger.getInstance) {
throw new Error("This object cannot be instanciated");
}
if (!console) {
console = {};
console.log = function() {};
console.info = function() {};
}
options = options ? options : {};
var logMaxNumber,
logs = [],
lastLog = null,
lastLogIteration = 1,
log,
oldConsoleLog,
logging = false,
tmpLogMaxNumber,
canvas, ctxOptions;
//init options
if (options.fitToCanvas) {
if (options.fitToCanvas.nodeName !== 'CANVAS') {
throw new Error('Please pass canvas node via fitToCanvas attribute');
} else {
canvas = options.fitToCanvas;
}
}
ctxOptions = options.ctxOptions ? options.ctxOptions : {};
ctxOptions.x = typeof ctxOptions.x !== 'undefined' ? ctxOptions.x : 10;
ctxOptions.y = typeof ctxOptions.y !== 'undefined' ? ctxOptions.y : 10;
ctxOptions.lineHeight = typeof ctxOptions.lineHeight !== 'undefined' ? ctxOptions.lineHeight : 14;
ctxOptions.fillStyle = typeof ctxOptions.fillStyle !== 'undefined' ? ctxOptions.fillStyle : "black";
ctxOptions.font = typeof ctxOptions.font !== 'undefined' ? ctxOptions.font : "Arial 12px";
//init logMaxNumber
if (canvas) {
tmpLogMaxNumber = ((canvas.height - ctxOptions.y) - (canvas.height - ctxOptions.y) % ctxOptions.lineHeight) / ctxOptions.lineHeight;
if (!options.logMaxNumber || (options.logMaxNumber && options.logMaxNumber > tmpLogMaxNumber)) {
logMaxNumber = tmpLogMaxNumber;
}
} else {
logMaxNumber = options.logMaxNumber > 0 ? options.logMaxNumber : 10;
}
oldConsoleLog = console.log;
log = function() {
var result = '',
i;
//process the arugments to a string
for (i = 0; i < arguments.length; i++) {
if (i > 0) {
result += ', ';
}
result += arguments[i] !== null ? typeof arguments[i] !== 'undefined' ? arguments[i].toString() : 'undefined' : 'null';
}
//compare to the last log and add it to the logs
if (lastLog === result) {
lastLogIteration++;
logs.splice(logs.length - 1, 1);
logs.push(result + '(' + lastLogIteration + ')');
} else {
lastLogIteration = 1;
lastLog = result;
logs.push(result);
}
//crop the logs array if > logsMaxNumber
if (logs.length > logMaxNumber) {
logs.splice(0, 1);
}
};
/**
* Toggles from catching console entries to normal console mode
*/
this.toggleConsole = function() {
//only works if console api is present
if (oldConsoleLog) {
//catch the console entries, redirect them to SoftLogger
if (logging === false) {
console.log = log;
logging = true;
}
//switch back to normal consol logging
else {
console.log = oldConsoleLog;
logging = false;
}
}
};
/**
* Returns an array of the logs of the length logMaxNumber
* @returns {Array}
*/
this.getLogs = function() {
return logs;
};
this.draw = function(ctx) {
ctx.save();
ctx.fillStyle = ctxOptions.fillStyle;
ctx.font = ctxOptions.font;
for (var i = 0; i < logs.length; i++) {
ctx.fillText(logs[i], ctxOptions.x, ctxOptions.y + i * ctxOptions.lineHeight);
}
ctx.restore();
};
//switch on catching console entries
this.toggleConsole();
}
SoftLogger.instance = null;
SoftLogger.getInstance = function(logMaxNumber) {
if (this.instance === null) {
this.instance = new SoftLogger(logMaxNumber);
}
return this.instance;
};