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
};
};
@ -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
msg = "", // the log message; with newline
raw = ""; // no-newlines
// 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
} else {
var delimiter = '\n',
var delimiter = '',
multiLines = false; // if the log need multiples lines (ie. object, array)
// Process arg one by one
args.forEach(function (arg) {
args.forEach(function (arg, index) {
// 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
} 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
};
};
@ -550,32 +564,24 @@
opt : opt
};
//It's time to build the result string
var offsetSpace = "";
// 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);
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;
if (offset < 0) { //context string could be longer than medium size
offset = 0;
}
offsetSpace = new Array(offset + this.opt.spaceSize).join(' ');
}
// newline enabled
log.argsString = built.msg;
// Finally, the message
log.message = log.contextString + offsetSpace + log.argsString;
log.message = log.contextString + log.argsString;
// Emit events
this.emit('new', log, log.type); // 'new' event