|
1 | 1 | use anyhow::Result;
|
2 | 2 | use bore_cli::{client::Client, server::Server};
|
3 | 3 | use clap::{error::ErrorKind, CommandFactory, Parser, Subcommand};
|
| 4 | +use tracing; |
4 | 5 |
|
5 | 6 | #[derive(Parser, Debug)]
|
6 | 7 | #[clap(author, version, about)]
|
7 | 8 | struct Args {
|
8 | 9 | #[clap(subcommand)]
|
9 | 10 | command: Command,
|
| 11 | + #[arg(long)] |
| 12 | + log_path: Option<String>, |
10 | 13 | }
|
11 | 14 |
|
12 | 15 | #[derive(Subcommand, Debug)]
|
@@ -81,7 +84,33 @@ async fn run(command: Command) -> Result<()> {
|
81 | 84 | Ok(())
|
82 | 85 | }
|
83 | 86 |
|
| 87 | +fn setup_logging(log_path: Option<&String>) -> Result<tracing_appender::non_blocking::WorkerGuard> { |
| 88 | + match log_path { |
| 89 | + Some(x) => { |
| 90 | + let file_appender = tracing_appender::rolling::daily(x, "bore_server.log"); |
| 91 | + let (non_blocking, guard) = tracing_appender::non_blocking(file_appender); |
| 92 | + let subscriber = tracing_subscriber::fmt() |
| 93 | + .with_writer(non_blocking) |
| 94 | + .with_ansi(false) |
| 95 | + .finish(); |
| 96 | + tracing::subscriber::set_global_default(subscriber) |
| 97 | + .expect("setting up trace logging failed"); |
| 98 | + Ok(guard) |
| 99 | + } |
| 100 | + None => { |
| 101 | + let (non_blocking, guard) = tracing_appender::non_blocking(std::io::stdout()); |
| 102 | + let subscriber = tracing_subscriber::fmt() |
| 103 | + .with_writer(non_blocking) |
| 104 | + .finish(); |
| 105 | + tracing::subscriber::set_global_default(subscriber) |
| 106 | + .expect("setting up trace logging failed"); |
| 107 | + Ok(guard) |
| 108 | + } |
| 109 | + } |
| 110 | +} |
| 111 | + |
84 | 112 | fn main() -> Result<()> {
|
85 |
| - tracing_subscriber::fmt::init(); |
| 113 | + // _ to persist the global logger guard |
| 114 | + let _guard = setup_logging(Args::parse().log_path.as_ref()); |
86 | 115 | run(Args::parse().command)
|
87 | 116 | }
|
0 commit comments