Skip to content

Commit

Permalink
DEBUG_USE_MICROSECONDS=Y DEBUG=* node ./examples/node/useMicroseconds.js
Browse files Browse the repository at this point in the history
  • Loading branch information
amissine committed Aug 10, 2018
1 parent 207a6a2 commit 81c1c9e
Show file tree
Hide file tree
Showing 8 changed files with 91 additions and 1 deletion.
8 changes: 8 additions & 0 deletions binding.gyp
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
{
"targets": [
{
"target_name": "module",
"sources": [ "./src/module.c" ]
}
]
}
18 changes: 18 additions & 0 deletions examples/node/useMicroseconds.js
Original file line number Diff line number Diff line change
@@ -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)
3 changes: 3 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
"log",
"debugger"
],
"scripts": {
"test": "./node_modules/mocha/bin/_mocha"
},
"author": "TJ Holowaychuk <tj@vision-media.ca>",
"contributors": [
"Nathan Rajlich <nathan@tootallnate.net> (http://n8.io)",
Expand Down
1 change: 1 addition & 0 deletions src/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ exports.save = save;
exports.load = load;
exports.useColors = useColors;
exports.storage = localstorage();
exports.now = function () { return +new Date() }

/**
* Colors.
Expand Down
2 changes: 1 addition & 1 deletion src/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
9 changes: 9 additions & 0 deletions src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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`
}
}
}
49 changes: 49 additions & 0 deletions src/module.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#include <node_api.h> /* n-api */
#include <sys/time.h> /* 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)
2 changes: 2 additions & 0 deletions src/node.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down

0 comments on commit 81c1c9e

Please sign in to comment.