From 766fe25c55b1a3776dc94daccbe0505c5cdfb0a1 Mon Sep 17 00:00:00 2001 From: Clement Tsang <34804052+ClementTsang@users.noreply.github.com> Date: Fri, 11 Dec 2020 20:39:32 -0500 Subject: [PATCH] refactor: Use feature flags to avoid building with fern and log (#351) --- .github/workflows/ci.yml | 2 +- .github/workflows/deployment.yml | 2 +- Cargo.toml | 11 +++++++---- src/bin/main.rs | 3 ++- src/lib.rs | 1 + src/utils/error.rs | 1 + src/utils/logging.rs | 5 ++--- 7 files changed, 15 insertions(+), 10 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1f75635c..dfe27c55 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -152,7 +152,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: check - args: --all-targets --verbose --target=${{ matrix.triple.target }} + args: --all-targets --verbose --target=${{ matrix.triple.target }} --no-default-features use-cross: ${{ matrix.triple.cross }} tests: diff --git a/.github/workflows/deployment.yml b/.github/workflows/deployment.yml index 237b682a..4f7b34d7 100644 --- a/.github/workflows/deployment.yml +++ b/.github/workflows/deployment.yml @@ -185,7 +185,7 @@ jobs: uses: actions-rs/cargo@v1 with: command: build - args: --release --verbose --target=${{ matrix.triple.target }} + args: --release --verbose --target=${{ matrix.triple.target }} --no-default-features use-cross: ${{ matrix.triple.cross }} - name: Move autocomplete to working directory diff --git a/Cargo.toml b/Cargo.toml index 70ff40a7..96a692f3 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,13 +25,16 @@ lto = true opt-level = 3 codegen-units = 1 +[features] +default = ["fern", "log"] + [dependencies] anyhow = "1.0.34" backtrace = "0.3" battery = "0.7.8" chrono = "0.4.19" crossterm = "0.18.2" -ctrlc = {version = "3.1", features = ["termination"]} +ctrlc = { version = "3.1", features = ["termination"] } clap = "2.33" dirs-next = "2.0.0" fnv = "1.0.7" @@ -45,14 +48,14 @@ serde = {version = "1.0", features = ["derive"] } sysinfo = "0.15.3" thiserror = "1.0.22" toml = "0.5.7" -tui = {version = "0.13.0", features = ["crossterm"], default-features = false } +tui = { version = "0.13.0", features = ["crossterm"], default-features = false } typed-builder = "0.7.1" unicode-segmentation = "1.7.1" unicode-width = "0.1" # For debugging only... -fern = "0.6.0" -log = "0.4.11" +fern = { version = "0.6.0", optional=true } +log = { version="0.4.11", optional=true } [target.'cfg(not(any(target_arch = "arm", target_arch = "aarch64")))'.dependencies] heim = "0.0.11" diff --git a/src/bin/main.rs b/src/bin/main.rs index 320b100a..8df793a7 100644 --- a/src/bin/main.rs +++ b/src/bin/main.rs @@ -1,5 +1,6 @@ #![warn(rust_2018_idioms)] #[allow(unused_imports)] +#[cfg(feature = "log")] #[macro_use] extern crate log; @@ -33,7 +34,7 @@ fn main() -> Result<()> { // tmp_dir.push("bottom_debug.log"); // utils::logging::init_logger(log::LevelFilter::Trace, tmp_dir.as_os_str())?; // } else { - #[cfg(debug_assertions)] + #[cfg(all(feature = "fern", debug_assertions))] { utils::logging::init_logger(log::LevelFilter::Debug, std::ffi::OsStr::new("debug.log"))?; } diff --git a/src/lib.rs b/src/lib.rs index e245f1ef..1f10990e 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,5 +1,6 @@ #![warn(rust_2018_idioms)] #[allow(unused_imports)] +#[cfg(feature = "log")] #[macro_use] extern crate log; diff --git a/src/utils/error.rs b/src/utils/error.rs index 8bfec098..861fc101 100644 --- a/src/utils/error.rs +++ b/src/utils/error.rs @@ -73,6 +73,7 @@ impl From for BottomError { } } +#[cfg(feature = "fern")] impl From for BottomError { fn from(err: fern::InitError) -> Self { BottomError::FernError(err.to_string()) diff --git a/src/utils/logging.rs b/src/utils/logging.rs index 86a55656..704f6434 100644 --- a/src/utils/logging.rs +++ b/src/utils/logging.rs @@ -1,7 +1,6 @@ -use std::ffi::OsStr; - +#[cfg(feature = "fern")] pub fn init_logger( - min_level: log::LevelFilter, debug_file_name: &OsStr, + min_level: log::LevelFilter, debug_file_name: &std::ffi::OsStr, ) -> Result<(), fern::InitError> { fern::Dispatch::new() .format(|out, message, record| {