From: Ian Jackson Date: Sun, 31 Jan 2021 16:45:06 +0000 (+0000) Subject: config: Allow OTTER_LOG to (completely) override config X-Git-Tag: otter-0.4.0~612 X-Git-Url: https://www.chiark.greenend.org.uk/ucgi/~ianmdlvl/git?a=commitdiff_plain;h=14ee376d92a885fb2461eee83431f7b95a14180b;p=otter.git config: Allow OTTER_LOG to (completely) override config If the env var is supplied and results in a nonempty log spec, we replace the log config completely. This is quite a lot of faff because no-one likes to give us the right interfaces. In particular flexi_logger wants to give and receive (byte) strings. This is not the final state; we want to merge them, really. Signed-off-by: Ian Jackson --- diff --git a/src/config.rs b/src/config.rs index 870e932e..8a566ad7 100644 --- a/src/config.rs +++ b/src/config.rs @@ -16,6 +16,7 @@ pub const DEFAULT_CONFIG_LEAFNAME : &str = "server.toml"; pub const DEFAULT_SENDMAIL_PROGRAM : &str = "/usr/sbin/sendmail"; pub const DAEMON_STARTUP_REPORT : &str = "otter-daemon started"; +pub const LOG_ENV_VAR: &str = "OTTER_LOG"; #[derive(Deserialize,Debug,Clone)] pub struct ServerConfigSpec { @@ -128,7 +129,7 @@ impl TryFrom for WholeServerConfig { let log = { use toml::Value::Table; - let log = match log { + let mut log = match log { Some(Table(log)) => log, None => Default::default(), Some(x) => throw!(anyhow!( @@ -136,7 +137,34 @@ impl TryFrom for WholeServerConfig { x.type_str()) ), }; - + + // flexi_logger doesn't allow env var to override config, sigh + // But we can simulate this by having it convert the env results + // to toml and merging it with the stuff from the file. + (||{ + dbg!(&log); + if let Some(v) = env::var_os(LOG_ENV_VAR) { + let v = v.to_str().ok_or(anyhow!("UTF-8 conversion"))?; + let v = LogSpecification::parse(v).context("parse")?; + dbg!(&v); + let mut buf: Vec = default(); + v.to_toml(&mut buf).context("convert to toml")?; + let v = toml_de::from_slice(&buf).context("reparse")?; + dbg!(&v); + let v = match v { + Some(Table(v)) => v, + None => default(), + Some(x) => throw!(anyhow!("reparse gave {:?}, no table", x)), + }; + dbg!(&v); + log.extend(v); + } + Ok::<_,AE>(()) + })() + .context(LOG_ENV_VAR) + .context("processing env override")?; + + dbg!(&log); Table(log) };