forked from mirrors/Scribe.js
Add printf format compatibility
This commit is contained in:
parent
2c53de8ee6
commit
e11d2e1bd9
2 changed files with 58 additions and 25 deletions
|
@ -25,3 +25,5 @@ console.log(
|
|||
);
|
||||
|
||||
console.tag("Combo!").time().file().log("A combo");
|
||||
|
||||
console.log("A string %s and a number %d", "hello", "123"); //you can you printf-like format
|
||||
|
|
|
@ -74,6 +74,26 @@
|
|||
return '[' + infos.filename + ':' + infos.line + ']';
|
||||
};
|
||||
|
||||
/**
|
||||
* areAllStringOrNumber
|
||||
*
|
||||
* Check in an array contains only string and number
|
||||
*
|
||||
* @param {Array} arr
|
||||
* @retrun {Boolean}
|
||||
*/
|
||||
var areAllStringOrNumber = function (arr) {
|
||||
|
||||
arr.forEach(function (elem) {
|
||||
if (typeof elem !== 'string' && typeof elem !== 'number') {
|
||||
return false;
|
||||
}
|
||||
});
|
||||
|
||||
return true;
|
||||
|
||||
};
|
||||
|
||||
/**
|
||||
* applyColors
|
||||
*
|
||||
|
@ -194,7 +214,7 @@
|
|||
this._tags = [];
|
||||
this._time = false;
|
||||
this._date = false;
|
||||
this.location = false;
|
||||
this._location = false;
|
||||
|
||||
return this;
|
||||
};
|
||||
|
@ -234,7 +254,9 @@
|
|||
* Console2.prototype.tag
|
||||
*
|
||||
* Add tags
|
||||
* @param {*} tag
|
||||
* @param {String|Object} tag
|
||||
* @param {String} tag.msg The tag
|
||||
* @paral {String|Array} tag.color colors.js colors
|
||||
*/
|
||||
Console2.prototype.tag = Console2.prototype.t = function () {
|
||||
var tags = Array.prototype.slice.call(arguments, 0);
|
||||
|
@ -311,35 +333,44 @@
|
|||
//It's time to log the args
|
||||
|
||||
var args = Array.prototype.slice.call(log.args, 0), //transform args in an array
|
||||
msg = "", //the log message
|
||||
delimiter = '\n\n',
|
||||
multiLines = false; //if the log need multiples lines (ie. object, array)
|
||||
msg = ""; //the log message
|
||||
|
||||
//Process arg one by one
|
||||
args.forEach(function (arg) {
|
||||
//if all args are string or number, format args as usual
|
||||
if (areAllStringOrNumber(args)) {
|
||||
msg = util.format.apply(this, args);
|
||||
|
||||
//if objects or array present
|
||||
} else {
|
||||
|
||||
//if arg is an object / array
|
||||
//use multiples lines
|
||||
if (arg !== null && typeof arg === 'object') {
|
||||
var delimiter = '\n\n',
|
||||
multiLines = false; //if the log need multiples lines (ie. object, array)
|
||||
|
||||
msg += delimiter;
|
||||
|
||||
msg += JSON.stringify(arg, null, 2);
|
||||
|
||||
multiLines = true;
|
||||
|
||||
//for "normal" args
|
||||
} else {
|
||||
if (multiLines) {
|
||||
//Process arg one by one
|
||||
args.forEach(function (arg) {
|
||||
|
||||
//if arg is an object / array
|
||||
//use multiples lines
|
||||
if (arg !== null && typeof arg === 'object') {
|
||||
|
||||
msg += delimiter;
|
||||
|
||||
msg += JSON.stringify(arg, null, 2);
|
||||
|
||||
multiLines = true;
|
||||
|
||||
//for "normal" args
|
||||
} else {
|
||||
if (multiLines) {
|
||||
msg += delimiter;
|
||||
}
|
||||
msg += arg + "";
|
||||
}
|
||||
msg += arg + "";
|
||||
});
|
||||
|
||||
if (multiLines) {
|
||||
msg += '\n';
|
||||
msg = msg.replace(/\n/gm, '\n' + new Array(this.opt.spaceSize).join(' '));
|
||||
}
|
||||
});
|
||||
|
||||
if (multiLines) {
|
||||
msg += '\n';
|
||||
msg = msg.replace(/\n/gm, '\n' + new Array(this.opt.spaceSize).join(' '));
|
||||
}
|
||||
|
||||
result += msg;
|
||||
|
|
Loading…
Add table
Reference in a new issue