diff --git a/Cargo.toml b/Cargo.toml index bb55497efb6..223099b4d24 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -56,7 +56,7 @@ openssl = { version = "0.10.48", features = ["vendored"] } # pin openssl-src to bring in fix for https://rustsec.org/advisories/RUSTSEC-2022-0032 #openssl-src = { version = "111.22" } -nydus-api = { version = "0.3.0", path = "api", features = ["handler"] } +nydus-api = { version = "0.3.0", path = "api", features = ["error-backtrace", "handler"] } nydus-builder = { version = "0.1.0", path = "builder" } nydus-rafs = { version = "0.3.1", path = "rafs" } nydus-service = { version = "0.3.0", path = "service", features = ["block-device"] } diff --git a/api/Cargo.toml b/api/Cargo.toml index c6ffa9ce3ac..442a666ed5f 100644 --- a/api/Cargo.toml +++ b/api/Cargo.toml @@ -9,16 +9,17 @@ repository = "https://github.com/dragonflyoss/image-service" edition = "2018" [dependencies] -backtrace = "0.3" +libc = "0.2" +log = "0.4.8" +serde_json = "1.0.53" +toml = "0.5" + +backtrace = { version = "0.3", optional = true } dbs-uhttp = { version = "0.3.0", optional = true } http = { version = "0.2.1", optional = true } lazy_static = { version = "1.4.0", optional = true } -libc = "0.2" -log = "0.4.8" mio = { version = "0.8", features = ["os-poll", "os-ext"], optional = true } serde = { version = "1.0.110", features = ["rc", "serde_derive"] } -serde_json = "1.0.53" -toml = "0.5" url = { version = "2.1.1", optional = true } [dev-dependencies] @@ -26,3 +27,4 @@ vmm-sys-util = { version = "0.10" } [features] handler = ["dbs-uhttp", "http", "lazy_static", "mio", "url"] +error-backtrace = ["backtrace"] diff --git a/api/src/error.rs b/api/src/error.rs index ca122f8d583..e160f5e34c9 100644 --- a/api/src/error.rs +++ b/api/src/error.rs @@ -2,26 +2,29 @@ // // SPDX-License-Identifier: Apache-2.0 -use std::env; use std::fmt::Debug; -use log::error; - /// Display error messages with line number, file path and optional backtrace. -pub fn make_error(err: std::io::Error, raw: impl Debug, file: &str, line: u32) -> std::io::Error { - if cfg!(debug_assertions) { - if let Ok(val) = env::var("RUST_BACKTRACE") { +pub fn make_error( + err: std::io::Error, + _raw: impl Debug, + _file: &str, + _line: u32, +) -> std::io::Error { + #[cfg(all(debug_assertions, feature = "error-backtrace"))] + { + if let Ok(val) = std::env::var("RUST_BACKTRACE") { if val.trim() != "0" { - error!("Stack:\n{:?}", backtrace::Backtrace::new()); - error!("Error:\n\t{:?}\n\tat {}:{}", raw, file, line); + log::error!("Stack:\n{:?}", backtrace::Backtrace::new()); + log::error!("Error:\n\t{:?}\n\tat {}:{}", _raw, _file, _line); return err; } } + log::error!( + "Error:\n\t{:?}\n\tat {}:{}\n\tnote: enable `RUST_BACKTRACE=1` env to display a backtrace", + _raw, _file, _line + ); } - error!( - "Error:\n\t{:?}\n\tat {}:{}\n\tnote: enable `RUST_BACKTRACE=1` env to display a backtrace", - raw, file, line - ); err }