diff --git a/Cargo.lock b/Cargo.lock index f3e0ce229..0d9084615 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -446,6 +446,7 @@ dependencies = [ "hmac", "lazy_static", "log", + "logger", "openssl", "openssl-sys", "openssl-sys2", @@ -605,10 +606,10 @@ dependencies = [ "aziot-tpmd", "backtrace", "config-common", - "env_logger", "http-common", "hyper", "log", + "logger", "serde", "tokio", ] @@ -1493,6 +1494,14 @@ dependencies = [ "cfg-if 1.0.0", ] +[[package]] +name = "logger" +version = "0.1.0" +dependencies = [ + "env_logger", + "log", +] + [[package]] name = "matches" version = "0.1.8" diff --git a/Makefile b/Makefile index 3c9f888b8..160534be1 100644 --- a/Makefile +++ b/Makefile @@ -215,7 +215,13 @@ dist: # Copy source files cp -R \ - ./aziotctl ./aziotd ./cert ./config-common ./http-common ./identity ./iotedged ./key ./mini-sntp ./openssl-build ./openssl-sys2 ./openssl2 ./pkcs11 ./tpm \ + ./config-common ./http-common ./logger ./openssl-build ./openssl-sys2 ./openssl2 ./pkcs11 \ + ./aziotctl ./aziotd ./mini-sntp \ + ./cert \ + ./identity \ + ./key \ + ./tpm \ + ./iotedged \ /tmp/aziot-identity-service-$(PACKAGE_VERSION) cp ./Cargo.toml ./Cargo.lock ./CODE_OF_CONDUCT.md ./CONTRIBUTING.md ./LICENSE ./Makefile ./README.md ./rust-toolchain ./SECURITY.md /tmp/aziot-identity-service-$(PACKAGE_VERSION) diff --git a/aziotd/Cargo.toml b/aziotd/Cargo.toml index 13b822244..b3383a67a 100644 --- a/aziotd/Cargo.toml +++ b/aziotd/Cargo.toml @@ -7,7 +7,6 @@ edition = "2018" [dependencies] backtrace = "0.3" -env_logger = "0.8" hyper = "0.14" log = "0.4" serde = "1" @@ -19,3 +18,4 @@ aziot-keyd = { path = "../key/aziot-keyd" } aziot-tpmd = { path = "../tpm/aziot-tpmd" } config-common = { path = "../config-common" } http-common = { path = "../http-common", features = ["tokio1"] } +logger = { path = "../logger" } diff --git a/aziotd/src/main.rs b/aziotd/src/main.rs index 7073d9bdd..fff1447c0 100644 --- a/aziotd/src/main.rs +++ b/aziotd/src/main.rs @@ -10,13 +10,13 @@ #![allow(clippy::default_trait_access, clippy::let_unit_value)] mod error; -mod logging; use error::{Error, ErrorKind}; #[tokio::main] async fn main() { - logging::init(); + logger::try_init() + .expect("cannot fail to initialize global logger from the process entrypoint"); if let Err(err) = main_inner().await { log::error!("{}", err.0); diff --git a/key/aziot-keys/Cargo.toml b/key/aziot-keys/Cargo.toml index 0615acd8b..23ede3eb0 100644 --- a/key/aziot-keys/Cargo.toml +++ b/key/aziot-keys/Cargo.toml @@ -21,6 +21,7 @@ sha2 = "0.9" url = "2" aziot-keys-common = { path = "../aziot-keys-common" } +logger = { path = "../../logger" } openssl2 = { path = "../../openssl2" } openssl-sys2 = { path = "../../openssl-sys2" } pkcs11 = { path = "../../pkcs11/pkcs11" } diff --git a/key/aziot-keys/src/implementation.rs b/key/aziot-keys/src/implementation.rs index a92ab5ed5..e5debdd5c 100644 --- a/key/aziot-keys/src/implementation.rs +++ b/key/aziot-keys/src/implementation.rs @@ -17,6 +17,10 @@ pub(crate) unsafe fn get_function_list( version: crate::AZIOT_KEYS_VERSION, pfunction_list: *mut *const crate::AZIOT_KEYS_FUNCTION_LIST, ) -> crate::AZIOT_KEYS_RC { + // Ignore the error from `try_init`. The error indicates a global logger was already set, + // which is because `get_function_list` is being called a second time. That's fine. + let _ = logger::try_init(); + crate::r#catch(|| { static AZIOT_KEYS_FUNCTION_LIST_2_0_0_0: crate::AZIOT_KEYS_FUNCTION_LIST_2_0_0_0 = crate::AZIOT_KEYS_FUNCTION_LIST_2_0_0_0 { diff --git a/logger/Cargo.toml b/logger/Cargo.toml new file mode 100644 index 000000000..c23f22c44 --- /dev/null +++ b/logger/Cargo.toml @@ -0,0 +1,10 @@ +[package] +name = "logger" +version = "0.1.0" +authors = ["Azure IoT Edge Devs"] +edition = "2018" + + +[dependencies] +env_logger = "0.8" +log = "0.4" diff --git a/aziotd/src/logging.rs b/logger/src/lib.rs similarity index 89% rename from aziotd/src/logging.rs rename to logger/src/lib.rs index 8d2df584a..2c069d19b 100644 --- a/aziotd/src/logging.rs +++ b/logger/src/lib.rs @@ -1,8 +1,12 @@ // Copyright (c) Microsoft. All rights reserved. +#![deny(rust_2018_idioms)] +#![warn(clippy::all, clippy::pedantic)] +#![allow(clippy::missing_errors_doc)] + const LOG_LEVEL_ENV_VAR: &str = "AZIOT_LOG"; -pub(crate) fn init() { +pub fn try_init() -> Result<(), log::SetLoggerError> { env_logger::Builder::new() .format(|fmt, record| { use std::io::Write; @@ -39,7 +43,7 @@ pub(crate) fn init() { }) .filter_level(log::LevelFilter::Info) .parse_env(LOG_LEVEL_ENV_VAR) - .init(); + .try_init() } fn to_syslog_level(level: log::Level) -> i8 {