Skip to content

Commit 108d5f0

Browse files
author
Mathew Kurian
committed
Merge pull request mathew-kurian#38 from guillaumewuip/feature-customTimeLocation
Feature custom time and location
2 parents 5c4e3b6 + 0f8a327 commit 108d5f0

File tree

2 files changed

+30
-10
lines changed

2 files changed

+30
-10
lines changed

examples/console2.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ console.log("A string %s and a number %d", "hello", "123"); //you can use printf
1414
//Time
1515
console.time().log("Print the full time");
1616
console.date().log("Just print the date");
17+
//custom time
18+
console.time((new Date()).setFullYear(1999)).log("Custom time");
1719

1820
//Tags
1921
console.tag("My Tag").log("Add a tag");
@@ -22,6 +24,7 @@ console.tag({msg : 'my-tag', colors : ['red', 'inverse']}).log("Use colors.js co
2224

2325
//File and line number
2426
console.file().log("Print the file and the line of the call");
27+
console.file('myFile.js', 42).log("Custom filename and line");
2528

2629
//Object
2730
console.log({just : 'an object'});

lib/console2.js

Lines changed: 27 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -269,7 +269,7 @@
269269
*
270270
* Log time (full date) ?
271271
*
272-
* @type {Boolean}
272+
* @type {Boolean|Number|String}
273273
*/
274274
this._time = false;
275275

@@ -287,7 +287,7 @@
287287
*
288288
* Should we log filename and line number ?
289289
*
290-
* @type {Boolean}
290+
* @type {Boolean|Object}
291291
*/
292292
this._location = false;
293293

@@ -325,9 +325,11 @@
325325
* Console2.prototype.time
326326
*
327327
* Log the time
328+
*
329+
* @param {String|Number} time Optional time if need to override.
328330
*/
329-
Console2.prototype.time = function () {
330-
this._time = true;
331+
Console2.prototype.time = function (time) {
332+
this._time = time || true;
331333

332334
return this;
333335
};
@@ -362,9 +364,21 @@
362364
* Console2.prototype.file
363365
*
364366
* Log the file name + line
367+
* You could force the filename and line by passing args.
368+
*
369+
* @param {String} file Filename. Optional
370+
* @param {String|Number} line Line. Optional
365371
*/
366-
Console2.prototype.file = Console2.prototype.f = function () {
367-
this._location = true;
372+
Console2.prototype.file = Console2.prototype.f = function (file, line) {
373+
374+
if (file || line) {
375+
this._location = {
376+
filename : file,
377+
line : line
378+
};
379+
} else {
380+
this._location = true;
381+
}
368382

369383
return this;
370384
};
@@ -569,17 +583,20 @@
569583
*/
570584
this[name] = function () {
571585

572-
var location = getLocation();
573-
var time = Date.now();
586+
//use this._location if it's an object (custom location)
587+
//or build the location
588+
var location = (this._location || this.opt.alwaysLocation) === true ? getLocation() : this._location;
589+
590+
var time = (typeof this._time !== 'boolean') ? this._time : Date.now();
574591

575592
// Let's build the log object
576593

577594
var log = {
578595
type : opt.type || name,
579596
show : {
580597
tags : this._tags.length > 0 || this.opt.alwaysTags || opt.defaultTags.length > 0,
581-
location : this._location || this.opt.alwaysLocation,
582-
time : this._time || this.opt.alwaysTime,
598+
location : this._location !== false || this.opt.alwaysLocation,
599+
time : this._time !== false || this.opt.alwaysTime,
583600
date : this._date || this.opt.alwaysDate
584601
},
585602
context : {

0 commit comments

Comments
 (0)