Skip to content

Instantly share code, notes, and snippets.

@airandfingers
Created January 22, 2014 18:24
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save airandfingers/8564332 to your computer and use it in GitHub Desktop.
Save airandfingers/8564332 to your computer and use it in GitHub Desktop.
Modified version of unix-dgram's Socket.send function, with winston-syslog's usage of this function for reference
//unix-dgram/src/unix_dgram.js
Socket.prototype.send = function(buf, offset, length, path, callback) {
// FIXME defer error and callback to next tick?
var err;
if (send(this.fd, buf, offset, length, path) == -1) {
err = 'Error with send'; // Replaces this.emit('error', errnoException(errno, 'send'));
}
callback(err); // Always call callback, sometimes with err
};
//winston-syslog/lib/winston-syslog.js, inside Syslog.prototype.log - UNCHANGED
this.connect(function (err) {
if (err) {
//
// If there was an error enqueue the message
//
return self.queue.push(syslogMsg);
}
//
// On any error writing to the socket, enqueue the message
//
function onError (logErr) {
if (logErr) { self.queue.push(syslogMsg) }
self.emit('logged');
self.inFlight--;
}
//
// Write to the `tcp*`, `udp*`, or `unix` socket.
//
if (self.isDgram) {
buffer = new Buffer(syslogMsg);
if (self.protocol.match(/^udp/)) {
self.inFlight++;
self.socket.send(buffer, 0, buffer.length, self.port, self.host, onError);
}
else {
self.socket.send(buffer, 0, buffer.length, self.path, onError);
}
}
else {
self.socket.write(syslogMsg, 'utf8', onError);
}
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment