1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2024-11-22 15:06:54 -05:00
denoland-deno/runtime/js/06_util.js
David Sherret 3b69d238cd
feat(runtime): add WorkerLogLevel (#19316)
This is not really used yet, but provides some infrastructure for doing
more fine grained logging in JS. I will add warn messages in a future
PR.
2023-05-30 15:34:50 +00:00

94 lines
1.6 KiB
JavaScript

// Copyright 2018-2023 the Deno authors. All rights reserved. MIT license.
const primordials = globalThis.__bootstrap.primordials;
const {
Promise,
SafeArrayIterator,
} = primordials;
// WARNING: Keep this in sync with Rust (search for LogLevel)
const LogLevel = {
Error: 1,
Warn: 2,
Info: 3,
Debug: 4,
};
let logLevel = 3;
let logSource = "JS";
function setLogLevel(level, source) {
logLevel = level;
if (source) {
logSource = source;
}
}
function log(...args) {
if (logLevel >= LogLevel.Debug) {
// if we destructure `console` off `globalThis` too early, we don't bind to
// the right console, therefore we don't log anything out.
globalThis.console.error(
`DEBUG ${logSource} -`,
...new SafeArrayIterator(args),
);
}
}
function createResolvable() {
let resolve;
let reject;
const promise = new Promise((res, rej) => {
resolve = res;
reject = rej;
});
promise.resolve = resolve;
promise.reject = reject;
return promise;
}
function writable(value) {
return {
value,
writable: true,
enumerable: true,
configurable: true,
};
}
function nonEnumerable(value) {
return {
value,
writable: true,
enumerable: false,
configurable: true,
};
}
function readOnly(value) {
return {
value,
enumerable: true,
writable: false,
configurable: true,
};
}
function getterOnly(getter) {
return {
get: getter,
set() {},
enumerable: true,
configurable: true,
};
}
export {
createResolvable,
getterOnly,
log,
nonEnumerable,
readOnly,
setLogLevel,
writable,
};