Skip to content

Commit

Permalink
feat(logging): centralize logging setup in teepot crate
Browse files Browse the repository at this point in the history
- Added a new logging module in `teepot` crate.
- Removed redundant logging setup code from individual projects.
- Updated dependencies and references for logging setup.

Signed-off-by: Harald Hoyer <harald@matterlabs.dev>
  • Loading branch information
haraldh committed Sep 18, 2024
1 parent 2ff3b11 commit af3ab51
Show file tree
Hide file tree
Showing 7 changed files with 84 additions and 67 deletions.
2 changes: 2 additions & 0 deletions Cargo.lock

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

17 changes: 8 additions & 9 deletions bin/vault-admin/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,12 +15,11 @@ use teepot::json::http::{
SignRequest, SignRequestData, SignResponse, VaultCommandRequest, VaultCommands,
VaultCommandsResponse, DIGEST_URL,
};
use teepot::log::{setup_logging, LogLevelParser};
use teepot::server::signatures::verify_sig;
use teepot::sgx::sign::Signature;
use tracing::level_filters::LevelFilter;
use tracing::{error, info};
use tracing_log::LogTracer;
use tracing_subscriber::Registry;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

#[derive(Args, Debug)]
struct SendArgs {
Expand Down Expand Up @@ -101,18 +100,18 @@ enum SubCommands {
struct Arguments {
#[clap(subcommand)]
cmd: SubCommands,
/// Log level for the log output.
/// Valid values are: `off`, `error`, `warn`, `info`, `debug`, `trace`
#[clap(long, default_value_t = LevelFilter::WARN, value_parser = LogLevelParser)]
pub log_level: LevelFilter,
}

#[actix_web::main]
async fn main() -> Result<()> {
LogTracer::init().context("Failed to set logger")?;
let args = Arguments::parse();

let subscriber = Registry::default()
.with(EnvFilter::from_default_env())
.with(fmt::layer().with_writer(std::io::stderr));
tracing::subscriber::set_global_default(subscriber).unwrap();
setup_logging(&args.log_level)?;

let args = Arguments::parse();
info!("Quote verified! Connection secure!");

match args.cmd {
Expand Down
34 changes: 3 additions & 31 deletions bin/verify-era-proof-attestation/src/args.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
use anyhow::{anyhow, Result};
use clap::{ArgGroup, Args, Parser};
use std::time::Duration;
use teepot::log::LogLevelParser;
use teepot::sgx::{parse_tcb_levels, EnumSet, TcbLevel};
use tracing_subscriber::filter::LevelFilter;
use url::Url;
Expand All @@ -18,6 +19,8 @@ use zksync_types::L2ChainId;
.args(&["batch_range", "continuous"]),
))]
pub struct Arguments {
/// Log level for the log output.
/// Valid values are: `off`, `error`, `warn`, `info`, `debug`, `trace`
#[clap(long, default_value_t = LevelFilter::WARN, value_parser = LogLevelParser)]
pub log_level: LevelFilter,
/// The batch number or range of batch numbers to verify the attestation and signature (e.g.,
Expand Down Expand Up @@ -90,34 +93,3 @@ fn parse_duration(s: &str) -> Result<Duration> {
let millis = s.parse()?;
Ok(Duration::from_millis(millis))
}

#[derive(Clone)]
struct LogLevelParser;

impl clap::builder::TypedValueParser for LogLevelParser {
type Value = LevelFilter;

fn parse_ref(
&self,
cmd: &clap::Command,
arg: Option<&clap::Arg>,
value: &std::ffi::OsStr,
) -> Result<Self::Value, clap::Error> {
clap::builder::TypedValueParser::parse(self, cmd, arg, value.to_owned())
}

fn parse(
&self,
cmd: &clap::Command,
arg: Option<&clap::Arg>,
value: std::ffi::OsString,
) -> std::result::Result<Self::Value, clap::Error> {
use std::str::FromStr;
let p = clap::builder::PossibleValuesParser::new([
"off", "error", "warn", "info", "debug", "trace",
]);
let v = p.parse(cmd, arg, value)?;

Ok(LevelFilter::from_str(&v).unwrap())
}
}
32 changes: 5 additions & 27 deletions bin/verify-era-proof-attestation/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,21 @@ mod client;
mod proof;
mod verification;

use anyhow::{Context, Result};
use crate::verification::{
log_quote_verification_summary, verify_attestation_quote, verify_batch_proof,
};
use anyhow::Result;
use args::{Arguments, AttestationPolicyArgs};
use clap::Parser;
use client::MainNodeClient;
use proof::get_proofs;
use reqwest::Client;
use teepot::log::setup_logging;
use tokio::{signal, sync::watch};
use tracing::{debug, error, info, trace, warn};
use tracing_log::LogTracer;
use tracing_subscriber::{filter::LevelFilter, fmt, prelude::*, EnvFilter, Registry};
use url::Url;
use zksync_basic_types::L1BatchNumber;

use crate::verification::{
log_quote_verification_summary, verify_attestation_quote, verify_batch_proof,
};

#[tokio::main]
async fn main() -> Result<()> {
let args = Arguments::parse();
Expand All @@ -45,26 +43,6 @@ async fn main() -> Result<()> {
Ok(())
}

fn setup_logging(log_level: &LevelFilter) -> Result<()> {
LogTracer::init().context("Failed to set logger")?;
let filter = EnvFilter::builder()
.try_from_env()
.unwrap_or(match *log_level {
LevelFilter::OFF => EnvFilter::new("off"),
_ => EnvFilter::new(format!(
"warn,{crate_name}={log_level},teepot={log_level}",
crate_name = env!("CARGO_CRATE_NAME"),
log_level = log_level
)),
});
let subscriber = Registry::default()
.with(filter)
.with(fmt::layer().with_writer(std::io::stderr));
tracing::subscriber::set_global_default(subscriber)?;

Ok(())
}

fn validate_arguments(args: &Arguments) -> Result<()> {
if args.attestation_policy.sgx_mrsigners.is_none()
&& args.attestation_policy.sgx_mrenclaves.is_none()
Expand Down
2 changes: 2 additions & 0 deletions crates/teepot/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,8 @@ sha2.workspace = true
signature.workspace = true
thiserror.workspace = true
tracing.workspace = true
tracing-log.workspace = true
tracing-subscriber.workspace = true
webpki-roots.workspace = true
x509-cert.workspace = true
zeroize.workspace = true
Expand Down
1 change: 1 addition & 0 deletions crates/teepot/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ pub mod json;
pub mod server;
pub mod sgx;

pub mod log;
pub mod quote;
63 changes: 63 additions & 0 deletions crates/teepot/src/log/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
// SPDX-License-Identifier: Apache-2.0
// Copyright (c) 2024 Matter Labs

//! Logging related stuff

use anyhow::Context;
use tracing::level_filters::LevelFilter;
use tracing_log::LogTracer;
use tracing_subscriber::Registry;
use tracing_subscriber::{fmt, prelude::*, EnvFilter};

/// A log level parser for clap, with "off", "error", "warn", "info", "debug", "trace" as valid values
#[derive(Clone)]
pub struct LogLevelParser;

impl clap::builder::TypedValueParser for LogLevelParser {
type Value = LevelFilter;

fn parse_ref(
&self,
cmd: &clap::Command,
arg: Option<&clap::Arg>,
value: &std::ffi::OsStr,
) -> anyhow::Result<Self::Value, clap::Error> {
clap::builder::TypedValueParser::parse(self, cmd, arg, value.to_owned())
}

fn parse(
&self,
cmd: &clap::Command,
arg: Option<&clap::Arg>,
value: std::ffi::OsString,
) -> std::result::Result<Self::Value, clap::Error> {
use std::str::FromStr;
let p = clap::builder::PossibleValuesParser::new([
"off", "error", "warn", "info", "debug", "trace",
]);
let v = p.parse(cmd, arg, value)?;

Ok(LevelFilter::from_str(&v).unwrap())
}
}

/// Setup standard logging and loglevel for the current crate and the `teepot` crate.
pub fn setup_logging(log_level: &LevelFilter) -> anyhow::Result<()> {
LogTracer::init().context("Failed to set logger")?;
let filter = EnvFilter::builder()
.try_from_env()
.unwrap_or(match *log_level {
LevelFilter::OFF => EnvFilter::new("off"),
_ => EnvFilter::new(format!(
"warn,{crate_name}={log_level},teepot={log_level}",
crate_name = env!("CARGO_CRATE_NAME"),
log_level = log_level
)),
});
let subscriber = Registry::default()
.with(filter)
.with(fmt::layer().with_writer(std::io::stderr));
tracing::subscriber::set_global_default(subscriber)?;

Ok(())
}

0 comments on commit af3ab51

Please sign in to comment.