clap_logflag/
clap.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
use clap::Parser;

use crate::LogDestinationConfig;

#[derive(Parser, Debug)]
pub struct LogArgs {
    // TODO Formatting of this is weird in `--help` output
    // TODO Mention how to disable logging
    /// Log definition consisting of an optional log level, and a log destination.
    /// You can define this argument multiple times for multiple log destinations.
    ///
    /// Format: \[level:\]destination
    ///
    /// level = "ERROR" | "WARN" | "INFO" | "DEBUG" | "TRACE"
    ///
    /// destination = "stderr" | "syslog" | "file:path"
    ///
    /// Examples:
    /// * "syslog"
    /// * "stderr"
    /// * "file:/path/to/file"
    /// * "INFO:stderr"
    /// * "DEBUG:file:/path/to/file"
    /// * "TRACE:syslog"
    #[arg(long, value_parser=parse_destination_config)]
    pub log: Vec<LogDestinationConfig>,
}

fn parse_destination_config(input: &str) -> Result<LogDestinationConfig, String> {
    crate::parser::parse_config_definition(input)
        .map_err(|err| err.to_string())
        .and_then(|config| config.ok_or_else(|| "Failed to parse log config".to_string()))
}

impl From<LogArgs> for crate::config::LoggingConfig {
    fn from(args: LogArgs) -> Self {
        if args.log.is_empty() {
            Self::LoggingDisabled
        } else {
            Self::LoggingEnabled {
                destinations: args.log,
            }
        }
    }
}