From cc31004e522ca60c6752cd569e75cba44ade27df Mon Sep 17 00:00:00 2001 From: Avril Date: Mon, 3 Aug 2020 19:52:25 +0100 Subject: [PATCH] conditionally disable debug logging --- Cargo.toml | 17 ++++++++++++----- src/live.rs | 2 ++ src/log.rs | 13 ++++++++++++- src/main.rs | 29 ++++++++++++++++++++--------- 4 files changed, 46 insertions(+), 15 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 991f24c..b23dbfd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -8,21 +8,28 @@ edition = "2018" [features] default = ["threaded", - "watcher_timeout"] + "watcher_timeout", + "debug_logger"] # Run with threads (TODO: rename to `threads`. What does this do?? We need `rt-threaded` for fs watcher to work...) -threaded = [] +threaded = ["tokio/rt-threaded"] + +# Enable `Debug` log level printing option at runtime in release mode. +debug_logger = [] + +# Hot-reload config files +watcher = ["threaded"] # FS watcher will have infinite backlog, instead of ignoring if it goes over its backlog. # This can help DoS, but potentially cause OOM. -watcher_unlimited = [] +watcher_unlimited = ["watcher"] # FS watcher hooks have a defined timeout # This can help with DoS, and hooks that hog resources -watcher_timeout = [] +watcher_timeout = ["watcher"] [dependencies] -tokio = {version = "0.2", features=["time", "macros", "io-driver", "sync", "rt-core", "rt-threaded", "fs"]} +tokio = {version = "0.2", features=["time", "macros", "io-driver", "sync", "rt-core", "fs"]} notify = "5.0.0-pre.3" futures= "0.3" sexp = "1.1" diff --git a/src/live.rs b/src/live.rs index 446d53d..b45b817 100644 --- a/src/live.rs +++ b/src/live.rs @@ -13,6 +13,7 @@ //! # TODO //! - Make child not panic if `watch()` is called on non-existant path //! - Change hook from just string path prefix to pattern match with optional regex capability. (For finding and dispatching on global/local config files differently) +#![allow(unused_imports)] // For when hotreload feature disabled use super::*; use std::{ @@ -241,6 +242,7 @@ impl Oneesan } } +#[cfg(feature="watcher")] /// Start watching this path for changes of files pub fn watch(path: impl AsRef) -> Oneesan { diff --git a/src/log.rs b/src/log.rs index 02705a0..ed207ad 100644 --- a/src/log.rs +++ b/src/log.rs @@ -312,7 +312,7 @@ impl Logger Ok(()) } } - +#[cfg(any(debug_assertions,feature="debug_logger"))] #[macro_export] macro_rules! debug { ($obj:expr) => { { @@ -326,8 +326,14 @@ impl Logger }; } +#[cfg(not(any(debug_assertions,feature="debug_logger")))] +#[macro_export] macro_rules! debug { + ($obj:expr) => {{}}; + ($fmt:literal, $($args:expr),*) => {{}}; +} +#[cfg(any(debug_assertions,feature="debug_logger"))] #[macro_export] macro_rules! status { ($obj:expr) => { { @@ -363,6 +369,11 @@ impl Logger }; } +#[cfg(not(any(debug_assertions,feature="debug_logger")))] +#[macro_export] macro_rules! status { + ($obj:expr) => {{}}; + ($fmt:literal, $($args:expr),*) => {{}}; +} #[macro_export] macro_rules! info { ($obj:expr) => { diff --git a/src/main.rs b/src/main.rs index 57e96dd..34ca12c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -26,6 +26,7 @@ mod interval; mod config; mod live; + mod context; mod job; @@ -80,6 +81,7 @@ fn print_stats() static ref BUILD_IDENT: recolored::ColoredString = "release".bright_red(); } + #[allow(unused_imports)] use std::ops::Deref; status!("This is the lolicron daemon version {} by {} ({} build)", env!("CARGO_PKG_VERSION"), &AUTHORS[..], BUILD_IDENT.deref()); status!("---"); @@ -92,6 +94,12 @@ fn print_stats() #[cfg(feature="threaded")] status!(" +threaded".red()); #[cfg(not(feature="threaded"))] status!(" -threaded".bright_blue()); + + #[cfg(feature="watcher")] status!(" +watcher".red()); + #[cfg(not(feature="watcher"))] status!(" -watcher".bright_blue()); + + #[cfg(feature="debug_logger")] status!(" +debug_logger".red()); + #[cfg(not(feature="debug_logger"))] status!(" -debug_logger".bright_blue()); #[cfg(feature="watcher_unlimited")] status!(" +watcher_unlimited".bright_red()); #[cfg(not(feature="watcher_unlimited"))] status!(" -watcher_unlimited".blue()); @@ -111,21 +119,24 @@ async fn main() -> Result<(), Box> { log::init(log::Level::Debug); - debug!("Logger initialised"); + debug!("Logger initialised"); //TODO: Parse config first print_stats(); - let oneesan = live::watch("."); - + #[cfg(feature="watcher")] { - let mut recv = oneesan.hook("src/main.rs", live::filter::ALL).await; - while let Some(event) = recv.recv().await + let oneesan = live::watch("."); + { - important!("Got ev {:?}", event); - break; + let mut recv = oneesan.hook("src/main.rs", live::filter::ALL).await; + while let Some(event) = recv.recv().await + { + important!("Got ev {:?}", event); + break; + } } - } - oneesan.shutdown().await.expect("oneesan panic"); + oneesan.shutdown().await.expect("oneesan panic"); + } println!("{:?}", config::parse_global_single("example.rori").await.expect("Waaaaaah"));