Skip to content
This repository was archived by the owner on Sep 9, 2025. It is now read-only.

Commit 37e8fc1

Browse files
author
Hendrik van Antwerpen
committed
Print analysis time in debug builds
1 parent f784152 commit 37e8fc1

File tree

1 file changed

+61
-27
lines changed
  • tree-sitter-stack-graphs/src/cli

1 file changed

+61
-27
lines changed

tree-sitter-stack-graphs/src/cli/util.rs

Lines changed: 61 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@ use std::io::Write;
1313
use std::path::Path;
1414
use std::path::PathBuf;
1515
use std::time::Duration;
16+
use std::time::Instant;
1617
use tree_sitter_graph::parse_error::TreeWithParseErrorVec;
1718

1819
use crate::cli::MAX_PARSE_ERRORS;
@@ -164,6 +165,8 @@ pub struct FileStatusLogger<'a> {
164165
path: &'a Path,
165166
verbose: bool,
166167
path_logged: bool,
168+
#[cfg(debug_assertions)]
169+
processing_started: Option<Instant>,
167170
}
168171

169172
impl<'a> FileStatusLogger<'a> {
@@ -172,50 +175,81 @@ impl<'a> FileStatusLogger<'a> {
172175
path,
173176
verbose,
174177
path_logged: false,
178+
#[cfg(debug_assertions)]
179+
processing_started: None,
175180
}
176181
}
177182

178-
pub fn processing(&mut self) -> Result<(), anyhow::Error> {
179-
self.print_path(false)?;
180-
Ok(())
183+
pub fn processing(&mut self) -> std::io::Result<()> {
184+
#[cfg(debug_assertions)]
185+
{
186+
self.processing_started = Some(Instant::now());
187+
}
188+
if !self.verbose {
189+
return Ok(());
190+
}
191+
self.print_path();
192+
std::io::stdout().flush()
181193
}
182194

183-
pub fn ok(&mut self, status: &str) -> Result<(), anyhow::Error> {
184-
self.print_path(false)?;
185-
if self.verbose {
186-
println!("{}", status.green());
187-
self.path_logged = false;
195+
pub fn ok(&mut self, status: &str) -> std::io::Result<()> {
196+
if !self.verbose {
197+
return Ok(());
188198
}
189-
Ok(())
199+
self.print_path();
200+
print!("{}", status.green());
201+
#[cfg(debug_assertions)]
202+
self.print_processing_time();
203+
println!();
204+
self.path_logged = false;
205+
std::io::stdout().flush()
190206
}
191207

192-
pub fn info(&mut self, status: &str) -> Result<(), anyhow::Error> {
193-
self.print_path(true)?;
194-
println!("{}", status.dimmed());
208+
pub fn info(&mut self, status: &str) -> std::io::Result<()> {
209+
if !self.verbose {
210+
return Ok(());
211+
}
212+
self.print_path();
213+
print!("{}", status.dimmed());
214+
#[cfg(debug_assertions)]
215+
self.print_processing_time();
216+
println!();
195217
self.path_logged = false;
196-
Ok(())
218+
std::io::stdout().flush()
197219
}
198220

199-
pub fn warn(&mut self, status: &str) -> Result<(), anyhow::Error> {
200-
self.print_path(true)?;
201-
println!("{}", status.yellow());
221+
pub fn warn(&mut self, status: &str) -> std::io::Result<()> {
222+
self.print_path();
223+
print!("{}", status.yellow());
224+
#[cfg(debug_assertions)]
225+
self.print_processing_time();
226+
println!();
202227
self.path_logged = false;
203-
Ok(())
228+
std::io::stdout().flush()
204229
}
205230

206-
pub fn error(&mut self, status: &str) -> Result<(), anyhow::Error> {
207-
self.print_path(true)?;
208-
println!("{}", status.red());
231+
pub fn error(&mut self, status: &str) -> std::io::Result<()> {
232+
self.print_path();
233+
print!("{}", status.red());
234+
#[cfg(debug_assertions)]
235+
self.print_processing_time();
236+
println!();
209237
self.path_logged = false;
210-
Ok(())
238+
std::io::stdout().flush()
239+
}
240+
241+
fn print_path(&mut self) {
242+
if self.path_logged {
243+
return;
244+
}
245+
print!("{}: ", self.path.display());
246+
self.path_logged = true;
211247
}
212248

213-
fn print_path(&mut self, force: bool) -> Result<(), anyhow::Error> {
214-
if (self.verbose || force) && !self.path_logged {
215-
print!("{}: ", self.path.display());
216-
std::io::stdout().flush()?;
217-
self.path_logged = true;
249+
#[cfg(debug_assertions)]
250+
fn print_processing_time(&mut self) {
251+
if let Some(processing_started) = self.processing_started {
252+
print!(" [{:.2} s]", processing_started.elapsed().as_secs_f64());
218253
}
219-
Ok(())
220254
}
221255
}

0 commit comments

Comments
 (0)