forked from harriedegroot/nl.hdg.mqtt
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Log.js
140 lines (117 loc) · 3.78 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
const Homey = require('homey');
const logArray = [];
const DEBUG = process.env.DEBUG === '1';
function getDateTime() {
var date = new Date();
var hour = date.getHours();
hour = (hour < 10 ? "0" : "") + hour;
var min = date.getMinutes();
min = (min < 10 ? "0" : "") + min;
var sec = date.getSeconds();
sec = (sec < 10 ? "0" : "") + sec;
var year = date.getFullYear();
var month = date.getMonth() + 1;
month = (month < 10 ? "0" : "") + month;
var day = date.getDate();
day = (day < 10 ? "0" : "") + day;
return year + month + day + "-" + hour + ":" + min + ":" + sec;
}
function getAllFuncs(obj) {
if (!obj) return [];
if (obj instanceof Error) {
console.log("Error object");
console.log(obj.name);
console.log(obj.message);
console.log(obj.stack);
return;
}
var props = [];
do {
props = props.concat(Object.getOwnPropertyNames(obj));
} while (obj = Object.getPrototypeOf(obj));
return props.sort();
}
function writelog(level, line, notification, functions, implementation) {
switch(level) {
case 'error':
const message = typeof line === 'object' ? (line.error_description || line.error || line) : line;
if (notification === true && message && typeof message === 'string') {
Homey.ManagerNotifications.registerNotification({
excerpt: message
}, function (err, notification) {
if (DEBUG) console.log('Notification added');
if (line) return console.error(line);
});
} else {
if (line) return console.error(line);
}
break;
case 'debug':
if (typeof line === 'object') {
if (!DEBUG) break;
let obj = line;
if (!obj) {
this.writelog('info', 'object: UNDEFINED');
return;
}
writelog('info', typeof obj + ' ' + obj.constructor.name);
if (implementation) {
writelog('info', typeof obj + ' ' + obj.constructor);
}
writelog('info', 'PROPERTIES: ' + JSON.stringify(obj, null, 2));
if (functions) {
writelog('info', 'FUNCTIONS:' + JSON.stringify(getAllFuncs(obj), null, 2));
}
break;
}
// NOTE: fall through
case 'info':
var logLine = getDateTime() + " " + line;
console.log( logLine );
if (logArray.length >= 100) {
logArray.pop();
}
logArray.unshift(logLine);
break;
}
}
function getLogLines() {
return logArray;
}
function clearLogLines() {
logArray.length = 0;
}
module.exports = {
LEVELS: ['off', 'error', 'warning', 'info', 'debug'],
level: DEBUG ? 4 : 3,
getLevel: function () {
return this.LEVELS[this.level];
},
setLevel: function (level) {
if (level !== undefined) {
this.level = typeof level === 'string' ? this.LEVELS.indexOf(level) : level;
if (level === 0) {
clearLogLines();
}
}
},
debug: function (line, functions, implementation) {
if(this.level >= 4) writelog('debug', line, null, functions, implementation);
},
info: function (line) {
if (this.level >= 3) writelog('info', line);
},
// wraning...
error: function (line, notification) {
if (this.level >= 1) writelog('error', line, notification);
},
writelog: function (level, line) {
writelog(level, line);
},
getLogLines: function () {
return getLogLines();
},
clearLogLines: function () {
clearLogLines();
}
};