Skip to content

Commit cf1812a

Browse files
committed
Add --verbose flag
1 parent af580c5 commit cf1812a

File tree

2 files changed

+27
-2
lines changed

2 files changed

+27
-2
lines changed

Cargo.toml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,13 @@ authors = ["Rik Huijzer"]
66
license = "MIT"
77

88
[dependencies]
9+
anyhow = "1"
910
clap = { version = "4.5.29", features = ["derive"] }
11+
futures-util = "0.3.31"
1012
tokio = { version = "1", features = ["macros", "rt-multi-thread"] }
13+
tracing-subscriber = "0.3"
14+
tracing = "0.1"
1115
transformrs = "0.6"
12-
anyhow = "1"
13-
futures-util = "0.3.31"
1416

1517
[dev-dependencies]
1618
assert_cmd = "2"

src/main.rs

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ mod tts;
44
use chat::ChatArgs;
55
use clap::Parser;
66
use std::io::Read;
7+
use tracing::subscriber::SetGlobalDefaultError;
78
use transformrs::Key;
89
use tts::TextToSpeechArgs;
910

@@ -30,6 +31,12 @@ enum Commands {
3031
struct Arguments {
3132
#[command(subcommand)]
3233
command: Commands,
34+
/// Verbose output.
35+
///
36+
/// The output of the logs is printed to stderr because the output is
37+
/// printed to stdout.
38+
#[arg(long)]
39+
verbose: bool,
3340
}
3441

3542
pub enum Task {
@@ -46,9 +53,25 @@ fn find_single_key(keys: transformrs::Keys) -> Key {
4653
keys[0].clone()
4754
}
4855

56+
/// Initialize logging with the given level.
57+
fn init_subscriber(level: tracing::Level) -> Result<(), SetGlobalDefaultError> {
58+
let subscriber = tracing_subscriber::FmtSubscriber::builder()
59+
.with_max_level(level)
60+
.with_writer(std::io::stderr)
61+
.without_time()
62+
.with_target(false)
63+
.finish();
64+
tracing::subscriber::set_global_default(subscriber)
65+
}
66+
4967
#[tokio::main]
5068
async fn main() {
5169
let args = Arguments::parse();
70+
if args.verbose {
71+
init_subscriber(tracing::Level::DEBUG).unwrap();
72+
} else {
73+
init_subscriber(tracing::Level::INFO).unwrap();
74+
}
5275

5376
let mut input = String::new();
5477
std::io::stdin().read_to_string(&mut input).unwrap();

0 commit comments

Comments
 (0)