From 43fabfbe36820efa0448f5cb8aba091201ca2b33 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sun, 24 Jan 2021 17:06:55 +0300 Subject: Add --no-buffering flag for the file logging. --- crates/rust-analyzer/src/bin/args.rs | 16 ++++++++++------ crates/rust-analyzer/src/bin/logger.rs | 14 ++++++++------ crates/rust-analyzer/src/bin/main.rs | 6 +++--- 3 files changed, 21 insertions(+), 15 deletions(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/bin/args.rs b/crates/rust-analyzer/src/bin/args.rs index 7d917946e..4ec755769 100644 --- a/crates/rust-analyzer/src/bin/args.rs +++ b/crates/rust-analyzer/src/bin/args.rs @@ -14,6 +14,7 @@ use vfs::AbsPathBuf; pub(crate) struct Args { pub(crate) verbosity: Verbosity, pub(crate) log_file: Option, + pub(crate) no_buffering: bool, pub(crate) command: Command, } @@ -47,7 +48,8 @@ FLAGS: -vv, --spammy -q, --quiet Set verbosity - --log-file Log to the specified filed instead of stderr + --log-file Log to the specified file instead of stderr + --no-buffering Flush log records to the file immediatly ENVIRONMENTAL VARIABLES: RA_LOG Set log filter in env_logger format @@ -114,6 +116,7 @@ impl Args { verbosity: Verbosity::Normal, log_file: None, command: Command::Version, + no_buffering: false, }); } @@ -130,21 +133,22 @@ impl Args { (false, true, true) => bail!("Invalid flags: -q conflicts with -v"), }; let log_file = matches.opt_value_from_str("--log-file")?; + let no_buffering = matches.contains("--no-buffering"); if matches.contains(["-h", "--help"]) { eprintln!("{}", HELP); - return Ok(Args { verbosity, log_file: None, command: Command::Help }); + return Ok(Args { verbosity, log_file: None, command: Command::Help, no_buffering }); } if matches.contains("--print-config-schema") { - return Ok(Args { verbosity, log_file, command: Command::PrintConfigSchema }); + return Ok(Args { verbosity, log_file, command: Command::PrintConfigSchema, no_buffering }, ); } let subcommand = match matches.subcommand()? { Some(it) => it, None => { finish_args(matches)?; - return Ok(Args { verbosity, log_file, command: Command::RunServer }); + return Ok(Args { verbosity, log_file, command: Command::RunServer, no_buffering }); } }; let command = match subcommand.as_str() { @@ -219,11 +223,11 @@ impl Args { }, _ => { eprintln!("{}", HELP); - return Ok(Args { verbosity, log_file: None, command: Command::Help }); + return Ok(Args { verbosity, log_file: None, command: Command::Help, no_buffering }); } }; finish_args(matches)?; - Ok(Args { verbosity, log_file, command }) + Ok(Args { verbosity, log_file, command, no_buffering }) } } diff --git a/crates/rust-analyzer/src/bin/logger.rs b/crates/rust-analyzer/src/bin/logger.rs index 3bcb1ae37..4ea4ffafb 100644 --- a/crates/rust-analyzer/src/bin/logger.rs +++ b/crates/rust-analyzer/src/bin/logger.rs @@ -2,10 +2,7 @@ //! filter syntax. Amusingly, there's no crates.io crate that can do this and //! only this. -use std::{ - fs::File, - io::{BufWriter, Write}, -}; +use std::{borrow::BorrowMut, fs::File, io::{BufWriter, Write}}; use env_logger::filter::{Builder, Filter}; use log::{Log, Metadata, Record}; @@ -14,10 +11,11 @@ use parking_lot::Mutex; pub(crate) struct Logger { filter: Filter, file: Option>>, + no_buffering: bool, } impl Logger { - pub(crate) fn new(log_file: Option, filter: Option<&str>) -> Logger { + pub(crate) fn new(log_file: Option, no_buffering: bool, filter: Option<&str>) -> Logger { let filter = { let mut builder = Builder::new(); if let Some(filter) = filter { @@ -28,7 +26,7 @@ impl Logger { let file = log_file.map(|it| Mutex::new(BufWriter::new(it))); - Logger { filter, file } + Logger { filter, file, no_buffering } } pub(crate) fn install(self) { @@ -55,6 +53,10 @@ impl Log for Logger { record.module_path().unwrap_or_default(), record.args(), ); + + if self.no_buffering { + w.lock().borrow_mut().flush().unwrap(); + } } None => eprintln!( "[{} {}] {}", diff --git a/crates/rust-analyzer/src/bin/main.rs b/crates/rust-analyzer/src/bin/main.rs index 1d6e5478b..9a54193f6 100644 --- a/crates/rust-analyzer/src/bin/main.rs +++ b/crates/rust-analyzer/src/bin/main.rs @@ -28,7 +28,7 @@ fn main() { fn try_main() -> Result<()> { let args = args::Args::parse()?; - setup_logging(args.log_file)?; + setup_logging(args.log_file, args.no_buffering)?; match args.command { args::Command::RunServer => run_server()?, args::Command::PrintConfigSchema => { @@ -56,7 +56,7 @@ fn try_main() -> Result<()> { Ok(()) } -fn setup_logging(log_file: Option) -> Result<()> { +fn setup_logging(log_file: Option, flush_file: bool) -> Result<()> { env::set_var("RUST_BACKTRACE", "short"); let log_file = match log_file { @@ -69,7 +69,7 @@ fn setup_logging(log_file: Option) -> Result<()> { None => None, }; let filter = env::var("RA_LOG").ok(); - logger::Logger::new(log_file, filter.as_deref()).install(); + logger::Logger::new(log_file, flush_file, filter.as_deref()).install(); tracing_setup::setup_tracing()?; -- cgit v1.2.3 From 98d7512e93ea66f1a259623f9f534bee4a922a81 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sun, 24 Jan 2021 17:41:02 +0300 Subject: Add stderr flush --- crates/rust-analyzer/src/bin/args.rs | 2 +- crates/rust-analyzer/src/bin/logger.rs | 22 +++++++++++++++------- 2 files changed, 16 insertions(+), 8 deletions(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/bin/args.rs b/crates/rust-analyzer/src/bin/args.rs index 4ec755769..32d7836ff 100644 --- a/crates/rust-analyzer/src/bin/args.rs +++ b/crates/rust-analyzer/src/bin/args.rs @@ -49,7 +49,7 @@ FLAGS: -q, --quiet Set verbosity --log-file Log to the specified file instead of stderr - --no-buffering Flush log records to the file immediatly + --no-buffering Flush log records to the file immediately ENVIRONMENTAL VARIABLES: RA_LOG Set log filter in env_logger format diff --git a/crates/rust-analyzer/src/bin/logger.rs b/crates/rust-analyzer/src/bin/logger.rs index 4ea4ffafb..3e5cc7acf 100644 --- a/crates/rust-analyzer/src/bin/logger.rs +++ b/crates/rust-analyzer/src/bin/logger.rs @@ -2,7 +2,10 @@ //! filter syntax. Amusingly, there's no crates.io crate that can do this and //! only this. -use std::{borrow::BorrowMut, fs::File, io::{BufWriter, Write}}; +use std::{ + fs::File, + io::{self, BufWriter, Write}, +}; use env_logger::filter::{Builder, Filter}; use log::{Log, Metadata, Record}; @@ -53,10 +56,6 @@ impl Log for Logger { record.module_path().unwrap_or_default(), record.args(), ); - - if self.no_buffering { - w.lock().borrow_mut().flush().unwrap(); - } } None => eprintln!( "[{} {}] {}", @@ -65,11 +64,20 @@ impl Log for Logger { record.args(), ), } + + if self.no_buffering { + self.flush(); + } } fn flush(&self) { - if let Some(w) = &self.file { - let _ = w.lock().flush(); + match &self.file { + Some(w) => { + let _ = w.lock().flush(); + } + None => { + let _ = io::stderr().flush(); + } } } } -- cgit v1.2.3 From 8c843d1dac0151e9251cddd2b36e940b5b1c7661 Mon Sep 17 00:00:00 2001 From: vsrs Date: Sun, 24 Jan 2021 18:04:47 +0300 Subject: Add the ability to wait for a debugger. --- crates/rust-analyzer/src/bin/args.rs | 39 +++++++++++++++++++++++++++++++----- crates/rust-analyzer/src/bin/main.rs | 13 ++++++++++-- 2 files changed, 45 insertions(+), 7 deletions(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/bin/args.rs b/crates/rust-analyzer/src/bin/args.rs index 32d7836ff..abc00d03b 100644 --- a/crates/rust-analyzer/src/bin/args.rs +++ b/crates/rust-analyzer/src/bin/args.rs @@ -16,6 +16,7 @@ pub(crate) struct Args { pub(crate) log_file: Option, pub(crate) no_buffering: bool, pub(crate) command: Command, + pub(crate) wait_dbg: bool, } pub(crate) enum Command { @@ -51,6 +52,8 @@ FLAGS: --log-file Log to the specified file instead of stderr --no-buffering Flush log records to the file immediately + --wait-dbg Wait until a debugger is attached to + ENVIRONMENTAL VARIABLES: RA_LOG Set log filter in env_logger format RA_PROFILE Enable hierarchical profiler @@ -117,6 +120,7 @@ impl Args { log_file: None, command: Command::Version, no_buffering: false, + wait_dbg: false, }); } @@ -134,21 +138,40 @@ impl Args { }; let log_file = matches.opt_value_from_str("--log-file")?; let no_buffering = matches.contains("--no-buffering"); + let wait_dbg = matches.contains("--wait-dbg"); if matches.contains(["-h", "--help"]) { eprintln!("{}", HELP); - return Ok(Args { verbosity, log_file: None, command: Command::Help, no_buffering }); + return Ok(Args { + verbosity, + log_file: None, + command: Command::Help, + no_buffering, + wait_dbg, + }); } if matches.contains("--print-config-schema") { - return Ok(Args { verbosity, log_file, command: Command::PrintConfigSchema, no_buffering }, ); + return Ok(Args { + verbosity, + log_file, + command: Command::PrintConfigSchema, + no_buffering, + wait_dbg, + }); } let subcommand = match matches.subcommand()? { Some(it) => it, None => { finish_args(matches)?; - return Ok(Args { verbosity, log_file, command: Command::RunServer, no_buffering }); + return Ok(Args { + verbosity, + log_file, + command: Command::RunServer, + no_buffering, + wait_dbg, + }); } }; let command = match subcommand.as_str() { @@ -223,11 +246,17 @@ impl Args { }, _ => { eprintln!("{}", HELP); - return Ok(Args { verbosity, log_file: None, command: Command::Help, no_buffering }); + return Ok(Args { + verbosity, + log_file: None, + command: Command::Help, + no_buffering, + wait_dbg, + }); } }; finish_args(matches)?; - Ok(Args { verbosity, log_file, command, no_buffering }) + Ok(Args { verbosity, log_file, command, no_buffering, wait_dbg }) } } diff --git a/crates/rust-analyzer/src/bin/main.rs b/crates/rust-analyzer/src/bin/main.rs index 9a54193f6..0cddfecb5 100644 --- a/crates/rust-analyzer/src/bin/main.rs +++ b/crates/rust-analyzer/src/bin/main.rs @@ -21,6 +21,7 @@ static ALLOC: jemallocator::Jemalloc = jemallocator::Jemalloc; fn main() { if let Err(err) = try_main() { + log::error!("Unexpected error: {}", err); eprintln!("{}", err); process::exit(101); } @@ -28,6 +29,14 @@ fn main() { fn try_main() -> Result<()> { let args = args::Args::parse()?; + if args.wait_dbg { + #[allow(unused_mut)] + let mut d = 4; + while d == 4 { + d = 4; + } + } + setup_logging(args.log_file, args.no_buffering)?; match args.command { args::Command::RunServer => run_server()?, @@ -56,7 +65,7 @@ fn try_main() -> Result<()> { Ok(()) } -fn setup_logging(log_file: Option, flush_file: bool) -> Result<()> { +fn setup_logging(log_file: Option, no_buffering: bool) -> Result<()> { env::set_var("RUST_BACKTRACE", "short"); let log_file = match log_file { @@ -69,7 +78,7 @@ fn setup_logging(log_file: Option, flush_file: bool) -> Result<()> { None => None, }; let filter = env::var("RA_LOG").ok(); - logger::Logger::new(log_file, flush_file, filter.as_deref()).install(); + logger::Logger::new(log_file, no_buffering, filter.as_deref()).install(); tracing_setup::setup_tracing()?; -- cgit v1.2.3 From 185cd736a6da34891a9a5c5e9a2457eb075e565b Mon Sep 17 00:00:00 2001 From: vsrs Date: Mon, 25 Jan 2021 16:38:58 +0300 Subject: Add RA_WAIT_DBG and docs --- crates/rust-analyzer/src/bin/args.rs | 1 + crates/rust-analyzer/src/bin/main.rs | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/bin/args.rs b/crates/rust-analyzer/src/bin/args.rs index abc00d03b..c5f814021 100644 --- a/crates/rust-analyzer/src/bin/args.rs +++ b/crates/rust-analyzer/src/bin/args.rs @@ -57,6 +57,7 @@ FLAGS: ENVIRONMENTAL VARIABLES: RA_LOG Set log filter in env_logger format RA_PROFILE Enable hierarchical profiler + RA_WAIT_DBG If set acts like a --wait-dbg flag COMMANDS: diff --git a/crates/rust-analyzer/src/bin/main.rs b/crates/rust-analyzer/src/bin/main.rs index 0cddfecb5..80637cbff 100644 --- a/crates/rust-analyzer/src/bin/main.rs +++ b/crates/rust-analyzer/src/bin/main.rs @@ -29,7 +29,7 @@ fn main() { fn try_main() -> Result<()> { let args = args::Args::parse()?; - if args.wait_dbg { + if args.wait_dbg || env::var("RA_WAIT_DBG").is_ok() { #[allow(unused_mut)] let mut d = 4; while d == 4 { -- cgit v1.2.3 From ad603c3867ab31e8f433794481bb657a26a45bc0 Mon Sep 17 00:00:00 2001 From: vsrs Date: Wed, 27 Jan 2021 00:09:15 +0300 Subject: Add debug only guard for the --wait-dbg flag --- crates/rust-analyzer/src/bin/args.rs | 3 ++- crates/rust-analyzer/src/bin/main.rs | 2 ++ 2 files changed, 4 insertions(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/bin/args.rs b/crates/rust-analyzer/src/bin/args.rs index c5f814021..100e46d2f 100644 --- a/crates/rust-analyzer/src/bin/args.rs +++ b/crates/rust-analyzer/src/bin/args.rs @@ -52,7 +52,8 @@ FLAGS: --log-file Log to the specified file instead of stderr --no-buffering Flush log records to the file immediately - --wait-dbg Wait until a debugger is attached to + --wait-dbg Wait until a debugger is attached to. + The flag is valid for debug builds only ENVIRONMENTAL VARIABLES: RA_LOG Set log filter in env_logger format diff --git a/crates/rust-analyzer/src/bin/main.rs b/crates/rust-analyzer/src/bin/main.rs index 80637cbff..8f8939863 100644 --- a/crates/rust-analyzer/src/bin/main.rs +++ b/crates/rust-analyzer/src/bin/main.rs @@ -29,6 +29,8 @@ fn main() { fn try_main() -> Result<()> { let args = args::Args::parse()?; + + #[cfg(debug_assertions)] if args.wait_dbg || env::var("RA_WAIT_DBG").is_ok() { #[allow(unused_mut)] let mut d = 4; -- cgit v1.2.3 From 0269071283cd8d090c00f2bc1d84b4fabbe9ea87 Mon Sep 17 00:00:00 2001 From: vsrs Date: Wed, 27 Jan 2021 00:33:27 +0300 Subject: cargo fmt --- crates/rust-analyzer/src/bin/main.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/bin/main.rs b/crates/rust-analyzer/src/bin/main.rs index 8f8939863..1789d6a73 100644 --- a/crates/rust-analyzer/src/bin/main.rs +++ b/crates/rust-analyzer/src/bin/main.rs @@ -29,7 +29,7 @@ fn main() { fn try_main() -> Result<()> { let args = args::Args::parse()?; - + #[cfg(debug_assertions)] if args.wait_dbg || env::var("RA_WAIT_DBG").is_ok() { #[allow(unused_mut)] -- cgit v1.2.3 From 5f1eb544da9f33f3402914ba5c4318032cbad0c3 Mon Sep 17 00:00:00 2001 From: vsrs Date: Wed, 27 Jan 2021 00:59:31 +0300 Subject: Apply suggestions. --- crates/rust-analyzer/src/bin/args.rs | 5 +++-- crates/rust-analyzer/src/bin/logger.rs | 23 ++++++++++++++--------- 2 files changed, 17 insertions(+), 11 deletions(-) (limited to 'crates') diff --git a/crates/rust-analyzer/src/bin/args.rs b/crates/rust-analyzer/src/bin/args.rs index 100e46d2f..37d8414f4 100644 --- a/crates/rust-analyzer/src/bin/args.rs +++ b/crates/rust-analyzer/src/bin/args.rs @@ -50,7 +50,8 @@ FLAGS: -q, --quiet Set verbosity --log-file Log to the specified file instead of stderr - --no-buffering Flush log records to the file immediately + --no-log-buffering + Flush log records to the file immediately --wait-dbg Wait until a debugger is attached to. The flag is valid for debug builds only @@ -139,7 +140,7 @@ impl Args { (false, true, true) => bail!("Invalid flags: -q conflicts with -v"), }; let log_file = matches.opt_value_from_str("--log-file")?; - let no_buffering = matches.contains("--no-buffering"); + let no_buffering = matches.contains("--no-log-buffering"); let wait_dbg = matches.contains("--wait-dbg"); if matches.contains(["-h", "--help"]) { diff --git a/crates/rust-analyzer/src/bin/logger.rs b/crates/rust-analyzer/src/bin/logger.rs index 3e5cc7acf..14887c5cc 100644 --- a/crates/rust-analyzer/src/bin/logger.rs +++ b/crates/rust-analyzer/src/bin/logger.rs @@ -47,7 +47,8 @@ impl Log for Logger { if !self.filter.matches(record) { return; } - match &self.file { + + let should_flush = match &self.file { Some(w) => { let _ = writeln!( w.lock(), @@ -56,16 +57,20 @@ impl Log for Logger { record.module_path().unwrap_or_default(), record.args(), ); + self.no_buffering } - None => eprintln!( - "[{} {}] {}", - record.level(), - record.module_path().unwrap_or_default(), - record.args(), - ), - } + None => { + eprintln!( + "[{} {}] {}", + record.level(), + record.module_path().unwrap_or_default(), + record.args(), + ); + true // flush stderr unconditionally + } + }; - if self.no_buffering { + if should_flush { self.flush(); } } -- cgit v1.2.3