Skip to content

Commit

Permalink
Improve logging
Browse files Browse the repository at this point in the history
  • Loading branch information
Ash-L2L authored and nchashch committed Oct 17, 2024
1 parent dd178a8 commit f021452
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 13 deletions.
6 changes: 5 additions & 1 deletion src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,11 @@ pub struct Config {
pub data_dir: PathBuf,
#[arg(long)]
pub enable_wallet: bool,
/// Log level
/// Log level.
/// Logs from most dependencies are filtered one level below the specified
/// log level, if a lower level exists.
/// For example, at the default log level `DEBUG`, logs from most
/// dependencies are only emitted if their level is `INFO` or lower.
#[arg(default_value_t = tracing::Level::DEBUG, long)]
pub log_level: tracing::Level,
#[command(flatten)]
Expand Down
50 changes: 46 additions & 4 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,54 @@ use proto::mainchain::{
use server::Validator;
use wallet::Wallet;

/// Saturating predecessor of a log level
fn saturating_pred_level(log_level: tracing::Level) -> tracing::Level {
match log_level {
tracing::Level::TRACE => tracing::Level::DEBUG,
tracing::Level::DEBUG => tracing::Level::INFO,
tracing::Level::INFO => tracing::Level::WARN,
tracing::Level::WARN => tracing::Level::ERROR,
tracing::Level::ERROR => tracing::Level::ERROR,
}
}

/// The empty string target `""` can be used to set a default level.
fn targets_directive_str<'a, Targets>(targets: Targets) -> String
where
Targets: IntoIterator<Item = (&'a str, tracing::Level)>,
{
targets
.into_iter()
.map(|(target, level)| {
let level = level.as_str().to_ascii_lowercase();
if target.is_empty() {
level
} else {
format!("{target}={level}")
}
})
.collect::<Vec<_>>()
.join(",")
}

// Configure logger.
fn set_tracing_subscriber(log_level: tracing::Level) -> miette::Result<()> {
let targets_filter = tracing_filter::EnvFilter::builder()
.with_default_directive(tracing_filter::LevelFilter::from_level(log_level).into())
.from_env()
.into_diagnostic()?;
let targets_filter = {
let default_directives_str = targets_directive_str([
("", saturating_pred_level(log_level)),
("bip300301", log_level),
("jsonrpsee_core::tracing", log_level),
("bip300301_enforcer", log_level),
]);
let directives_str = match std::env::var(tracing_filter::EnvFilter::DEFAULT_ENV) {
Ok(env_directives) => format!("{default_directives_str},{env_directives}"),
Err(std::env::VarError::NotPresent) => default_directives_str,
Err(err) => return Err(err).into_diagnostic(),
};
tracing_filter::EnvFilter::builder()
.parse(directives_str)
.into_diagnostic()?
};
let stdout_layer = tracing_subscriber::fmt::layer()
.compact()
.with_file(true)
Expand Down
8 changes: 7 additions & 1 deletion src/validator/dbs/block_hashes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,7 +116,8 @@ pub struct BlockHashDbs {
deposits: Database<SerdeBincode<BlockHash>, SerdeBincode<Vec<Deposit>>>,
// All keys in this DB MUST also exist in `height`
header: Database<SerdeBincode<BlockHash>, SerdeBincode<Header>>,
// All keys in this DB MUST also exist in `header`
// All keys in this DB MUST also exist in `header` as keys AND/OR
// `prev_blockhash` in a value
height: Database<SerdeBincode<BlockHash>, SerdeBincode<u32>>,
// All ancestors for each block MUST exist in this DB.
// All keys in this DB MUST also exist in ALL other DBs.
Expand Down Expand Up @@ -187,6 +188,11 @@ impl BlockHashDbs {
let block_hash = header.block_hash();
let () = self.header.put(rwtxn, &block_hash, header)?;
let () = self.height.put(rwtxn, &block_hash, &height)?;
if header.prev_blockhash != BlockHash::all_zeros() {
let () = self
.height
.put(rwtxn, &header.prev_blockhash, &(height - 1))?;
}
Ok(())
}

Expand Down
32 changes: 25 additions & 7 deletions src/validator/task.rs
Original file line number Diff line number Diff line change
Expand Up @@ -867,20 +867,38 @@ async fn sync_headers(
main_tip: BlockHash,
) -> Result<(), SyncError> {
let mut block_hash = main_tip;
while let Some(latest_missing_header) = tokio::task::block_in_place(|| {
let rotxn = dbs.read_txn()?;
dbs.block_hashes
.latest_missing_ancestor_header(&rotxn, block_hash)
.map_err(SyncError::DbTryGet)
})? {
tracing::debug!("Syncing header `{latest_missing_header}` -> `{main_tip}`");
while let Some((latest_missing_header, latest_missing_header_height)) =
tokio::task::block_in_place(|| {
let rotxn = dbs.read_txn()?;
match dbs
.block_hashes
.latest_missing_ancestor_header(&rotxn, block_hash)
.map_err(SyncError::DbTryGet)?
{
Some(latest_missing_header) => {
let height = dbs
.block_hashes
.height()
.try_get(&rotxn, &latest_missing_header)?;
Ok::<_, SyncError>(Some((latest_missing_header, height)))
}
None => Ok(None),
}
})?
{
if let Some(latest_missing_header_height) = latest_missing_header_height {
tracing::debug!("Syncing header #{latest_missing_header_height} `{latest_missing_header}` -> `{main_tip}`");
} else {
tracing::debug!("Syncing header `{latest_missing_header}` -> `{main_tip}`");
}
let header = main_client
.getblockheader(latest_missing_header)
.map_err(|err| SyncError::JsonRpc {
method: "getblockheader".to_owned(),
source: err,
})
.await?;
latest_missing_header_height.inspect(|height| assert_eq!(*height, header.height));
let height = header.height;
let mut rwtxn = dbs.write_txn()?;
dbs.block_hashes
Expand Down

0 comments on commit f021452

Please sign in to comment.