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

Create flags to enable logs when call methods with fibers is in use. #461

Merged
merged 5 commits into from
Aug 9, 2022
Merged
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
37 changes: 31 additions & 6 deletions fibers.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,21 +85,46 @@ function setupAsyncHacks(Fiber) {
}
}

function wrapFunction(fn) {
return function() {
function logUsingFibers(fibersMethod) {
const logUseFibersLevel = +(process.env.ENABLE_LOG_USE_FIBERS || 0);

if (!logUseFibersLevel) return;

if (logUseFibersLevel === 1) {
console.warn(`[FIBERS_LOG] Using ${fibersMethod}.`);
return;
}

const { LOG_USE_FIBERS_INCLUDE_IN_PATH } = process.env;
const stackFromError = new Error(`[FIBERS_LOG] Using ${fibersMethod}.`).stack;

if (
!LOG_USE_FIBERS_INCLUDE_IN_PATH ||
stackFromError.includes(LOG_USE_FIBERS_INCLUDE_IN_PATH)
) {
console.warn(stackFromError);
}
}

function wrapFunction(fn, fibersMethod) {
return function () {
logUsingFibers(fibersMethod);
var stack = getAndClearStack();
try {
return fn.apply(this, arguments);
} finally {
restoreStack(stack);
}
}
};
}

// Monkey patch methods which may long jump
Fiber.yield = wrapFunction(Fiber.yield);
Fiber.prototype.run = wrapFunction(Fiber.prototype.run);
Fiber.prototype.throwInto = wrapFunction(Fiber.prototype.throwInto);
Fiber.yield = wrapFunction(Fiber.yield, "Fiber.yield");
Fiber.prototype.run = wrapFunction(Fiber.prototype.run, "Fiber.run");
Fiber.prototype.throwInto = wrapFunction(
Fiber.prototype.throwInto,
"Fiber.throwInto"
);

} catch (err) {
return;
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "fibers",
"version": "5.0.0",
"version": "5.0.2",
"description": "Cooperative multi-tasking for Javascript",
"keywords": [
"fiber",
Expand Down