Permalink
Browse files

[api] add query and stream.

  • Loading branch information...
1 parent 235cc19 commit c0875a98416ffb43e658ee04fe5af3e00b6757bd @chjj chjj committed May 10, 2012
Showing with 75 additions and 4 deletions.
  1. +75 −4 lib/winston-nssocket.js
View
79 lib/winston-nssocket.js
@@ -1,8 +1,15 @@
var util = require('util'),
- winston = require('winston'),
- nssocket = require('nssocket');
+ winston = require('../../winston'),
+ nssocket = require('nssocket')
+ Stream = require('stream').Stream;
-var NsSocket = exports.NsSocket = function (options) {
+//
+// ### function NsSocket (options)
+// #### @options {Object} Options for this instance.
+// Constructor function for the NsSocket transport object responsible
+// for persisting log messages and metadata to a terminal or TTY.
+//
+var NsSocket = exports.Nssocket = function (options) {
options = options || {};
this.name = 'nssocket';
@@ -13,8 +20,27 @@ var NsSocket = exports.NsSocket = function (options) {
});
this.socket.connect(options.host, options.port);
};
+
util.inherits(NsSocket, winston.Transport);
+NsSocket.uid = 0;
+
+//
+// ### function close ()
+// Closes the stream associated with this instance.
+//
+NsSocket.prototype.close = function () {
+ this.socket.destroy();
+};
+
+//
+// ### function log (level, msg, [meta], callback)
+// #### @level {string} Level at which to log the message.
+// #### @msg {string} Message to log
+// #### @meta {Object} **Optional** Additional metadata to attach
+// #### @callback {function} Continuation to respond to when complete.
+// Core logging method exposed to Winston. Metadata is optional.
+//
NsSocket.prototype.log = function (level, msg, meta, callback) {
var self = this;
@@ -31,9 +57,54 @@ NsSocket.prototype.log = function (level, msg, meta, callback) {
if (err) {
return self.emit('error', err);
}
-
+
self.emit('logged');
callback && callback();
});
};
+//
+// ### function query ()
+// Query the transport.
+//
+NsSocket.prototype.query = function (options, callback) {
+ var self = this,
+ options = options || {};
+
+ var id = NsSocket.uid++ + '';
+
+ this.socket.send(['query', id], options);
+
+ this.socket.dataOnce(['result', id], function (log) {
+ if (callback) callback(null, log);
+ });
+
+ this.socket.dataOnce(['error', id], function (err) {
+ if (callback) callback(err);
+ });
+};
+
+
+//
+// ### function stream ()
+// Return a log stream.
+//
+NsSocket.prototype.stream = function (options) {
+ var self = this,
+ options = options || {},
+ stream = new Stream;
+
+ stream.destroy = function () {
+ self.socket.undata(['log'], onData);
+ };
+
+ this.socket.send(['stream'], options);
+
+ this.socket.data(['log'], onData);
+
+ function onData(log) {
+ stream.emit('log', log);
+ }
+
+ return stream;
+};

0 comments on commit c0875a9

Please sign in to comment.