Skip to content

Commit

Permalink
Add env_logger to binaries (#66)
Browse files Browse the repository at this point in the history
This adds logging to inferno-collapse-perf and inferno-flamegraph. 
The following flags were added:
-v increases verbosity
-q silences all logs
  • Loading branch information
jasonrhansen committed Mar 1, 2019
1 parent 829c6b1 commit 53ac6a5
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 1 deletion.
117 changes: 117 additions & 0 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 @@ -27,6 +27,7 @@ object = "0.11"
gimli = "0.16"
memmap = "0.7"
log = "0.4"
env_logger = "0.6.0"
quick-xml = "0.13.3"
num-format = { version = "0.4", default-features = false }
str_stack = "0.1"
Expand Down
25 changes: 24 additions & 1 deletion src/bin/collapse-perf.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use env_logger::Env;
use std::io;
use std::path::PathBuf;
use structopt::StructOpt;
Expand Down Expand Up @@ -53,6 +54,14 @@ struct Opt {
#[structopt(long = "event-filter", value_name = "EVENT")]
event_filter: Option<String>,

/// Silence all log output
#[structopt(short = "q", long = "quiet")]
quiet: bool,

/// Verbose logging mode (-v, -vv, -vvv)
#[structopt(short = "v", long = "verbose", parse(from_occurrences))]
verbose: usize,

/// perf script output file, or STDIN if not specified
infile: Option<PathBuf>,
}
Expand All @@ -76,6 +85,20 @@ impl Opt {
}

fn main() -> io::Result<()> {
let (infile, options) = Opt::from_args().into_parts();
let opt = Opt::from_args();

// Initialize logger
if !opt.quiet {
env_logger::Builder::from_env(Env::default().default_filter_or(match opt.verbose {
0 => "warn",
1 => "info",
2 => "debug",
_ => "trace",
}))
.default_format_timestamp(false)
.init();
}

let (infile, options) = opt.into_parts();
Folder::from(options).collapse_file(infile.as_ref())
}
21 changes: 21 additions & 0 deletions src/bin/flamegraph.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
use env_logger::Env;
use std::fs::File;
use std::io::{self, BufReader, Read};
use std::path::PathBuf;
Expand Down Expand Up @@ -48,6 +49,14 @@ struct Opt {
/// Factor to scale sample counts by
#[structopt(long = "factor", default_value = "1.0")]
factor: f64,

/// Silence all log output
#[structopt(short = "q", long = "quiet")]
quiet: bool,

/// Verbose logging mode (-v, -vv, -vvv)
#[structopt(short = "v", long = "verbose", parse(from_occurrences))]
verbose: usize,
}

impl Into<Options> for Opt {
Expand Down Expand Up @@ -78,6 +87,18 @@ impl Into<Options> for Opt {
fn main() -> quick_xml::Result<()> {
let opt = Opt::from_args();

// Initialize logger
if !opt.quiet {
env_logger::Builder::from_env(Env::default().default_filter_or(match opt.verbose {
0 => "warn",
1 => "info",
2 => "debug",
_ => "trace",
}))
.default_format_timestamp(false)
.init();
}

if opt.infiles.is_empty() || opt.infiles.len() == 1 && opt.infiles[0].to_str() == Some("-") {
let stdin = io::stdin();
let r = BufReader::with_capacity(128 * 1024, stdin.lock());
Expand Down

0 comments on commit 53ac6a5

Please sign in to comment.