-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathutil.js
More file actions
67 lines (45 loc) · 1.47 KB
/
util.js
File metadata and controls
67 lines (45 loc) · 1.47 KB
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
var util = require('util');
var ERROR = 'error';
var WARN = 'warning';
var INFO = 'info';
var DEBUG = 'debug';
var LOG_TYPE_SET = {};
LOG_TYPE_SET[ERROR] = 'error';
LOG_TYPE_SET[WARN] = 'warn';
LOG_TYPE_SET[INFO] = 'info';
LOG_TYPE_SET[DEBUG] = 'debug';
function getObjectClass(obj) {
if(obj && obj.constructor && obj.constructor.toString) {
var arr = obj.constructor.toString().match(
/function\s*(\w+)/);
if(arr & arr.length == 2) {
return arr[1];
}
}
return undefined;
}
function debugPrint(arg, depth) {
var type = typeof(arg);
if(type == 'object') {
var obj = eval(arg);
arg = util.inspect(arg, {showHidden: true, depth: depth, colors: true});
type = getObjectClass(obj);
}
console.log('['+typeof(arg)+']'+arg.toString());
}
var logger = function (type, msg) {
if(!LOG_TYPE_SET.hasOwnProperty(type)) {
console.error('[error] invalid logging type: ' + type);
throw 'invalid logging type';
}
var now = new Date();
var timestamp = now.getUTCMonth()+'-'+now.getUTCDate()+'-'+now.getUTCFullYear()+' '+now.getUTCHours()+':'+now.getUTCMinutes()+':'+now.getUTCSeconds()+'.'+now.getUTCMilliseconds()
console.log('['+type+']'+'('+timestamp+') '+ msg);
return;
}
exports.logger = logger;
exports.debugPrint = debugPrint;
exports.ERROR = ERROR;
exports.WARN = WARN;
exports.INFO = INFO;
exports.DEBUG = DEBUG;