Skip to content

Commit

Permalink
Also initialize an env_logger::Logger in libaziot-keys. (Azure#140)
Browse files Browse the repository at this point in the history
It's compiled as a cdylib so it doesn't share the global logger of
the host process (keyd).

Ref: Azure#120
  • Loading branch information
arsing authored Mar 3, 2021
1 parent da456d2 commit ba79ffb
Show file tree
Hide file tree
Showing 8 changed files with 41 additions and 7 deletions.
11 changes: 10 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

8 changes: 7 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
2 changes: 1 addition & 1 deletion aziotd/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ edition = "2018"

[dependencies]
backtrace = "0.3"
env_logger = "0.8"
hyper = "0.14"
log = "0.4"
serde = "1"
Expand All @@ -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" }
4 changes: 2 additions & 2 deletions aziotd/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
1 change: 1 addition & 0 deletions key/aziot-keys/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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" }
Expand Down
4 changes: 4 additions & 0 deletions key/aziot-keys/src/implementation.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
10 changes: 10 additions & 0 deletions logger/Cargo.toml
Original file line number Diff line number Diff line change
@@ -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"
8 changes: 6 additions & 2 deletions aziotd/src/logging.rs → logger/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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 {
Expand Down

0 comments on commit ba79ffb

Please sign in to comment.