Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

DEBUG_USE_MICROSECONDS=Y DEBUG=* node ./examples/node/useMicroseconds.js #594

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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