Adding multiline tags

This commit is contained in:
Mathew Kurian 2014-12-21 21:13:27 -06:00
parent 41a815a764
commit 21e3342d01

View file

@ -86,12 +86,13 @@
length = 0;
tags.forEach(function (tag) {
if (tag !== null && typeof tag === 'object') {
if(typeof tag === "undefined" || tag === null) return;
if (typeof tag === 'object') {
result += applyColors('[' + tag.msg + ']', tag.colors);
length += ("" + tag.msg).length + 2;
length += tag.msg.toString().length + 2;
} else {
result += '[' + tag + ']';
length += ("" + tag).length + 2;
length += tag.toString().length + 2;
}
});
@ -116,16 +117,12 @@
* @return {int} msgLength
*/
var buildFileInfos = function (infos, fileColors, lineColors) {
var result = '[' +
applyColors(infos.filename, fileColors) +
':' +
applyColors(infos.line, lineColors) +
']';
var length = ('[' + infos.filename + ':' + infos.line + ']').length;
return {
msg : result,
msgLength : length
msg : '[' + applyColors(infos.filename, fileColors) + ':' +
applyColors(infos.line, lineColors) + ']',
msgLength : (infos.filename.toString() +
infos.line.toString()).length + 3
};
};
@ -293,7 +290,7 @@
//inherits form EventEmitter.prototype
// inherits form EventEmitter.prototype
util.inherits(Console2, EventEmitter);
@ -345,6 +342,14 @@
return this;
};
var spaceLUT = {};
var getSpaces = function(offset){
if(typeof spaceLUT[offset] === "undefined"){
spaceLUT[offset] = new Array(offset).join(' ');
}
return spaceLUT[offset];
}
/**
* Console2.prototype.buildArgs
*
@ -354,50 +359,59 @@
* @param {Object} log
* @return {String} the string
*/
Console2.prototype.buildArgs = function (log) {
Console2.prototype.buildArgs = function (log, offset, context) {
var args = Array.prototype.slice.call(log.args, 0), //transform args in an array
msg = ""; //the log message
var args = Array.prototype.slice.call(log.args, 0), // transform args in an array
msg = "", // the log message; with newline
raw = ""; // no-newlines
//if all args are string or number, format args as usual
// if all args are string or number, format args as usual
if (areAllStringOrNumber(args)) {
msg = util.format.apply(this, args);
raw = util.format.apply(util, args);
msg = getSpaces(offset) + raw;
//if objects or array present
// if objects or array present
} else {
var delimiter = '\n',
multiLines = false; //if the log need multiples lines (ie. object, array)
var delimiter = '',
multiLines = false; // if the log need multiples lines (ie. object, array)
//Process arg one by one
args.forEach(function (arg) {
// Process arg one by one
args.forEach(function (arg, index) {
//if arg is an object / array
//use multiples lines
// if arg is an object / array
// use multiples lines
if (arg !== null && typeof arg === 'object') {
msg += delimiter;
msg += JSON.stringify(arg, null, 2);
msg += delimiter + JSON.stringify(arg, null, 2);
multiLines = true;
//for "normal" args
// for "normal" args
} else {
if (multiLines) {
msg += delimiter;
}
msg += arg + "";
msg += arg.toString();
}
if(index === 0){
delimiter = '\n';
}
});
msg = getSpaces(offset) + msg;
if (multiLines) {
msg += '\n';
msg = msg.replace(/\n/gm, '\n' + new Array(this.opt.spaceSize).join(' '));
msg = msg.replace(/\n/gm, '\n' + context + getSpaces(offset));
}
}
return msg;
return {
msg : msg,
raw : raw
};
};
@ -430,10 +444,10 @@
opt.time = opt.time === true;
opt.date = opt.date === true;
var result = "", //final output
space = " ", //space between context parts
length = 0; //length of the context part (human readable string)
//ie. without escapted or colors caracters
var result = "", // final output
space = " ", // space between context parts
length = 0; // length of the context part (human readable string)
// ie. without escapted or colors caracters
if (opt.tags && log.context.tags) {
var tags = buildTags(log.context.tags);
@ -477,7 +491,7 @@
* @param {String} name The name of the logger.
* @param {Array|String} colors Optional colorsjs colors of the console output.
* Override constructor default.
* See text colors from https://github.com/Marak/colors.js
* See text colors from https:// github.com/Marak/colors.js
*
* @param {Object} opt Optional options object. @see Console2 opt for default values.
*
@ -530,7 +544,7 @@
var location = getLocation();
var time = Date.now();
//Let's build the log object
// Let's build the log object
var log = {
type : opt.type || name,
@ -550,35 +564,27 @@
opt : opt
};
//It's time to build the result string
var offsetSpace = "";
//Build the context string
// Build the context string
var context = this.buildContext(log, log.show);
// Generate the according number of space between context and args strings
// add space according to the contextMediumSize
var offset = Math.max(0, this.opt.contextMediumSize - context.length);
// save context
log.contextString = context.result;
//Build the args string
log.argsString = this.buildArgs(log);
// Build the args string
var built = this.buildArgs(log, offset, log.contextString);
//Generate the according number of space between context and args strings
if (context.length > 0) { //if there is context string
//add space according to the contextMediumSize
var offset = this.opt.contextMediumSize - context.length;
// newline enabled
log.argsString = built.msg;
if (offset < 0) { //context string could be longer than medium size
offset = 0;
}
// Finally, the message
log.message = log.contextString + log.argsString;
offsetSpace = new Array(offset + this.opt.spaceSize).join(' ');
}
//Finally, the message
log.message = log.contextString + offsetSpace + log.argsString;
//Emit events
this.emit('new', log, log.type); //'new' event
// Emit events
this.emit('new', log, log.type); // 'new' event
var msg = log.type;
@ -590,10 +596,10 @@
if (msg === 'error') {
msg += 'Event';
}
this.emit(msg, log); //`log.type` event
this.emit(msg, log); // `log.type` event
//If the logger should print the message
//Print it
// If the logger should print the message
// Print it
if (opt.logInConsole) {
global.console.log(applyColors(log.message, opt.colors));
}
@ -609,8 +615,8 @@
};
//Keep the old console
//use also `global.console`
// Keep the old console
// use also `global.console`
Console2.prototype.Original = consoleOriginal;
module.exports = Console2;