-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
196 lines (167 loc) · 4.76 KB
/
index.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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
'use strict';
const unix_dgram = require('unix-dgram');
const dgram = require('dgram');
let socket;
let socketUsers = 0;
let releaseTimeout;
const socketErrorHandler = () => {
if (socket !== undefined) {
socket.close();
socket = undefined;
socketUsers = 0;
}
};
const getSocket = (type) => {
if (undefined === socket) {
if (type =='udp4') {
socket = dgram.createSocket(type);
}
if (type =='unix_dgram') {
socket = unix_dgram.createSocket(type);
}
if (undefined === socket) {
throw new Error('Unknown socket type: ' + type);
}
socket.on('error', socketErrorHandler);
}
socketUsers++;
return socket;
};
const releaseSocket = () => {
socketUsers--;
if (0 == socketUsers && undefined === releaseTimeout) {
releaseTimeout = setTimeout(() => {
if (0 == socketUsers && socket !== undefined) {
socket.close();
socket = undefined;
}
releaseTimeout = undefined;
}, 1000);
}
};
module.exports = {
Facility: {
kern: 0,
user: 1,
mail: 2,
daemon: 3,
auth: 4,
syslog: 5,
lpr: 6,
news: 7,
uucp: 8,
local0: 16,
local1: 17,
local2: 18,
local3: 19,
local4: 20,
local5: 21,
local6: 22,
local7: 23
},
Priority: {
emerg: 0,
alert: 1,
crit: 2,
err: 3,
warn: 4,
notice: 5,
info: 6,
debug: 7
},
path: null,
address: '127.0.0.1',
port: 514,
encoding: 'utf8',
maxChunkSize: 2000,
chunkSpaceSize: 200,
/**
Set host & port and set to null path to use UDP
@param {string} h
*/
setHost: function (h) {
this.address = h;
},
/**
Set host & port and set to null path to use UDP
@param {number} p
*/
setPort: function (p) {
this.port = p;
},
/**
Set path to not null to use unix socket
@param {string} p
*/
setPath: function (p) {
this.path = p;
},
/**
Write message to syslog, by default UDP to 127.0.0.1:514
@param {string} message
@param {string} facility
@param {string} priority
@throws {Error}
*/
write: function (message, facility, priority) {
if (this.path) {
const preambleBuffer = new Buffer(`<${facility * 8 + priority}> `);
message = Buffer.isBuffer(message) ? message : new Buffer(message);
const chunkSize = this.maxChunkSize - preambleBuffer.length - this.chunkSpaceSize;
const numChunks = Math.ceil(message.length / chunkSize);
const fragments = [preambleBuffer];
if (numChunks > 1) {
for (let i = 0; i < numChunks; i++) {
fragments.push(
message.slice(i * chunkSize, Math.min(message.length, (i + 1) * chunkSize)),
new Buffer(` [${i + 1}/${numChunks}]`, this.encoding)
);
}
} else {
fragments.push(message);
}
const chunk = Buffer.concat(fragments);
try {
getSocket('unix_dgram').send(
chunk,
0,
chunk.length,
this.path,
() => {
releaseSocket();
}
);
} catch (e) {
console.error('node-syslog.js unix_dgram', e);
}
} else {
const chunk = new Buffer(`<${facility * 8 + priority}> ${message}`);
try {
getSocket('udp4').send(
chunk,
0,
chunk.length,
this.port,
this.address,
() => {
releaseSocket();
}
);
} catch(e) {
console.error('node-syslog.js udp4', e);
}
}
}
};
if (process.argv[1] == module.filename) {
/* For test module can be executed: node index.js /dev/log "test message" "facility" "priority" */
module.exports.path = process.argv[2] || '/dev/log';
module.exports.write(process.argv[3], process.argv[4] || 'user', process.argv[5] || 'warn');
if (process.argv[6] == '--debug') {
console.log('wrote to unix socket:', module.exports.path, '\n',
'falility:', process.argv[4] || 'user', '\n',
'priority:', process.argv[5] || 'warn', '\n',
'message:', process.argv[3]
)
}
}