Skip to main content

sunday_slate/
lib.rs

1#![forbid(unsafe_code)]
2pub mod admin;
3pub mod app;
4pub mod assets;
5pub mod auth;
6pub mod avatars;
7pub mod background;
8pub mod chrome;
9mod config;
10pub mod contests;
11pub mod context;
12#[cfg(any(feature = "test-support", test))]
13pub mod db;
14#[cfg(not(any(feature = "test-support", test)))]
15mod db;
16pub mod draft_entry;
17pub mod entries;
18mod error;
19pub mod fantasy_teams;
20pub mod flash;
21pub mod health;
22pub mod home;
23pub mod injuries;
24pub mod invites;
25pub mod leagues;
26pub mod live;
27pub mod mail;
28pub mod media;
29pub mod nfl_player_headshots;
30pub mod nfl_teams;
31mod observability;
32pub(crate) mod player_identity;
33mod router;
34pub mod rules;
35pub mod scoring;
36pub mod sessions;
37pub mod standings;
38mod state;
39pub mod users;
40pub mod validation;
41pub mod web;
42
43#[cfg(test)]
44mod tests;
45
46pub use app::App;
47pub use config::Config;
48pub use db::Db;
49pub use error::AppError;
50pub use state::AppState;
51pub use users::User;
52
53/// Default filter; a non-blank `RUST_LOG` replaces it.
54const DEFAULT_LOG_FILTER: &str = "warn,sunday_slate=info,nfl_data=info,nflverse_data=info,tank01_data=info,fanduel_data=info,utils=info";
55
56/// Build the subscriber filter. Unset and blank values fall back to the
57/// default: a blank value parses to a filter that admits nothing.
58fn log_filter(value: Option<&str>) -> anyhow::Result<tracing_subscriber::EnvFilter> {
59    use tracing_subscriber::EnvFilter;
60
61    match value {
62        None => Ok(EnvFilter::new(DEFAULT_LOG_FILTER)),
63        Some(value) if value.trim().is_empty() => Ok(EnvFilter::new(DEFAULT_LOG_FILTER)),
64        Some(value) => EnvFilter::try_new(value)
65            .map_err(|error| anyhow::anyhow!("invalid RUST_LOG {value:?}: {error}")),
66    }
67}
68
69pub fn init_tracing() -> anyhow::Result<()> {
70    use std::io::IsTerminal;
71    use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt};
72
73    let filter = match std::env::var("RUST_LOG") {
74        Ok(value) => log_filter(Some(&value))?,
75        Err(std::env::VarError::NotPresent) => log_filter(None)?,
76        Err(error) => return Err(anyhow::anyhow!("RUST_LOG is not valid Unicode: {error}")),
77    };
78
79    // journald and pipes are not terminals.
80    let ansi = std::io::stdout().is_terminal();
81
82    tracing_subscriber::registry()
83        .with(filter)
84        .with(tracing_subscriber::fmt::layer().with_ansi(ansi))
85        .try_init()?;
86    Ok(())
87}
88
89#[cfg(test)]
90mod logging_tests {
91    use super::{DEFAULT_LOG_FILTER, log_filter};
92
93    fn default_rendered() -> String {
94        tracing_subscriber::EnvFilter::new(DEFAULT_LOG_FILTER).to_string()
95    }
96
97    #[test]
98    fn unset_and_blank_rust_log_use_the_default_filter() {
99        let default = default_rendered();
100        for value in [None, Some(""), Some("   \n\t ")] {
101            let filter = log_filter(value).expect("default filter parses");
102            assert_eq!(filter.to_string(), default, "{value:?}");
103        }
104    }
105
106    #[test]
107    fn a_set_rust_log_replaces_the_default_filter() {
108        let filter = log_filter(Some("error")).expect("valid filter");
109        assert_eq!(filter.to_string(), "error");
110        assert_ne!(filter.to_string(), default_rendered());
111    }
112
113    #[test]
114    fn an_invalid_rust_log_is_reported() {
115        let error = log_filter(Some("sunday_slate=loud")).expect_err("invalid level");
116        let message = error.to_string();
117        assert!(message.contains("RUST_LOG"), "{message}");
118        assert!(message.contains("loud"), "{message}");
119    }
120}