From 81c1c9e61974bf61194fd077f95151cb9002b7fd Mon Sep 17 00:00:00 2001 From: Alec Missine Date: Fri, 10 Aug 2018 15:31:36 -0400 Subject: [PATCH] DEBUG_USE_MICROSECONDS=Y DEBUG=* node ./examples/node/useMicroseconds.js --- binding.gyp | 8 ++++++ examples/node/useMicroseconds.js | 18 ++++++++++++ package.json | 3 ++ src/browser.js | 1 + src/common.js | 2 +- src/index.js | 9 ++++++ src/module.c | 49 ++++++++++++++++++++++++++++++++ src/node.js | 2 ++ 8 files changed, 91 insertions(+), 1 deletion(-) create mode 100644 binding.gyp create mode 100644 examples/node/useMicroseconds.js create mode 100644 src/module.c diff --git a/binding.gyp b/binding.gyp new file mode 100644 index 00000000..1f2fa299 --- /dev/null +++ b/binding.gyp @@ -0,0 +1,8 @@ +{ + "targets": [ + { + "target_name": "module", + "sources": [ "./src/module.c" ] + } + ] +} diff --git a/examples/node/useMicroseconds.js b/examples/node/useMicroseconds.js new file mode 100644 index 00000000..c8fb1c3a --- /dev/null +++ b/examples/node/useMicroseconds.js @@ -0,0 +1,18 @@ +/* + * node-gyp configure build + * + * DEBUG_USE_MICROSECONDS=Y DEBUG=* node ./examples/node/useMicroseconds.js + * DEBUG=* node ./examples/node/useMicroseconds.js + */ + +const debug = require('../../')('use:µs'); + +debug('the ∆ is') +debug('the ∆ is') +debug('the ∆ is') +debug('the ∆ is') +debug('the ∆ is') +debug('the ∆ is') +debug('the ∆ is') +debug('the ∆ is') +setTimeout(() => debug('the ∆ is'), 11000) diff --git a/package.json b/package.json index 9af3874d..c379751c 100644 --- a/package.json +++ b/package.json @@ -11,6 +11,9 @@ "log", "debugger" ], + "scripts": { + "test": "./node_modules/mocha/bin/_mocha" + }, "author": "TJ Holowaychuk ", "contributors": [ "Nathan Rajlich (http://n8.io)", diff --git a/src/browser.js b/src/browser.js index b6d94e17..33ccda3e 100644 --- a/src/browser.js +++ b/src/browser.js @@ -8,6 +8,7 @@ exports.save = save; exports.load = load; exports.useColors = useColors; exports.storage = localstorage(); +exports.now = function () { return +new Date() } /** * Colors. diff --git a/src/common.js b/src/common.js index 61eaf893..7a027d8d 100644 --- a/src/common.js +++ b/src/common.js @@ -73,7 +73,7 @@ module.exports = function setup(env) { var self = debug; // set `diff` timestamp - var curr = +new Date(); + var curr = createDebug.now() var ms = curr - (prevTime || curr); self.diff = ms; self.prev = prevTime; diff --git a/src/index.js b/src/index.js index a8792cc1..69b6de50 100644 --- a/src/index.js +++ b/src/index.js @@ -8,4 +8,13 @@ if (typeof process === 'undefined' || process.type === 'renderer' || process.bro module.exports = require('./browser.js'); } else { module.exports = require('./node.js'); + if (process.env.DEBUG_USE_MICROSECONDS) { + var humanizeMs = module.exports.humanize + module.exports.humanize = delta => { + if (delta > 10000) { + return humanizeMs((delta + 500) / 1000) + } + return `${delta}µs` + } + } } diff --git a/src/module.c b/src/module.c new file mode 100644 index 00000000..37326536 --- /dev/null +++ b/src/module.c @@ -0,0 +1,49 @@ +#include /* n-api */ +#include /* gettimeofday, timeval (for timestamp in microseconds) */ + +long long int now () { + struct timeval t_us; + long long int us; + + if (!gettimeofday(&t_us, NULL)) { + us = ((long long int) t_us.tv_sec) * 1000000ll + + (long long int) t_us.tv_usec; + } + else return -1ll; + + return us; +} + +napi_value MyFunction (napi_env env, napi_callback_info info) { + napi_status status; + + /* Convert to JS Number the current time in microseconds. + */ + long long int number = now(); + napi_value myNumber; + status = napi_create_int64(env, number, &myNumber); + if (status != napi_ok) { + napi_throw_error(env, NULL, "Unable to create return value"); + } + + return myNumber; +} + +napi_value Init (napi_env env, napi_value exports) { + napi_status status; + napi_value fn; + + status = napi_create_function(env, NULL, 0, MyFunction, NULL, &fn); + if (status != napi_ok) { + napi_throw_error(env, NULL, "Unable to wrap native function"); + } + + status = napi_set_named_property(env, exports, "gettimeofday", fn); + if (status != napi_ok) { + napi_throw_error(env, NULL, "Unable to populate exports"); + } + + return exports; +} + +NAPI_MODULE(NODE_GYP_MODULE_NAME, Init) diff --git a/src/node.js b/src/node.js index c13b932c..5cd11e97 100644 --- a/src/node.js +++ b/src/node.js @@ -15,6 +15,8 @@ exports.formatArgs = formatArgs; exports.save = save; exports.load = load; exports.useColors = useColors; +exports.now = process.env.DEBUG_USE_MICROSECONDS ? + require('../build/Release/module').gettimeofday : () => { return +new Date() } /** * Colors.