add log method to program object.

This commit is contained in:
Christopher Jeffrey 2013-07-31 00:14:13 -05:00
parent 2e58aed1ea
commit 51771dc19a
3 changed files with 45 additions and 13 deletions

View File

@ -153,6 +153,10 @@ The screen on which every other node renders.
- **tabSize** - the width of tabs within an element's content.
- **autoPadding** - automatically position child elements with border and
padding in mind.
- **log** - create a log file. see `log` method.
- **dump** - dump all output and input to desired file. can be used together
with `log` option if set as a boolean.
- **debug** - debug mode. enables usage of the `debug` method.
##### Properties:
@ -190,6 +194,9 @@ The screen on which every other node renders.
##### Methods:
- inherits all from Node.
- **log(msg, ...)** - write string to the log file if one was created.
- **debug(msg, ...)** - same as the log method, but only gets called if the
`debug` option was set.
- **alloc()** - allocate a new pending screen buffer and a new output screen
buffer.
- **draw(start, end)** - draw the screen based on the contents of the screen

View File

@ -9,6 +9,7 @@
*/
var EventEmitter = require('events').EventEmitter
, util = require('util')
, Tput = require('./tput')
, colors = require('./colors')
, slice = Array.prototype.slice;
@ -41,8 +42,10 @@ function Program(options) {
this.input = options.input || process.stdin;
this.output = options.output || process.stdout;
if (options.dump) {
this.dump(options.dump);
options.log = options.log || options.dump;
if (options.log) {
this._logger = require('fs').createWriteStream(options.log);
if (options.dump) this.setupDump();
}
this.zero = options.zero !== false;
@ -76,12 +79,26 @@ function Program(options) {
Program.prototype.__proto__ = EventEmitter.prototype;
Program.prototype.dump = function(file) {
var self = this
, _write = this.output.write
, decoder = new (require('string_decoder')).StringDecoder('utf8');
Program.prototype.log = function() {
return this._log('LOG', util.format.apply(util, arguments));
};
this.logger = require('fs').createWriteStream(file);
Program.prototype.debug = function() {
if (!this.options.debug) return;
return this._log('DEBUG', util.format.apply(util, arguments));
};
Program.prototype._log = function(pre, msg) {
if (!this._logger) return;
return this._logger.write(pre + ': ' + msg + '\n-\n');
};
Program.prototype.setupDump = function() {
var self = this
, write = this.output.write
, decoder;
decoder = new (require('string_decoder')).StringDecoder('utf8');
function stringify(data) {
return caret(data
@ -139,14 +156,14 @@ Program.prototype.dump = function(file) {
});
}
this.output.write = function(data) {
self.logger.write('OUT: ' + stringify(data) + '\n-\n');
return _write.apply(this, arguments);
};
this.input.on('data', function(data) {
self.logger.write('IN: ' + stringify(decoder.write(data)) + '\n-\n');
self._log('IN', stringify(decoder.write(data)));
});
this.output.write = function(data) {
self._log('OUT', stringify(data));
return write.apply(this, arguments);
};
};
Program.prototype.setupTput = function() {

View File

@ -414,6 +414,14 @@ Screen.prototype.__proto__ = Node.prototype;
Screen.prototype.type = 'screen';
Screen.prototype.log = function() {
return this.program.log.apply(this.program, arguments);
};
Screen.prototype.debug = function() {
return this.program.debug.apply(this.program, arguments);
};
// TODO: Bubble and capture events throughout the tree.
Screen.prototype._listenMouse = function(el) {
var self = this;