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; length = 0;
tags.forEach(function (tag) { 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); result += applyColors('[' + tag.msg + ']', tag.colors);
length += ("" + tag.msg).length + 2; length += tag.msg.toString().length + 2;
} else { } else {
result += '[' + tag + ']'; result += '[' + tag + ']';
length += ("" + tag).length + 2; length += tag.toString().length + 2;
} }
}); });
@ -116,16 +117,12 @@
* @return {int} msgLength * @return {int} msgLength
*/ */
var buildFileInfos = function (infos, fileColors, lineColors) { var buildFileInfos = function (infos, fileColors, lineColors) {
var result = '[' +
applyColors(infos.filename, fileColors) +
':' +
applyColors(infos.line, lineColors) +
']';
var length = ('[' + infos.filename + ':' + infos.line + ']').length;
return { return {
msg : result, msg : '[' + applyColors(infos.filename, fileColors) + ':' +
msgLength : length applyColors(infos.line, lineColors) + ']',
msgLength : (infos.filename.toString() +
infos.line.toString()).length + 3
}; };
}; };
@ -345,6 +342,14 @@
return this; 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 * Console2.prototype.buildArgs
* *
@ -354,50 +359,59 @@
* @param {Object} log * @param {Object} log
* @return {String} the string * @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 var args = Array.prototype.slice.call(log.args, 0), // transform args in an array
msg = ""; //the log message 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)) { 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 { } else {
var delimiter = '\n', var delimiter = '',
multiLines = false; // if the log need multiples lines (ie. object, array) multiLines = false; // if the log need multiples lines (ie. object, array)
// Process arg one by one // Process arg one by one
args.forEach(function (arg) { args.forEach(function (arg, index) {
// if arg is an object / array // if arg is an object / array
// use multiples lines // use multiples lines
if (arg !== null && typeof arg === 'object') { if (arg !== null && typeof arg === 'object') {
msg += delimiter; msg += delimiter + JSON.stringify(arg, null, 2);
msg += JSON.stringify(arg, null, 2);
multiLines = true; multiLines = true;
// for "normal" args // for "normal" args
} else { } else {
if (multiLines) { if (multiLines) {
msg += delimiter; msg += delimiter;
} }
msg += arg + "";
msg += arg.toString();
}
if(index === 0){
delimiter = '\n';
} }
}); });
msg = getSpaces(offset) + msg;
if (multiLines) { if (multiLines) {
msg += '\n'; msg = msg.replace(/\n/gm, '\n' + context + getSpaces(offset));
msg = msg.replace(/\n/gm, '\n' + new Array(this.opt.spaceSize).join(' '));
} }
} }
return msg; return {
msg : msg,
raw : raw
};
}; };
@ -550,32 +564,24 @@
opt : opt 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); 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; log.contextString = context.result;
// Build the args string // Build the args string
log.argsString = this.buildArgs(log); var built = this.buildArgs(log, offset, log.contextString);
//Generate the according number of space between context and args strings // newline enabled
if (context.length > 0) { //if there is context string log.argsString = built.msg;
//add space according to the contextMediumSize
var offset = this.opt.contextMediumSize - context.length;
if (offset < 0) { //context string could be longer than medium size
offset = 0;
}
offsetSpace = new Array(offset + this.opt.spaceSize).join(' ');
}
// Finally, the message // Finally, the message
log.message = log.contextString + offsetSpace + log.argsString; log.message = log.contextString + log.argsString;
// Emit events // Emit events
this.emit('new', log, log.type); // 'new' event this.emit('new', log, log.type); // 'new' event