| 323 | |
| 324 | #[cfg(feature = "flame-it")] |
| 325 | fn write_profile(settings: &Settings) -> Result<(), Box<dyn core::error::Error>> { |
| 326 | use std::{fs, io}; |
| 327 | |
| 328 | enum ProfileFormat { |
| 329 | Html, |
| 330 | Text, |
| 331 | SpeedScope, |
| 332 | } |
| 333 | let profile_output = settings.profile_output.as_deref(); |
| 334 | let profile_format = match settings.profile_format.as_deref() { |
| 335 | Some("html") => ProfileFormat::Html, |
| 336 | Some("text") => ProfileFormat::Text, |
| 337 | None if profile_output == Some("-".as_ref()) => ProfileFormat::Text, |
| 338 | // spell-checker:ignore speedscope |
| 339 | Some("speedscope") | None => ProfileFormat::SpeedScope, |
| 340 | Some(other) => { |
| 341 | error!("Unknown profile format {}", other); |
| 342 | // TODO: Need to change to ExitCode or Termination |
| 343 | std::process::exit(1); |
| 344 | } |
| 345 | }; |
| 346 | |
| 347 | let profile_output = profile_output.unwrap_or_else(|| match profile_format { |
| 348 | ProfileFormat::Html => "flame-graph.html".as_ref(), |
| 349 | ProfileFormat::Text => "flame.txt".as_ref(), |
| 350 | ProfileFormat::SpeedScope => "flamescope.json".as_ref(), |
| 351 | }); |
| 352 | |
| 353 | let profile_output: Box<dyn io::Write> = if profile_output == "-" { |
| 354 | Box::new(io::stdout()) |
| 355 | } else { |
| 356 | Box::new(fs::File::create(profile_output)?) |
| 357 | }; |
| 358 | |
| 359 | let profile_output = io::BufWriter::new(profile_output); |
| 360 | |
| 361 | match profile_format { |
| 362 | ProfileFormat::Html => flame::dump_html(profile_output)?, |
| 363 | ProfileFormat::Text => flame::dump_text_to_writer(profile_output)?, |
| 364 | ProfileFormat::SpeedScope => flamescope::dump(profile_output)?, |
| 365 | } |
| 366 | |
| 367 | Ok(()) |
| 368 | } |
| 369 | |
| 370 | #[cfg(test)] |
| 371 | mod tests { |