1
0
Fork 0
mirror of https://github.com/denoland/deno.git synced 2025-01-11 16:42:21 -05:00

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.
This commit is contained in:
David Sherret 2023-05-30 11:34:50 -04:00 committed by Bartek Iwańczuk
parent 02ff280496
commit 03398ddace
No known key found for this signature in database
GPG key ID: 0C6BCDDC3B3AD750
7 changed files with 60 additions and 23 deletions

View file

@ -678,11 +678,7 @@ impl CliFactory {
) -> Result<CliMainWorkerOptions, AnyError> { ) -> Result<CliMainWorkerOptions, AnyError> {
Ok(CliMainWorkerOptions { Ok(CliMainWorkerOptions {
argv: self.options.argv().clone(), argv: self.options.argv().clone(),
debug: self log_level: self.options.log_level().unwrap_or(log::Level::Info).into(),
.options
.log_level()
.map(|l| l == log::Level::Debug)
.unwrap_or(false),
coverage_dir: self.options.coverage_dir(), coverage_dir: self.options.coverage_dir(),
enable_testing_features: self.options.enable_testing_features(), enable_testing_features: self.options.enable_testing_features(),
has_node_modules_dir: self.options.has_node_modules_dir(), has_node_modules_dir: self.options.has_node_modules_dir(),

View file

@ -46,6 +46,7 @@ use deno_runtime::deno_tls::RootCertStoreProvider;
use deno_runtime::deno_web::BlobStore; use deno_runtime::deno_web::BlobStore;
use deno_runtime::permissions::Permissions; use deno_runtime::permissions::Permissions;
use deno_runtime::permissions::PermissionsContainer; use deno_runtime::permissions::PermissionsContainer;
use deno_runtime::WorkerLogLevel;
use deno_semver::npm::NpmPackageReqReference; use deno_semver::npm::NpmPackageReqReference;
use import_map::parse_from_json; use import_map::parse_from_json;
use std::pin::Pin; use std::pin::Pin;
@ -423,7 +424,7 @@ pub async fn run(
None, None,
CliMainWorkerOptions { CliMainWorkerOptions {
argv: metadata.argv, argv: metadata.argv,
debug: false, log_level: WorkerLogLevel::Info,
coverage_dir: None, coverage_dir: None,
enable_testing_features: false, enable_testing_features: false,
has_node_modules_dir, has_node_modules_dir,

View file

@ -37,6 +37,7 @@ use deno_runtime::web_worker::WebWorkerOptions;
use deno_runtime::worker::MainWorker; use deno_runtime::worker::MainWorker;
use deno_runtime::worker::WorkerOptions; use deno_runtime::worker::WorkerOptions;
use deno_runtime::BootstrapOptions; use deno_runtime::BootstrapOptions;
use deno_runtime::WorkerLogLevel;
use deno_semver::npm::NpmPackageReqReference; use deno_semver::npm::NpmPackageReqReference;
use crate::args::StorageKeyResolver; use crate::args::StorageKeyResolver;
@ -73,7 +74,7 @@ pub trait HasNodeSpecifierChecker: Send + Sync {
#[derive(Clone)] #[derive(Clone)]
pub struct CliMainWorkerOptions { pub struct CliMainWorkerOptions {
pub argv: Vec<String>, pub argv: Vec<String>,
pub debug: bool, pub log_level: WorkerLogLevel,
pub coverage_dir: Option<String>, pub coverage_dir: Option<String>,
pub enable_testing_features: bool, pub enable_testing_features: bool,
pub has_node_modules_dir: bool, pub has_node_modules_dir: bool,
@ -434,7 +435,7 @@ impl CliMainWorkerFactory {
cpu_count: std::thread::available_parallelism() cpu_count: std::thread::available_parallelism()
.map(|p| p.get()) .map(|p| p.get())
.unwrap_or(1), .unwrap_or(1),
debug_flag: shared.options.debug, log_level: shared.options.log_level,
enable_testing_features: shared.options.enable_testing_features, enable_testing_features: shared.options.enable_testing_features,
locale: deno_core::v8::icu::get_language_tag(), locale: deno_core::v8::icu::get_language_tag(),
location: shared.options.location.clone(), location: shared.options.location.clone(),
@ -562,7 +563,7 @@ fn create_web_worker_callback(
cpu_count: std::thread::available_parallelism() cpu_count: std::thread::available_parallelism()
.map(|p| p.get()) .map(|p| p.get())
.unwrap_or(1), .unwrap_or(1),
debug_flag: shared.options.debug, log_level: shared.options.log_level,
enable_testing_features: shared.options.enable_testing_features, enable_testing_features: shared.options.enable_testing_features,
locale: deno_core::v8::icu::get_language_tag(), locale: deno_core::v8::icu::get_language_tag(),
location: Some(args.main_module.clone()), location: Some(args.main_module.clone()),

View file

@ -5,18 +5,27 @@ const {
Promise, Promise,
SafeArrayIterator, SafeArrayIterator,
} = primordials; } = primordials;
let logDebug = false;
// 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"; let logSource = "JS";
function setLogDebug(debug, source) { function setLogLevel(level, source) {
logDebug = debug; logLevel = level;
if (source) { if (source) {
logSource = source; logSource = source;
} }
} }
function log(...args) { function log(...args) {
if (logDebug) { if (logLevel >= LogLevel.Debug) {
// if we destructure `console` off `globalThis` too early, we don't bind to // if we destructure `console` off `globalThis` too early, we don't bind to
// the right console, therefore we don't log anything out. // the right console, therefore we don't log anything out.
globalThis.console.error( globalThis.console.error(
@ -80,6 +89,6 @@ export {
log, log,
nonEnumerable, nonEnumerable,
readOnly, readOnly,
setLogDebug, setLogLevel,
writable, writable,
}; };

View file

@ -299,7 +299,7 @@ function runtimeStart(
v8Version, v8Version,
tsVersion, tsVersion,
target, target,
debugFlag, logLevel,
noColor, noColor,
isTty, isTty,
source, source,
@ -315,7 +315,7 @@ function runtimeStart(
tsVersion, tsVersion,
); );
core.setBuildInfo(target); core.setBuildInfo(target);
util.setLogDebug(debugFlag, source); util.setLogLevel(logLevel, source);
setNoColor(noColor || !isTty); setNoColor(noColor || !isTty);
// deno-lint-ignore prefer-primordials // deno-lint-ignore prefer-primordials
Error.prepareStackTrace = core.prepareStackTrace; Error.prepareStackTrace = core.prepareStackTrace;
@ -428,7 +428,7 @@ function bootstrapMainRuntime(runtimeOptions) {
const { const {
0: args, 0: args,
1: cpuCount, 1: cpuCount,
2: debugFlag, 2: logLevel,
3: denoVersion, 3: denoVersion,
4: locale, 4: locale,
5: location_, 5: location_,
@ -495,7 +495,7 @@ function bootstrapMainRuntime(runtimeOptions) {
v8Version, v8Version,
tsVersion, tsVersion,
target, target,
debugFlag, logLevel,
noColor, noColor,
isTty, isTty,
); );
@ -542,7 +542,7 @@ function bootstrapWorkerRuntime(
const { const {
0: args, 0: args,
1: cpuCount, 1: cpuCount,
2: debugFlag, 2: logLevel,
3: denoVersion, 3: denoVersion,
4: locale, 4: locale,
5: location_, 5: location_,
@ -610,7 +610,7 @@ function bootstrapWorkerRuntime(
v8Version, v8Version,
tsVersion, tsVersion,
target, target,
debugFlag, logLevel,
noColor, noColor,
isTty, isTty,
internalName ?? name, internalName ?? name,

View file

@ -35,3 +35,4 @@ pub mod worker;
mod worker_bootstrap; mod worker_bootstrap;
pub use worker_bootstrap::BootstrapOptions; pub use worker_bootstrap::BootstrapOptions;
pub use worker_bootstrap::WorkerLogLevel;

View file

@ -6,13 +6,42 @@ use std::thread;
use crate::colors; use crate::colors;
/// The log level to use when printing diagnostic log messages, warnings,
/// or errors in the worker.
///
/// Note: This is disconnected with the log crate's log level and the Rust code
/// in this crate will respect that value instead. To specify that, use
/// `log::set_max_level`.
#[derive(Debug, Default, Clone, Copy)]
pub enum WorkerLogLevel {
// WARNING: Ensure this is kept in sync with
// the JS values (search for LogLevel).
Error = 1,
Warn = 2,
#[default]
Info = 3,
Debug = 4,
}
impl From<log::Level> for WorkerLogLevel {
fn from(value: log::Level) -> Self {
match value {
log::Level::Error => WorkerLogLevel::Error,
log::Level::Warn => WorkerLogLevel::Warn,
log::Level::Info => WorkerLogLevel::Info,
log::Level::Debug => WorkerLogLevel::Debug,
log::Level::Trace => WorkerLogLevel::Debug,
}
}
}
/// Common bootstrap options for MainWorker & WebWorker /// Common bootstrap options for MainWorker & WebWorker
#[derive(Clone)] #[derive(Clone)]
pub struct BootstrapOptions { pub struct BootstrapOptions {
/// Sets `Deno.args` in JS runtime. /// Sets `Deno.args` in JS runtime.
pub args: Vec<String>, pub args: Vec<String>,
pub cpu_count: usize, pub cpu_count: usize,
pub debug_flag: bool, pub log_level: WorkerLogLevel,
pub enable_testing_features: bool, pub enable_testing_features: bool,
pub locale: String, pub locale: String,
pub location: Option<ModuleSpecifier>, pub location: Option<ModuleSpecifier>,
@ -44,7 +73,7 @@ impl Default for BootstrapOptions {
no_color: !colors::use_color(), no_color: !colors::use_color(),
is_tty: colors::is_tty(), is_tty: colors::is_tty(),
enable_testing_features: Default::default(), enable_testing_features: Default::default(),
debug_flag: Default::default(), log_level: Default::default(),
ts_version: Default::default(), ts_version: Default::default(),
locale: "en".to_string(), locale: "en".to_string(),
location: Default::default(), location: Default::default(),
@ -77,7 +106,7 @@ impl BootstrapOptions {
} }
{ {
let val = v8::Boolean::new(scope, self.debug_flag); let val = v8::Integer::new(scope, self.log_level as i32);
array.set_index(scope, 2, val.into()); array.set_index(scope, 2, val.into());
} }