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