Skip to content

Commit

Permalink
add support for noop logger also (#77)
Browse files Browse the repository at this point in the history
Currently , user could only input the tracinglogger, added option to add
nooplogger also for testing purposes

---------

Co-authored-by: supernovahs <supernovahs@proton.me>
  • Loading branch information
supernovahs and supernovahs authored Aug 16, 2024
1 parent 5d9a2cc commit ddb0136
Show file tree
Hide file tree
Showing 14 changed files with 148 additions and 144 deletions.
44 changes: 30 additions & 14 deletions crates/chainio/clients/avsregistry/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use alloy_primitives::{Address, Bytes, FixedBytes, B256, U256};
use alloy_provider::Provider;
use alloy_rpc_types::Filter;
use ark_ff::Zero;
use eigen_logging::{logger::Logger, tracing_logger::TracingLogger};
use eigen_logging::logger::{Logger, SharedLogger};
use eigen_logging::tracing_logger::TracingLogger;
use eigen_types::operator::{bitmap_to_quorum_ids, OperatorPubKeys};
use eigen_utils::NEW_PUBKEY_REGISTRATION_EVENT;
use eigen_utils::{
Expand All @@ -13,26 +14,40 @@ use eigen_utils::{
use num_bigint::BigInt;
use std::collections::HashMap;
use std::fmt::Debug;
use std::sync::Arc;

/// Avs Registry chainreader
#[derive(Debug, Clone, Default)]
#[derive(Debug, Clone)]
pub struct AvsRegistryChainReader {
logger: TracingLogger,
logger: SharedLogger,
bls_apk_registry_addr: Address,
registry_coordinator_addr: Address,
operator_state_retriever: Address,
stake_registry_addr: Address,
provider: String,
}

impl Default for AvsRegistryChainReader {
fn default() -> Self {
AvsRegistryChainReader {
logger: Arc::new(TracingLogger::default()),
bls_apk_registry_addr: Default::default(),
registry_coordinator_addr: Default::default(),
operator_state_retriever: Default::default(),
stake_registry_addr: Default::default(),
provider: String::new(),
}
}
}

trait AvsRegistryReader {
fn get_quorum_count() -> Result<u8, String>;
}

impl AvsRegistryChainReader {
/// New AvsRegistryChainReader instance
pub async fn new(
logger: TracingLogger,
logger: SharedLogger,
registry_coordinator_addr: Address,
operator_state_retriever_addr: Address,
http_provider_url: String,
Expand Down Expand Up @@ -397,8 +412,9 @@ impl AvsRegistryChainReader {
i,
to_block
),
&["eigen-client-avsregistry.reader.query_existing_registered_operator_pub_keys"]
"eigen-client-avsregistry.reader.query_existing_registered_operator_pub_keys"
);

for pub_key_reg in logs
.iter()
.map(|v| {
Expand Down Expand Up @@ -497,14 +513,14 @@ impl AvsRegistryChainReader {
}
}
self.logger.debug(
&format!(
"num_transaction_logs : {} , from_block: {} , to_block: {}",
logs.len(),
i,
to_block
),
&["eigen-client-avsregistry.reader.query_existing_registered_operator_sockets"],
);
&format!(
"num_transaction_logs : {} , from_block: {} , to_block: {}",
logs.len(),
i,
to_block
),
"eigen-client-avsregistry.reader.query_existing_registered_operator_sockets",
);

i += query_block_range;
}
Expand Down Expand Up @@ -539,7 +555,7 @@ mod tests {

let holesky_provider = "https://ethereum-holesky.blockpi.network/v1/rpc/public";
AvsRegistryChainReader::new(
get_test_logger().clone(),
get_test_logger(),
holesky_registry_coordinator,
holesky_operator_state_retriever,
holesky_provider.to_string(),
Expand Down
6 changes: 3 additions & 3 deletions crates/chainio/clients/avsregistry/src/writer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use alloy_signer::Signer;
use alloy_signer_local::PrivateKeySigner;
use eigen_client_elcontracts::reader::ELChainReader;
use eigen_logging::tracing_logger::TracingLogger;
use eigen_logging::logger::SharedLogger;
use eigen_utils::binding::RegistryCoordinator::{
self, G1Point as RegistryG1Point, G2Point as RegistryG2Point, PubkeyRegistrationParams,
};
Expand All @@ -26,7 +26,7 @@ pub const GAS_LIMIT_REGISTER_OPERATOR_REGISTRY_COORDINATOR: u128 = 2000000;
/// AvsRegistry Writer
#[derive(Debug)]
pub struct AvsRegistryChainWriter {
logger: TracingLogger,
logger: SharedLogger,
service_manager_addr: Address,
registry_coordinator_addr: Address,
operator_state_retriever_addr: Address,
Expand All @@ -40,7 +40,7 @@ pub struct AvsRegistryChainWriter {
impl AvsRegistryChainWriter {
/// build avs registry chain writer instance
pub async fn build_avs_registry_chain_writer(
logger: TracingLogger,
logger: SharedLogger,
provider: String,
signer: String,
registry_coordinator_addr: Address,
Expand Down
10 changes: 5 additions & 5 deletions crates/chainio/clients/elcontracts/src/reader.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use crate::error::ElContractsError;
use alloy_primitives::{Address, FixedBytes, U256};
use eigen_logging::tracing_logger::TracingLogger;
use eigen_logging::logger::SharedLogger;
use eigen_types::operator::Operator;
use eigen_utils::{
binding::{AVSDirectory, DelegationManager, ISlasher, IStrategy, IERC20},
get_provider,
};
#[derive(Debug, Clone)]
pub struct ELChainReader {
_logger: TracingLogger,
_logger: SharedLogger,
slasher: Address,
delegation_manager: Address,
avs_directory: Address,
Expand All @@ -17,7 +17,7 @@ pub struct ELChainReader {

impl ELChainReader {
pub fn new(
_logger: TracingLogger,
_logger: SharedLogger,
slasher: Address,
delegation_manager: Address,
avs_directory: Address,
Expand All @@ -34,7 +34,7 @@ impl ELChainReader {

/// Builds a new [`ELChainReader`] instance .
pub async fn build(
_logger: TracingLogger,
_logger: SharedLogger,
delegation_manager: Address,
avs_directory: Address,
client: &String,
Expand Down Expand Up @@ -315,7 +315,7 @@ mod tests {
} = avs_directory_address_return;

ELChainReader::new(
get_test_logger().clone(),
get_test_logger(),
slasher_address,
delegation_manager_address,
avs_directory_address,
Expand Down
20 changes: 12 additions & 8 deletions crates/logging/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,34 +12,38 @@ pub static COMPONENT_KEY: &str = "component";
use ctor::ctor;
use log_level::LogLevel;
use logger::Logger;
use noop_logger::NoopLogger;
use once_cell::sync::OnceCell;
use std::sync::Arc;
use tracing_logger::TracingLogger;

static TEST_LOGGER: OnceCell<TracingLogger> = OnceCell::new();
/// [`NoopLogger`](for testing) OnceCell instance that can be initialized once
static TEST_LOGGER: OnceCell<Arc<(dyn Logger + 'static)>> = OnceCell::new();

static LOGGER: OnceCell<TracingLogger> = OnceCell::new();
/// [`TracingLogger`] OnceCell instance that can be initialized once
static LOGGER: OnceCell<Arc<(dyn Logger + 'static)>> = OnceCell::new();

/// Initializes the test logger at the start using ctor.
#[ctor]
fn init_test_logger() {
TEST_LOGGER.get_or_init(|| {
TracingLogger::new_text_logger(false, String::from(""), LogLevel::Debug, false)
NoopLogger::new_text_logger(false, String::from(""), LogLevel::Debug, false)
});
}

/// get the initialized test logger
pub fn get_test_logger() -> &'static TracingLogger {
TEST_LOGGER.get().expect("Logger not initialized")
pub fn get_test_logger() -> Arc<dyn Logger> {
TEST_LOGGER.get().expect("Logger not initialized").clone()
}

/// Use this to initialize the tracer. It can only be initialized once.
/// Use this to initialize the tracer. It can only be initialized once.
/// Set the tracing level according to [`LogLevel`]
pub fn init_logger(log_level: LogLevel) {
LOGGER
.get_or_init(|| TracingLogger::new_text_logger(false, String::from(""), log_level, false));
}

/// get the initialized logger
pub fn get_logger() -> &'static TracingLogger {
LOGGER.get().expect("Logger not initialized")
pub fn get_logger() -> Arc<dyn Logger> {
LOGGER.get().expect("Logger not initialized").clone()
}
35 changes: 12 additions & 23 deletions crates/logging/src/logger.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,17 @@
use super::log_level::LogLevel;
use std::fmt::Debug;
use std::sync::Arc;

pub trait Logger {
type LoggerType: Logger;
pub type SharedLogger = Arc<dyn Logger>;

fn new_text_logger(
no_color: bool,
time_format: String,
level: LogLevel,
add_source: bool,
) -> Self::LoggerType;

fn new_json_logger(
no_color: bool,
time_format: String,
level: LogLevel,
add_source: bool,
) -> Self::LoggerType;

fn debug(&self, msg: &str, args: &[impl Debug]);
fn info(&self, msg: &str, args: &[impl Debug]);
fn warn(&self, msg: &str, args: &[impl Debug]);
fn error(&self, msg: &str, args: &[impl Debug]);
fn fatal(&self, msg: &str, args: &[impl Debug]);
pub fn tags_as_debug<'a>(tags: &'a [&'a str]) -> Vec<&'a dyn Debug> {
tags.iter().map(|tag| tag as &dyn Debug).collect()
}

fn log(&self, msg: &str, args: &[impl Debug]);
pub trait Logger: Debug + Send + Sync {
fn debug(&self, msg: &str, args: &str);
fn info(&self, msg: &str, args: &str);
fn warn(&self, msg: &str, args: &str);
fn error(&self, msg: &str, args: &str);
fn fatal(&self, msg: &str, args: &str);
fn log(&self, msg: &str, args: &str);
}
31 changes: 16 additions & 15 deletions crates/logging/src/noop_logger.rs
Original file line number Diff line number Diff line change
@@ -1,36 +1,37 @@
use super::log_level::LogLevel;
use super::logger::Logger;
use std::fmt::Debug;
use std::sync::Arc;

#[derive(Debug, Default)]
#[derive(Debug, Default, Clone)]
pub struct NoopLogger {}

impl Logger for NoopLogger {
type LoggerType = NoopLogger;

fn new_text_logger(
impl NoopLogger {
pub fn new_text_logger(
_no_color: bool,
_time_format: String,
_level: LogLevel,
_add_source: bool,
) -> Self::LoggerType {
NoopLogger {}
) -> Arc<dyn Logger> {
Arc::new(NoopLogger {})
}

fn new_json_logger(
pub fn new_json_logger(
_no_color: bool,
_time_format: String,
_level: LogLevel,
_add_source: bool,
) -> Self::LoggerType {
) -> Self {
NoopLogger {}
}
}

fn debug(&self, _msg: &str, _args: &[impl Debug]) {}
fn info(&self, _msg: &str, _args: &[impl Debug]) {}
fn warn(&self, _msg: &str, _args: &[impl Debug]) {}
fn error(&self, _msg: &str, _args: &[impl Debug]) {}
fn fatal(&self, _msg: &str, _args: &[impl Debug]) {}
impl Logger for NoopLogger {
fn debug(&self, _msg: &str, _args: &str) {}
fn info(&self, _msg: &str, _args: &str) {}
fn warn(&self, _msg: &str, _args: &str) {}
fn error(&self, _msg: &str, _args: &str) {}
fn fatal(&self, _msg: &str, _args: &str) {}

fn log(&self, _msg: &str, _args: &[impl Debug]) {}
fn log(&self, _msg: &str, _args: &str) {}
}
Loading

0 comments on commit ddb0136

Please sign in to comment.