Skip to content

Commit

Permalink
Updates cli args to be more explicit, still hitting annoying type errors
Browse files Browse the repository at this point in the history
  • Loading branch information
scottopell committed Nov 30, 2024
1 parent 94915f2 commit a13f938
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 47 deletions.
48 changes: 33 additions & 15 deletions crates/librqbit/src/tracing_subscriber_config_utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -81,9 +81,16 @@ pub fn init_logging(opts: InitLoggingOptions) -> anyhow::Result<InitLoggingResul

let (line_sub, line_broadcast) = Subscriber::new();

// Stdout logging layer.
let (json, human) = if opts.log_json {
(
Some(fmt::Layer::default().json().with_filter(stdout_filter)),
None,
)
} else {
(None, Some(fmt::Layer::default().with_filter(stdout_filter)))
};
let layered = tracing_subscriber::registry()
// Stdout logging layer.
.with(fmt::layer().with_filter(stdout_filter))
// HTTP API log broadcast layer.
.with(
fmt::layer()
Expand All @@ -109,19 +116,30 @@ pub fn init_logging(opts: InitLoggingOptions) -> anyhow::Result<InitLoggingResul
.open(&log_file)
.with_context(|| format!("error opening log file {:?}", log_file))?,
));
layered
.with(
fmt::layer()
.with_ansi(false)
.with_writer(log_file)
.with_filter(
EnvFilter::builder()
.parse(opts.log_file_rust_log.unwrap_or("info,librqbit=debug"))
.context("can't parse log-file-rust-log")?,
),
)
.try_init()
.context("can't init logging")?;
let log_env_filter = EnvFilter::builder()
.parse(opts.log_file_rust_log.unwrap_or("info,librqbit=debug"))
.context("can't parse log-file-rust-log")?;
if opts.log_file_json {
layered
.with(
fmt::layer()
.json()
.with_writer(log_file)
.with_filter(log_env_filter),
)
.try_init()
.context("can't init json file logging")?;
} else {
layered
.with(
fmt::layer()
.with_ansi(false)
.with_writer(log_file)
.with_filter(log_env_filter),
)
.try_init()
.context("can't init logging to file")?;
}
} else {
layered.try_init().context("can't init logging")?;
}
Expand Down
39 changes: 7 additions & 32 deletions crates/rqbit/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,27 +36,6 @@ enum LogLevel {
Error,
}

#[derive(Debug, Clone, ValueEnum)]
enum LogFormat {
Human,
Json,
}

impl Default for LogFormat {
fn default() -> Self {
LogFormat::Human
}
}

impl From<LogFormat> for librqbit::tracing_subscriber_config_utils::Format {
fn from(log_format: LogFormat) -> Self {
match log_format {
LogFormat::Human => librqbit::tracing_subscriber_config_utils::Format::Human,
LogFormat::Json => librqbit::tracing_subscriber_config_utils::Format::Json,
}
}
}

#[cfg(not(target_os = "windows"))]
fn parse_umask(value: &str) -> anyhow::Result<libc::mode_t> {
fn parse_oct_digit(d: u8) -> Option<libc::mode_t> {
Expand Down Expand Up @@ -102,9 +81,13 @@ struct Opts {
)]
log_file_rust_log: String,

/// Use structured logging when emitting to stdout
#[arg(long = "structured-stdout", default_value_t = false)]
structured_stdout: bool,
/// Logging to console (ie, stdout) will emit each log record as a json object
#[arg(long = "log-json", env = "RQBIT_LOG_JSON")]
log_json: bool,

/// Log file will contain json-formatted log records
#[arg(long = "log-file-json", env = "RQBIT_LOG_FILE_JSON")]
log_file_json: bool,

/// The interval to poll trackers, e.g. 30s.
/// Trackers send the refresh interval when we connect to them. Often this is
Expand Down Expand Up @@ -243,14 +226,6 @@ struct Opts {
#[cfg(feature = "disable-upload")]
#[arg(long, env = "RQBIT_DISABLE_UPLOAD")]
disable_upload: bool,

/// Logging to stdout will emit each log record as a json object
#[arg(long = "log-json", env = "RQBIT_LOG_JSON")]
log_json: bool,

/// Log file will contain json-formatted log records
#[arg(long = "log-file-json", env = "RQBIT_LOG_FILE_JSON")]
log_file_json: bool,
}

#[derive(Parser)]
Expand Down

0 comments on commit a13f938

Please sign in to comment.