use std::path::{Path, PathBuf};
#[cfg(feature = "qlog")]
use std::sync::Arc;
use n0_error::Result;
#[cfg(feature = "qlog")]
use n0_future::time::Instant;
#[cfg(feature = "qlog")]
use crate::endpoint::QlogFileFactory;
use crate::endpoint::QuicTransportConfig;
#[derive(Debug, Clone)]
pub struct QlogFileGroup {
#[cfg(feature = "qlog")]
directory: PathBuf,
#[cfg(feature = "qlog")]
title: String,
#[cfg(feature = "qlog")]
start: Instant,
}
impl QlogFileGroup {
pub fn from_env(title: impl ToString) -> Self {
let directory = PathBuf::from(env!("CARGO_MANIFEST_DIR")).join("qlog");
Self::new(directory, title)
}
pub fn new(directory: impl AsRef<Path>, title: impl ToString) -> Self {
#[cfg(not(feature = "qlog"))]
let this = {
let _ = (directory, title);
Self {}
};
#[cfg(feature = "qlog")]
let this = Self {
title: title.to_string(),
directory: directory.as_ref().to_owned(),
start: Instant::now(),
};
this
}
pub fn create(&self, name: impl ToString) -> Result<QuicTransportConfig> {
#[cfg(not(feature = "qlog"))]
{
let _name = name;
Ok(QuicTransportConfig::default())
}
#[cfg(feature = "qlog")]
{
let mut builder = QuicTransportConfig::builder();
if std::env::var("IROH_TEST_QLOG").is_ok() {
let prefix = format!("{}.{}", self.title, name.to_string());
let factory = QlogFileFactory::new(self.directory.clone())
.with_prefix(prefix)
.with_start_instant(self.start.into());
builder = builder.qlog_factory(Arc::new(factory));
}
Ok(builder.build())
}
}
}