-
Notifications
You must be signed in to change notification settings - Fork 5.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
re-work to use logging infrastructure
- Loading branch information
Showing
6 changed files
with
186 additions
and
105 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright 2018-2021 the Deno authors. All rights reserved. MIT license. | ||
|
||
use std::io::Write; | ||
use std::sync::atomic::AtomicBool; | ||
use std::sync::atomic::Ordering; | ||
use std::sync::Arc; | ||
|
||
struct CliLogger { | ||
logger: env_logger::Logger, | ||
lsp_debug_flag: Arc<AtomicBool>, | ||
} | ||
|
||
impl CliLogger { | ||
pub fn new( | ||
logger: env_logger::Logger, | ||
lsp_debug_flag: Arc<AtomicBool>, | ||
) -> Self { | ||
Self { | ||
logger, | ||
lsp_debug_flag, | ||
} | ||
} | ||
|
||
pub fn filter(&self) -> log::LevelFilter { | ||
self.logger.filter() | ||
} | ||
} | ||
|
||
impl log::Log for CliLogger { | ||
fn enabled(&self, metadata: &log::Metadata) -> bool { | ||
if metadata.target() == "deno::lsp::performance" | ||
&& metadata.level() == log::Level::Debug | ||
{ | ||
self.lsp_debug_flag.load(Ordering::Relaxed) | ||
} else { | ||
self.logger.enabled(metadata) | ||
} | ||
} | ||
|
||
fn log(&self, record: &log::Record) { | ||
if self.enabled(record.metadata()) { | ||
self.logger.log(record); | ||
} | ||
} | ||
|
||
fn flush(&self) { | ||
self.logger.flush(); | ||
} | ||
} | ||
|
||
pub(crate) fn init( | ||
maybe_level: Option<log::Level>, | ||
lsp_debug_flag: Arc<AtomicBool>, | ||
) { | ||
let log_level = maybe_level.unwrap_or(log::Level::Info); | ||
let logger = env_logger::Builder::from_env( | ||
env_logger::Env::default() | ||
.default_filter_or(log_level.to_level_filter().to_string()), | ||
) | ||
// https://github.com/denoland/deno/issues/6641 | ||
.filter_module("rustyline", log::LevelFilter::Off) | ||
// wgpu crates (gfx_backend), have a lot of useless INFO and WARN logs | ||
.filter_module("wgpu", log::LevelFilter::Error) | ||
.filter_module("gfx", log::LevelFilter::Error) | ||
// used to make available the lsp_debug which is then filtered out at runtime | ||
// in the cli logger | ||
.filter_module("deno::lsp::performance", log::LevelFilter::Debug) | ||
.format(|buf, record| { | ||
let mut target = record.target().to_string(); | ||
if let Some(line_no) = record.line() { | ||
target.push(':'); | ||
target.push_str(&line_no.to_string()); | ||
} | ||
if record.level() <= log::Level::Info | ||
|| (record.target() == "deno::lsp::performance" | ||
&& record.level() == log::Level::Debug) | ||
{ | ||
// Print ERROR, WARN, INFO and lsp_debug logs as they are | ||
writeln!(buf, "{}", record.args()) | ||
} else { | ||
// Add prefix to DEBUG or TRACE logs | ||
writeln!( | ||
buf, | ||
"{} RS - {} - {}", | ||
record.level(), | ||
target, | ||
record.args() | ||
) | ||
} | ||
}) | ||
.build(); | ||
|
||
let cli_logger = CliLogger::new(logger, lsp_debug_flag); | ||
let max_level = cli_logger.filter(); | ||
let r = log::set_boxed_logger(Box::new(cli_logger)); | ||
if r.is_ok() { | ||
log::set_max_level(max_level); | ||
} | ||
r.expect("Could not install logger."); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.