Skip to content

Commit

Permalink
Make '--output' optional benfred#229
Browse files Browse the repository at this point in the history
If no filename is specified by the user it is auto generated from
filename/pid the current time and the appropriate extension.
  • Loading branch information
Nudin committed Feb 12, 2021
1 parent 5f534b7 commit b16613a
Show file tree
Hide file tree
Showing 4 changed files with 63 additions and 8 deletions.
41 changes: 38 additions & 3 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ serde_json = "1.0"
rand = "0.8"
rand_distr = "0.4"
remoteprocess = {version="0.4.2", features=["unwind"]}
chrono = "0.4.19"

[target.'cfg(unix)'.dependencies]
termios = "0.3.2"
Expand Down
2 changes: 1 addition & 1 deletion src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ impl Config {
.value_name("filename")
.help("Output filename")
.takes_value(true)
.required(true))
.required(false))
.arg(Arg::with_name("format")
.short("f")
.long("format")
Expand Down
27 changes: 23 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ extern crate lazy_static;
extern crate libc;
#[macro_use]
extern crate log;
extern crate chrono;
#[cfg(unwind)]
extern crate lru;
extern crate memmap;
Expand Down Expand Up @@ -66,6 +67,8 @@ use stack_trace::{StackTrace, Frame};
use console_viewer::ConsoleViewer;
use config::{Config, FileFormat, RecordDuration};

use chrono::{SecondsFormat, Local};

#[cfg(unix)]
fn permission_denied(err: &Error) -> bool {
err.iter_chain().any(|cause| {
Expand Down Expand Up @@ -153,9 +156,25 @@ fn record_samples(pid: remoteprocess::Pid, config: &Config) -> Result<(), Error>
None => return Err(format_err!("A file format is required to record samples"))
};

let filename = match config.filename.as_ref() {
let filename = match config.filename.clone() {
Some(filename) => filename,
None => return Err(format_err!("A filename is required to record samples"))
None => {
let ext = match config.format.as_ref() {
Some(FileFormat::flamegraph) => "svg",
Some(FileFormat::speedscope) => "json",
Some(FileFormat::raw) => "txt",
None => return Err(format_err!("A file format is required to record samples"))
};
let local_time = Local::now().to_rfc3339_opts(SecondsFormat::Secs, true);
let name = match config.python_program.as_ref() {
Some(prog) => prog[0].to_string(),
None => match config.pid.as_ref() {
Some(pid) => pid.to_string(),
None => String::from("unknown")
}
};
format!("{}-{}.{}", name, local_time, ext)
}
};

let sampler = sampler::Sampler::new(pid, config)?;
Expand Down Expand Up @@ -294,7 +313,7 @@ fn record_samples(pid: remoteprocess::Pid, config: &Config) -> Result<(), Error>
}

{
let mut out_file = std::fs::File::create(filename)?;
let mut out_file = std::fs::File::create(&filename)?;
output.write(&mut out_file)?;
}

Expand All @@ -305,7 +324,7 @@ fn record_samples(pid: remoteprocess::Pid, config: &Config) -> Result<(), Error>
// you might be SSH'ed into a server somewhere and this isn't desired, but on
// that is pretty unlikely for osx) (note to self: xdg-open will open on linux)
#[cfg(target_os = "macos")]
std::process::Command::new("open").arg(filename).spawn()?;
std::process::Command::new("open").arg(&filename).spawn()?;
},
FileFormat::speedscope => {
println!("{}Wrote speedscope file to '{}'. Samples: {} Errors: {}", lede, filename, samples, errors);
Expand Down

0 comments on commit b16613a

Please sign in to comment.