diff options
author | vsrs <[email protected]> | 2021-01-24 14:06:55 +0000 |
---|---|---|
committer | vsrs <[email protected]> | 2021-01-25 14:46:03 +0000 |
commit | 43fabfbe36820efa0448f5cb8aba091201ca2b33 (patch) | |
tree | 487cffd7c99b2aab0b3a742657203d17cc7703b4 | |
parent | 2c735ed734be9b9041921478e0049fffd7160f78 (diff) |
Add --no-buffering flag for the file logging.
-rw-r--r-- | crates/rust-analyzer/src/bin/args.rs | 16 | ||||
-rw-r--r-- | crates/rust-analyzer/src/bin/logger.rs | 14 | ||||
-rw-r--r-- | crates/rust-analyzer/src/bin/main.rs | 6 |
3 files changed, 21 insertions, 15 deletions
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; | |||
14 | pub(crate) struct Args { | 14 | pub(crate) struct Args { |
15 | pub(crate) verbosity: Verbosity, | 15 | pub(crate) verbosity: Verbosity, |
16 | pub(crate) log_file: Option<PathBuf>, | 16 | pub(crate) log_file: Option<PathBuf>, |
17 | pub(crate) no_buffering: bool, | ||
17 | pub(crate) command: Command, | 18 | pub(crate) command: Command, |
18 | } | 19 | } |
19 | 20 | ||
@@ -47,7 +48,8 @@ FLAGS: | |||
47 | -vv, --spammy | 48 | -vv, --spammy |
48 | -q, --quiet Set verbosity | 49 | -q, --quiet Set verbosity |
49 | 50 | ||
50 | --log-file <PATH> Log to the specified filed instead of stderr | 51 | --log-file <PATH> Log to the specified file instead of stderr |
52 | --no-buffering Flush log records to the file immediatly | ||
51 | 53 | ||
52 | ENVIRONMENTAL VARIABLES: | 54 | ENVIRONMENTAL VARIABLES: |
53 | RA_LOG Set log filter in env_logger format | 55 | RA_LOG Set log filter in env_logger format |
@@ -114,6 +116,7 @@ impl Args { | |||
114 | verbosity: Verbosity::Normal, | 116 | verbosity: Verbosity::Normal, |
115 | log_file: None, | 117 | log_file: None, |
116 | command: Command::Version, | 118 | command: Command::Version, |
119 | no_buffering: false, | ||
117 | }); | 120 | }); |
118 | } | 121 | } |
119 | 122 | ||
@@ -130,21 +133,22 @@ impl Args { | |||
130 | (false, true, true) => bail!("Invalid flags: -q conflicts with -v"), | 133 | (false, true, true) => bail!("Invalid flags: -q conflicts with -v"), |
131 | }; | 134 | }; |
132 | let log_file = matches.opt_value_from_str("--log-file")?; | 135 | let log_file = matches.opt_value_from_str("--log-file")?; |
136 | let no_buffering = matches.contains("--no-buffering"); | ||
133 | 137 | ||
134 | if matches.contains(["-h", "--help"]) { | 138 | if matches.contains(["-h", "--help"]) { |
135 | eprintln!("{}", HELP); | 139 | eprintln!("{}", HELP); |
136 | return Ok(Args { verbosity, log_file: None, command: Command::Help }); | 140 | return Ok(Args { verbosity, log_file: None, command: Command::Help, no_buffering }); |
137 | } | 141 | } |
138 | 142 | ||
139 | if matches.contains("--print-config-schema") { | 143 | if matches.contains("--print-config-schema") { |
140 | return Ok(Args { verbosity, log_file, command: Command::PrintConfigSchema }); | 144 | return Ok(Args { verbosity, log_file, command: Command::PrintConfigSchema, no_buffering }, ); |
141 | } | 145 | } |
142 | 146 | ||
143 | let subcommand = match matches.subcommand()? { | 147 | let subcommand = match matches.subcommand()? { |
144 | Some(it) => it, | 148 | Some(it) => it, |
145 | None => { | 149 | None => { |
146 | finish_args(matches)?; | 150 | finish_args(matches)?; |
147 | return Ok(Args { verbosity, log_file, command: Command::RunServer }); | 151 | return Ok(Args { verbosity, log_file, command: Command::RunServer, no_buffering }); |
148 | } | 152 | } |
149 | }; | 153 | }; |
150 | let command = match subcommand.as_str() { | 154 | let command = match subcommand.as_str() { |
@@ -219,11 +223,11 @@ impl Args { | |||
219 | }, | 223 | }, |
220 | _ => { | 224 | _ => { |
221 | eprintln!("{}", HELP); | 225 | eprintln!("{}", HELP); |
222 | return Ok(Args { verbosity, log_file: None, command: Command::Help }); | 226 | return Ok(Args { verbosity, log_file: None, command: Command::Help, no_buffering }); |
223 | } | 227 | } |
224 | }; | 228 | }; |
225 | finish_args(matches)?; | 229 | finish_args(matches)?; |
226 | Ok(Args { verbosity, log_file, command }) | 230 | Ok(Args { verbosity, log_file, command, no_buffering }) |
227 | } | 231 | } |
228 | } | 232 | } |
229 | 233 | ||
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 @@ | |||
2 | //! filter syntax. Amusingly, there's no crates.io crate that can do this and | 2 | //! filter syntax. Amusingly, there's no crates.io crate that can do this and |
3 | //! only this. | 3 | //! only this. |
4 | 4 | ||
5 | use std::{ | 5 | use std::{borrow::BorrowMut, fs::File, io::{BufWriter, Write}}; |
6 | fs::File, | ||
7 | io::{BufWriter, Write}, | ||
8 | }; | ||
9 | 6 | ||
10 | use env_logger::filter::{Builder, Filter}; | 7 | use env_logger::filter::{Builder, Filter}; |
11 | use log::{Log, Metadata, Record}; | 8 | use log::{Log, Metadata, Record}; |
@@ -14,10 +11,11 @@ use parking_lot::Mutex; | |||
14 | pub(crate) struct Logger { | 11 | pub(crate) struct Logger { |
15 | filter: Filter, | 12 | filter: Filter, |
16 | file: Option<Mutex<BufWriter<File>>>, | 13 | file: Option<Mutex<BufWriter<File>>>, |
14 | no_buffering: bool, | ||
17 | } | 15 | } |
18 | 16 | ||
19 | impl Logger { | 17 | impl Logger { |
20 | pub(crate) fn new(log_file: Option<File>, filter: Option<&str>) -> Logger { | 18 | pub(crate) fn new(log_file: Option<File>, no_buffering: bool, filter: Option<&str>) -> Logger { |
21 | let filter = { | 19 | let filter = { |
22 | let mut builder = Builder::new(); | 20 | let mut builder = Builder::new(); |
23 | if let Some(filter) = filter { | 21 | if let Some(filter) = filter { |
@@ -28,7 +26,7 @@ impl Logger { | |||
28 | 26 | ||
29 | let file = log_file.map(|it| Mutex::new(BufWriter::new(it))); | 27 | let file = log_file.map(|it| Mutex::new(BufWriter::new(it))); |
30 | 28 | ||
31 | Logger { filter, file } | 29 | Logger { filter, file, no_buffering } |
32 | } | 30 | } |
33 | 31 | ||
34 | pub(crate) fn install(self) { | 32 | pub(crate) fn install(self) { |
@@ -55,6 +53,10 @@ impl Log for Logger { | |||
55 | record.module_path().unwrap_or_default(), | 53 | record.module_path().unwrap_or_default(), |
56 | record.args(), | 54 | record.args(), |
57 | ); | 55 | ); |
56 | |||
57 | if self.no_buffering { | ||
58 | w.lock().borrow_mut().flush().unwrap(); | ||
59 | } | ||
58 | } | 60 | } |
59 | None => eprintln!( | 61 | None => eprintln!( |
60 | "[{} {}] {}", | 62 | "[{} {}] {}", |
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() { | |||
28 | 28 | ||
29 | fn try_main() -> Result<()> { | 29 | fn try_main() -> Result<()> { |
30 | let args = args::Args::parse()?; | 30 | let args = args::Args::parse()?; |
31 | setup_logging(args.log_file)?; | 31 | setup_logging(args.log_file, args.no_buffering)?; |
32 | match args.command { | 32 | match args.command { |
33 | args::Command::RunServer => run_server()?, | 33 | args::Command::RunServer => run_server()?, |
34 | args::Command::PrintConfigSchema => { | 34 | args::Command::PrintConfigSchema => { |
@@ -56,7 +56,7 @@ fn try_main() -> Result<()> { | |||
56 | Ok(()) | 56 | Ok(()) |
57 | } | 57 | } |
58 | 58 | ||
59 | fn setup_logging(log_file: Option<PathBuf>) -> Result<()> { | 59 | fn setup_logging(log_file: Option<PathBuf>, flush_file: bool) -> Result<()> { |
60 | env::set_var("RUST_BACKTRACE", "short"); | 60 | env::set_var("RUST_BACKTRACE", "short"); |
61 | 61 | ||
62 | let log_file = match log_file { | 62 | let log_file = match log_file { |
@@ -69,7 +69,7 @@ fn setup_logging(log_file: Option<PathBuf>) -> Result<()> { | |||
69 | None => None, | 69 | None => None, |
70 | }; | 70 | }; |
71 | let filter = env::var("RA_LOG").ok(); | 71 | let filter = env::var("RA_LOG").ok(); |
72 | logger::Logger::new(log_file, filter.as_deref()).install(); | 72 | logger::Logger::new(log_file, flush_file, filter.as_deref()).install(); |
73 | 73 | ||
74 | tracing_setup::setup_tracing()?; | 74 | tracing_setup::setup_tracing()?; |
75 | 75 | ||