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(-) 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