forked from iizukanao/node-rtsp-rtmp-server
-
Notifications
You must be signed in to change notification settings - Fork 1
/
logger.js
120 lines (97 loc) · 2.46 KB
/
logger.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
/*
* Usage
logger = require './logger'
* Set log level to filter out unwanted log messages
logger.setLevel logger.LEVEL_INFO
logger.debug 'debug message'
logger.info 'info message'
logger.warn 'warn message'
logger.error 'error message'
logger.fatal 'fatal message'
* Enable a tag to activate log messages for the tag
logger.enableTag 'testtag'
logger.tag 'testtag', 'testtag message'
logger.tag 'anothertag', 'anothertag message'
* Print raw string. Equivalent of console.log().
logger.raw "hello\nraw\nstring"
*/
// Current log level
let logLevel = null;
let activeTags = {};
let zeropad = function(columns, num) {
num += '';
while (num.length < columns) {
num = `0${num}`;
}
return num;
};
var api = {
LEVEL_DEBUG: 0,
LEVEL_INFO: 1,
LEVEL_WARN: 2,
LEVEL_ERROR: 3,
LEVEL_FATAL: 4,
LEVEL_OFF: 5,
enableTag(tag) {
return activeTags[tag] = true;
},
disableTag(tag) {
return delete activeTags[tag];
},
print(str, raw) {
if (raw == null) { raw = false; }
if (!raw) {
let d = new Date();
process.stdout.write(`${d.getFullYear()}-${zeropad(2, d.getMonth()+1)}-` +
`${zeropad(2, d.getDate())} ${zeropad(2, d.getHours())}:` +
`${zeropad(2, d.getMinutes())}:${zeropad(2, d.getSeconds())}.` +
`${zeropad(3, d.getMilliseconds())} `
);
}
return console.log(str);
},
tag(tag, str, raw) {
if (raw == null) { raw = false; }
if (activeTags[tag] != null) {
return api.print(str, raw);
}
},
msg(level, str, raw) {
if (raw == null) { raw = false; }
if (level >= logLevel) {
return api.print(str, raw);
}
},
// Prints message without header
raw(str) {
return api.print(str, true);
},
setLevel(level) {
return logLevel = level;
},
getLevel() {
return logLevel;
},
debug(str, raw) {
if (raw == null) { raw = false; }
return api.msg(api.LEVEL_DEBUG, str, raw);
},
info(str, raw) {
if (raw == null) { raw = false; }
return api.msg(api.LEVEL_INFO, str, raw);
},
warn(str, raw) {
if (raw == null) { raw = false; }
return api.msg(api.LEVEL_WARN, str, raw);
},
error(str, raw) {
if (raw == null) { raw = false; }
return api.msg(api.LEVEL_ERROR, str, raw);
},
fatal(str, raw) {
if (raw == null) { raw = false; }
return api.msg(api.LEVEL_FATAL, str, raw);
}
};
logLevel = api.LEVEL_INFO; // default verbosity
export default api;