Skip to content

Commit

Permalink
feat: add log level
Browse files Browse the repository at this point in the history
  • Loading branch information
oneofthezombies committed Feb 12, 2024
1 parent 6ea8a8e commit 58d7244
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 100 deletions.
13 changes: 3 additions & 10 deletions crates/apps/kill_tree_cli/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,14 +8,7 @@ repository.workspace = true
license.workspace = true

[dependencies]
kill_tree = { path = "../../libs/kill_tree", features = ["tokio", "blocking"] }
kill_tree = { path = "../../libs/kill_tree" }
clap = { version = "4.4.18", features = ["derive", "cargo"] }
tokio = { version = "1.36.0", features = ["full"] }

[[bin]]
name = "kill_tree_tokio"
path = "src/kill_tree_tokio.rs"

[[bin]]
name = "kill_tree_blocking"
path = "src/kill_tree_blocking.rs"
tracing = "0.1.40"
tracing-subscriber = "0.3.18"
90 changes: 0 additions & 90 deletions crates/apps/kill_tree_cli/src/kill_tree_tokio.rs

This file was deleted.

Original file line number Diff line number Diff line change
@@ -1,8 +1,15 @@
use std::io;

use clap::{
builder::{styling::AnsiColor, Styles},
command, value_parser, ArgAction, Parser,
};
use kill_tree::{blocking::kill_tree_with_config, Config};
use tracing::{
subscriber::{self, SetGlobalDefaultError},
Level,
};
use tracing_subscriber::FmtSubscriber;

fn get_styles() -> Styles {
Styles::styled()
Expand Down Expand Up @@ -31,12 +38,23 @@ struct Cli {
#[arg(help = "No logs are output.")]
#[arg(action = ArgAction::SetTrue)]
quiet: bool,

#[arg(long)]
#[arg(help = "Set the log level. Available levels: error, warn, info, debug, trace")]
#[arg(default_value = "warn")]
#[arg(value_parser = value_parser!(Level))]
log_level: Level,
}

fn init_log(level: Level) -> Result<(), SetGlobalDefaultError> {
subscriber::set_global_default(FmtSubscriber::builder().with_max_level(level).finish())
}

fn main() -> kill_tree::Result<()> {
let cli = Cli::parse();
let do_print = !cli.quiet;
if do_print {
init_log(cli.log_level).map_err(|e| io::Error::new(io::ErrorKind::Other, e))?;
println!(
"Killing all of target process and its children recursively. process id: {}, signal: {}",
cli.process_id, cli.signal
Expand Down
1 change: 1 addition & 0 deletions crates/libs/kill_tree/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,5 +30,6 @@ nix = { version = "0.27.1", features = ["signal"] }
bindgen = "0.69.2"

[features]
default = ["blocking"]
blocking = []
tokio = ["dep:tokio"]

0 comments on commit 58d7244

Please sign in to comment.