-
Notifications
You must be signed in to change notification settings - Fork 0
/
logconsole.js
65 lines (57 loc) · 1.75 KB
/
logconsole.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
module.exports = function (mod) {
var modTxt = getModTxt(mod);
var obj = {};
obj.log = function () {
var args = prefix().concat([].slice.apply(arguments));
console.log.apply(console, args);
};
obj.error = function () {
var args = prefix().concat(['!!!ERROR'], [].slice.apply(arguments));
console.error.apply(console, args);
}
obj.log('log attached');
return obj;
function prefix() {
var d = new Date();
var time = fmt(d.getHours(), 2) + ':' + fmt(d.getMinutes(), 2) + ':' + fmt(d.getSeconds(), 2) + '.' + fmt(d.getMilliseconds(), 3);
return [time, modTxt, '-'];
}
}
function getModTxt(mod) {
var modTxt;
if (mod.parent) {
var start = commonSubstring(mod.filename, mod.parent.filename);
modTxt = mod.filename.substring(start.length);
var slash = modTxt.lastIndexOf('\\');
if (slash != -1) {
modTxt = modTxt.substring(0, slash);
}
}
else {
modTxt = mod.filename.substring(mod.filename.lastIndexOf('\\') + 1);
}
modTxt = modTxt.replace(/\.js/, '');
return modTxt;
}
function commonSubstring() {
var words = arguments;
var iChar, iWord,
refWord = words[0],
lRefWord = refWord.length,
lWords = words.length;
for (iChar = 0; iChar < lRefWord; iChar += 1) {
for (iWord = 1; iWord < lWords; iWord += 1) {
if (refWord[iChar] !== words[iWord][iChar]) {
return refWord.substring(0, iChar);
}
}
}
return refWord;
}
function fmt(str, cnt) {
str = str.toString();
while (str.length < cnt) {
str = '0' + str;
}
return str;
}