Skip to content

Commit

Permalink
lots of changes, now using util
Browse files Browse the repository at this point in the history
  • Loading branch information
devgru committed Mar 16, 2011
1 parent 80093d8 commit 796bbb9
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 53 deletions.
125 changes: 73 additions & 52 deletions index.js
@@ -1,78 +1,99 @@
var colors = require('./colors.js');
var sys = require('sys');
var colors = require('./colors.js'),
util = require('util')

require('./date.format.js');
require('./date.format.js')

var Logger = function (context, contextColor) {
Logger.padding = Math.max(Logger.padding, context.length);
Logger.padding = Math.max(Logger.padding, context.length)

var contextColorCode = contextColor ? colors.bold[contextColor] : colors.bold.white;
this.contextColorCode = contextColor ? colors.bold[contextColor] : colors.bold.white

this.doPadding = function () {
var ct = context;
var ct = context
while (ct.length < Logger.padding) {
ct = ct.length % 2 ? ct + ' ' : ' ' + ct;
ct = ct.length % 2 ? ct + ' ' : ' ' + ct
}
context = ct;
this.context = ct;
};
context = ct
this.context = ct
}

this.prepareText = function (text) {
this.doPadding();
return typeof(text) == "object" ? JSON.stringify(text) : text;
};
this.doPadding()
if (typeof text != 'string') text = util.inspect(text, false, 0)
return text
}

this.log = function (text, textColor) {
var last = Logger.lastUsed;
!!last && !!last.id && !this.equals(last) && sys.puts('');//TODO DIRTY
this.rawPrint(colorize(this.prepareText(text), textColor));
Logger.lastUsed = this;
return this;
};

var colorize = function (text, color) { return color + text + colors.reset; };
var last = Logger.lastUsed
!!last && !!last.id && !this.equals(last) && util.puts('') //TODO DIRTY
this.rawPrint(colorize(this.prepareText(text), textColor))
Logger.lastUsed = this
return this
}

var colorize = function (text, color) { return color + text + colors.reset }
this.header = function () {
return block(context, contextColorCode) +
block(new Date().format(this.dateFormat), colors[this.timeColor]);
};
var block = function (text, color) { return '[' + colorize(text, color) + '] '; };
return block(context, this.contextColorCode) +
(this.appendDateTime ? block(new Date().format(this.dateTimeFormat), colors.bold[this.dateTimeColor]) : '')
}

var block = function (text, color) { return '[' + colorize(text, color) + '] ' }

this.rawPrint = function (text) {
console.log(this.header() + text);
};
util.puts(this.header() + text)
}

var that = this

this.info = function (text) { return this.log(text, colors.white); };
this.debug = function (text) { return this.showDebug ? this.log(text, colors.cyan) : this; };
this.error = function (text) { return this.log(text, colors.bold.red); };
this.warn = function (text) { return this.log(text, colors.yellow); };
this.info = function (text) { return that.log(text, colors.white) }
this.debug = function (text) { return that.showDebug ? that.log(text, colors.cyan) : that }
this.err =
this.error = function (text) { return that.log(text, colors.bold.red) }
this.warning =
this.warn = function (text) { return that.log(text, colors.bold.yellow) }

this.subcontext = function (id) {
var mama = new Logger(context, contextColor)
mama.rawPrint = function (text) {
util.print(this.header() + colorize('|' + id + '|', colors.bold.white) + ' ' + text)
}
return mama
}
this.buffered = function (id) {
if (typeof(id) == 'undefined') id = 'buffer';
var mama = new Logger(context, contextColor);
mama.id = id;
mama.oldRawPrint = mama.rawPrint;
if (typeof(id) == 'undefined') id = 'buffer'
var mama = new Logger(context, contextColor)
mama.id = id
mama.line = function() {
Logger.lastUsed = {id: ' '}
return this
}
mama.rawPrint = function (text) {
var last = Logger.lastUsed;
var last = Logger.lastUsed
if (!this.equals(last)) {
sys.print(this.header() + colorize('|' + id + '|', colors.bold.white) + ' ');
util.print(this.header() + colorize('|' + id + '|', colors.bold.white) + ' ')
}
sys.print(text + ' ');
};
return mama;
};
util.print(text + ' ')
}
return mama
}

this.equals = function (logger) {
return !!logger && context == logger.context && (!!this.id == !!logger.id) && (!this.id || this.id == logger.id);
};
return !!logger && context == logger.context && (!!this.id == !!logger.id) && (!this.id || this.id == logger.id)
}

}

Logger.prototype = Logger.default = module.exports.default = {
showDebug: false,
appendDateTime: false,
dateTimeFormat: 'HH:MM:ss dd.mm',
dateTimeColor: 'cyan'
}

this.dateFormat = "HH:MM:ss dd.mm";
this.showDebug = false;
this.appendDateTime = false;
this.timeColor = 'cyan';
};
Logger.padding = 0
Logger.lastUsed = null

Logger.padding = 0;
Logger.lastUsed = null;
module.exports.colors = colors

exports.forContext = function (context, color) { return new Logger(context, color); };
exports.setPadding = function (padding) { Logger.padding = Math.max(padding, Logger.padding); };
module.exports.forContext = function (context, color) { return new Logger(context, color) }
module.exports.setPadding = function (padding) { Logger.padding = Math.max(padding, Logger.padding) }
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "node-ccl",
"version": "0.0.3",
"version": "0.0.4",

"author": "Devgru <git@devg.ru> (http://home.devg.ru)",
"repository": { "type": "git", "url": "http://github.com/devgru/node-ccl.git" },
Expand Down

0 comments on commit 796bbb9

Please sign in to comment.