From 30c21bed7f8ba50d582f520245c4442cbf0c4df7 Mon Sep 17 00:00:00 2001 From: cheshirecatalyst Date: Sun, 28 Jun 2015 23:47:03 +0700 Subject: [PATCH] node: allow msDiff disable --- Readme.md | 2 +- dist/debug.js | 23 +++++++++-------------- node.js | 21 +++++++++++++++++---- package.json | 2 +- 4 files changed, 28 insertions(+), 20 deletions(-) diff --git a/Readme.md b/Readme.md index b4f45e3c..fdc9354c 100644 --- a/Readme.md +++ b/Readme.md @@ -63,7 +63,7 @@ Then, run the program to be debugged as usual. ## Millisecond diff - When actively developing an application it can be useful to see when the time spent between one `debug()` call and the next. Suppose for example you invoke `debug()` before requesting a resource, and after as well, the "+NNNms" will show you how much time was spent between calls. + When actively developing an application it can be useful to see when the time spent between one `debug()` call and the next. Suppose for example you invoke `debug()` before requesting a resource, and after as well, the "+NNNms" will show you how much time was spent between calls. Can be disabled with `DEBUG_DIFF=0` ![](http://f.cl.ly/items/2i3h1d3t121M2Z1A3Q0N/Screenshot.png) diff --git a/dist/debug.js b/dist/debug.js index 97d6f787..92393068 100644 --- a/dist/debug.js +++ b/dist/debug.js @@ -238,6 +238,8 @@ module.exports = function(val, options){ */ function parse(str) { + str = '' + str; + if (str.length > 10000) return; var match = /^((?:\d+)?\.?\d+) *(milliseconds?|msecs?|ms|seconds?|secs?|s|minutes?|mins?|m|hours?|hrs?|h|days?|d|years?|yrs?|y)?$/i.exec(str); if (!match) return; var n = parseFloat(match[1]); @@ -336,17 +338,10 @@ exports.formatArgs = formatArgs; exports.save = save; exports.load = load; exports.useColors = useColors; - -/** - * Use chrome.storage.local if we are in an app - */ - -var storage; - -if (typeof chrome !== 'undefined' && typeof chrome.storage !== 'undefined') - storage = chrome.storage.local; -else - storage = localstorage(); +exports.storage = 'undefined' != typeof chrome + && 'undefined' != typeof chrome.storage + ? chrome.storage.local + : localstorage(); /** * Colors. @@ -454,9 +449,9 @@ function log() { function save(namespaces) { try { if (null == namespaces) { - storage.removeItem('debug'); + exports.storage.removeItem('debug'); } else { - storage.debug = namespaces; + exports.storage.debug = namespaces; } } catch(e) {} } @@ -471,7 +466,7 @@ function save(namespaces) { function load() { var r; try { - r = storage.debug; + r = exports.storage.debug; } catch(e) {} return r; } diff --git a/node.js b/node.js index 1d392a81..e361486c 100644 --- a/node.js +++ b/node.js @@ -18,6 +18,7 @@ exports.formatArgs = formatArgs; exports.save = save; exports.load = load; exports.useColors = useColors; +exports.useDiff = useDiff; /** * Colors. @@ -53,6 +54,18 @@ function useColors() { } } +/** + * Allow disabling ms diff, enabled by default. + */ + +function useDiff() { + var diff = (process.env.DEBUG_DIFF || '').trim().toLowerCase(); + return '0' !== diff + && 'no' !== diff + && 'false' !== diff + && 'disabled' !== diff; +} + /** * Map %o to `util.inspect()`, since Node doesn't do that out of the box. */ @@ -86,14 +99,14 @@ function formatArgs() { if (useColors) { var c = this.color; - + var timeStamp = useDiff() ? ' +' + exports.humanize(this.diff) : '' args[0] = ' \u001b[3' + c + ';1m' + name + ' ' + '\u001b[0m' + args[0] + '\u001b[3' + c + 'm' - + ' +' + exports.humanize(this.diff) + '\u001b[0m'; + + timeStamp + '\u001b[0m'; } else { - args[0] = new Date().toUTCString() - + ' ' + name + ' ' + args[0]; + var timeStamp = useDiff() ? new Date().toUTCString() + ' ' : '' + args[0] = timeStamp + name + ' ' + args[0]; } return args; } diff --git a/package.json b/package.json index b9f033a7..f9f1a9b2 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "debug", - "version": "2.2.0", + "version": "2.3.0", "repository": { "type": "git", "url": "git://github.com/visionmedia/debug.git"