Skip to content

Commit

Permalink
Merge pull request #39 from akoshelev/helper-service
Browse files Browse the repository at this point in the history
Migrate to `tracing` crate
  • Loading branch information
martinthomson authored Jul 11, 2022
2 parents 60dcf5e + 430f30b commit 31a8d4e
Show file tree
Hide file tree
Showing 7 changed files with 48 additions and 30 deletions.
27 changes: 14 additions & 13 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,36 +6,37 @@ edition = "2021"

[features]
default = ["debug", "cli"]
cli = ["enable-serde", "stderrlog", "structopt", "web-app"]
cli = ["enable-serde", "structopt", "web-app", "tracing-subscriber"]
debug = ["hex"]
enable-serde = ["serde", "serde_json", "rust-elgamal/enable-serde"]
web-app = ["tokio", "axum", "axum-server", "hyper", "hyper-tls"]
web-app = ["tokio", "axum", "axum-server", "hyper", "hyper-tls", "tower-http"]
self-signed-certs = ["hyper-tls"]

[dependencies]
axum = { version = "0.5.7", optional = true, features = ["http2"] }
axum-server = { version = "0.4.0", optional = true, features = ["rustls", "rustls-pemfile", "tls-rustls"] }
# rust-elgamal (via curve25519-dalek-ng) only works with digest 0.9, not 0.10
digest = "0.9"
hex = {version = "0.4", optional = true}
hex = { version = "0.4", optional = true }
# rust-elgamal (via curve25519-dalek-ng) only works with digest 0.9, so pin this
hkdf = "0.11"
hyper = { version = "0.14.19", optional = true, features = ["client", "h2"] }
hyper-tls = { version = "0.5.0", optional = true }
log = "0.4"
rand_core = "0.6"
rand = "0.8"
rand_core = "0.6"
redis = "0.21.5"
rust-elgamal = "0.4"
serde = {version = "1.0", optional = true}
serde_json = {version = "1.0", optional = true}
serde = { version = "1.0", optional = true }
serde_json = { version = "1.0", optional = true }
# rust-elgamal (via curve25519-dalek-ng) only works with digest 0.9, so pin this
sha2 = "0.9"
stderrlog = {version = "0.5", optional = true}
structopt = {version = "0.3", optional = true}
structopt = { version = "0.3", optional = true }
thiserror = "1.0"
tokio = { version = "1.19.2", optional = true, features = ["rt", "rt-multi-thread", "macros"] }
axum = { version = "0.5.7", optional = true, features = ["http2"] }
hyper = { version = "0.14.19", optional = true, features = ["client", "h2"] }
hyper-tls = { version = "0.5.0", optional = true }
axum-server = { version = "0.4.0", optional = true, features = ["rustls", "rustls-pemfile", "tls-rustls"] }

tower-http = { version = "0.3.4", optional = true, features = ["trace"] }
tracing = "0.1.35"
tracing-subscriber = { version = "0.3.14", optional = true }

[dev-dependencies]
hex = "0.4"
Expand Down
2 changes: 1 addition & 1 deletion src/bin/helper.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use std::error::Error;

use hyper::http::uri::Scheme;
use log::info;
use raw_ipa::cli::Verbosity;
use raw_ipa::net::{bind_mpc_helper_server, BindTarget};
use std::net::SocketAddr;
use structopt::StructOpt;
use tracing::info;

#[derive(Debug, StructOpt)]
#[structopt(name = "mpc-helper", about = "CLI to start an MPC helper endpoint")]
Expand Down
2 changes: 1 addition & 1 deletion src/bin/ua.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
use log::{debug, error, info, trace, warn};
use raw_ipa::cli::{HelperArgs, StringN, Verbosity};
use raw_ipa::{helpers::Helpers, user::User};
use std::fs;
use std::ops::Range;
use std::path::PathBuf;
use std::str::FromStr;
use structopt::StructOpt;
use tracing::{debug, error, info, trace, warn};

#[derive(Debug, StructOpt)]
#[structopt(name = "ua", about = "Functions for IPA User Agents")]
Expand Down
38 changes: 26 additions & 12 deletions src/cli/verbosity.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
use log::info;
use std::io::stderr;
use structopt::StructOpt;
use tracing::metadata::LevelFilter;
use tracing::{info, Level};
use tracing_subscriber::{fmt, layer::SubscriberExt, util::SubscriberInitExt};

#[derive(Debug, StructOpt)]
pub struct Verbosity {
Expand All @@ -14,16 +17,27 @@ pub struct Verbosity {

impl Verbosity {
pub fn setup_logging(&self) {
stderrlog::new()
.quiet(self.quiet)
.verbosity(self.verbose)
.timestamp(stderrlog::Timestamp::Off)
.init()
.unwrap_or_else(|e| {
if !self.quiet {
eprintln!("unable to configure logging: {:?}", e);
}
});
info!("Logging setup at level {}", self.verbose);
let filter_layer = self.level_filter();
let fmt_layer = fmt::layer().without_time().with_writer(stderr);

tracing_subscriber::registry()
.with(self.level_filter())
.with(fmt_layer)
.init();
info!("Logging setup at level {}", filter_layer);
}

fn level_filter(&self) -> LevelFilter {
if self.quiet {
LevelFilter::OFF
} else {
LevelFilter::from_level(match self.verbose {
0 => Level::ERROR,
1 => Level::WARN,
2 => Level::INFO,
3 => Level::DEBUG,
_ => Level::TRACE,
})
}
}
}
5 changes: 4 additions & 1 deletion src/net/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use axum_server::{tls_rustls::RustlsConfig, Handle};
use hyper::StatusCode;
use thiserror::Error;
use tokio::task::JoinHandle;
use tower_http::trace::TraceLayer;

mod handlers;

Expand Down Expand Up @@ -45,7 +46,9 @@ pub enum BindTarget {
/// Starts a new instance of MPC helper and binds it to a given target.
/// Returns a socket it is listening to and the join handle of the web server running.
pub async fn bind(target: BindTarget) -> (SocketAddr, JoinHandle<()>) {
let svc = router().into_make_service();
let svc = router()
.layer(TraceLayer::new_for_http())
.into_make_service();
let handle = Handle::new();

let task_handle = match target {
Expand Down
2 changes: 1 addition & 1 deletion src/net/thread.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use log::{error, info, warn};
use std::fmt::{Debug, Formatter};
use std::sync::{mpsc, Arc, Mutex};
use std::thread;
use tracing::{error, info, warn};

use crate::error::Res;

Expand Down
2 changes: 1 addition & 1 deletion src/user.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ use crate::error::{Error, Res};
use crate::report::{EncryptedMatchkeys, EventReport};
use crate::threshold::{Ciphertext, EncryptionKey as ThresholdEncryptionKey, RistrettoPoint};
use hkdf::Hkdf;
use log::trace;
use rand::{thread_rng, RngCore};
#[cfg(feature = "enable-serde")]
use serde::{Deserialize, Serialize};
Expand All @@ -13,6 +12,7 @@ use std::collections::HashMap;
use std::fs;
#[cfg(feature = "enable-serde")]
use std::path::{Path, PathBuf};
use tracing::trace;

#[cfg_attr(feature = "enable-serde", derive(Serialize, Deserialize))]
pub struct User {
Expand Down

0 comments on commit 31a8d4e

Please sign in to comment.