forked from strongloop/modern-syslog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
147 lines (115 loc) · 3.4 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
// Copyright IBM Corp. 2015,2016. All Rights Reserved.
// Node module: @ssimicro/postmodern-syslog
// This file is licensed under the MIT License.
// License text available at https://opensource.org/licenses/MIT
'use strict';
var Writable = require('stream').Writable;
var core = require('./build/Release/core.node');
var inherits = require('util').inherits;
var fmt = require('util').format;
// Direct access to the core binding
exports.core = core;
// Constants
exports.option = core.option;
exports.facility = core.facility;
exports.level = core.level;
// High-level API, remove the redundant 'log' from method names.
exports.version = require('./package.json').version;
exports.open = open;
exports.init = exports.open;
exports.log = log;
exports.upto = upto;
exports.curmask = curmask;
exports.setMask = setMask;
exports.close = core.closelog;
exports.Stream = Stream;
function open(ident, option, facility) {
// XXX(sam) would be nice to allow single strings for option
core.openlog(ident, option, toFacility(facility));
}
function log(level, msg, callback) {
core.syslog(toLevel(level), msg, callback);
}
function wrap(name, level) {
level = core.level[level];
exports[name] = function(msg) {
core.syslog(level, fmt.apply(null, arguments));
};
}
wrap('emerg', 'LOG_EMERG');
wrap('alert', 'LOG_ALERT');
wrap('crit', 'LOG_CRIT');
wrap('error', 'LOG_ERR');
wrap('err', 'LOG_ERR');
wrap('warn', 'LOG_WARNING');
wrap('warning', 'LOG_WARNING');
wrap('note', 'LOG_NOTICE');
wrap('notice', 'LOG_NOTICE');
wrap('info', 'LOG_INFO');
wrap('debug', 'LOG_DEBUG');
// Low-level API
exports.setmask = core.setlogmask;
exports.toLevel = toLevel;
exports.toFacility = toFacility;
exports.logMask = logMask;
exports.logUpto = logUpto;
// Invert constants, so its easy to look the string up by the value.
// Expose keys globally, for backwards compatibility with node-syslog.
function expose(obj) {
for (var key in obj) {
var val = obj[key];
exports[key] = val;
obj[val] = key;
}
}
expose(exports.option);
expose(exports.facility);
expose(exports.level);
// setlogmask() is too painful to use, most systems have a LOG_UPTO(level)
// macro, we'll just export upto() directly, its what most users will want.
function upto(level) {
return core.setlogmask(logUpto(level));
}
// Linux allows calling setmask(0) to get the current mask, but OS X does not,
// so make a curmask() to smooth this over.
function curmask() {
var cur = core.setlogmask(0);
core.setlogmask(cur);
return cur;
}
function setMask(level, upto) {
var mask;
if (upto)
mask = logUpto(level);
else
mask = logMask(level);
core.setlogmask(mask);
}
// Writable stream for syslog.
function Stream(level, facility) {
if (!(this instanceof Stream))
return new Stream(level, facility);
this.priority = toLevel(level) | toFacility(facility);
Writable.apply(this);
}
inherits(Stream, Writable);
Stream.prototype._write = function(chunk, encoding, callback) {
core.syslog(this.priority, chunk, callback);
};
// Low-level API
function toLevel(level) {
if (typeof level === 'string' && level in core.level)
return core.level[level];
return level;
}
function toFacility(facility) {
if (typeof facility === 'string' && facility in core.facility)
return core.facility[facility];
return facility;
}
function logMask(level) {
return 1 << toLevel(level);
}
function logUpto(level) {
return (1 << (toLevel(level) + 1)) - 1;
}