use rand::{distr::Alphanumeric, prelude::*, rng};
use tansu_schema::{Error, Result};
use tracing::subscriber::DefaultGuard;
use tracing_subscriber::EnvFilter;
pub(crate) fn init_tracing() -> Result<DefaultGuard> {
use std::{fs::File, sync::Arc, thread};
Ok(tracing::subscriber::set_default(
tracing_subscriber::fmt()
.with_level(true)
.with_line_number(true)
.with_thread_names(false)
.with_env_filter(
EnvFilter::from_default_env()
.add_directive(format!("{}=debug", env!("CARGO_CRATE_NAME")).parse()?),
)
.with_writer(
thread::current()
.name()
.ok_or(Error::Message(String::from("unnamed thread")))
.and_then(|name| {
File::create(format!(
"../logs/{}/{}::{name}.log",
env!("CARGO_PKG_NAME"),
env!("CARGO_CRATE_NAME")
))
.map_err(Into::into)
})
.map(Arc::new)?,
)
.finish(),
))
}
pub(crate) fn alphanumeric_string(length: usize) -> String {
let first: char = rng()
.sample_iter(&Alphanumeric)
.map(char::from)
.find(|c| c.is_ascii_alphabetic())
.unwrap_or('a');
let rest: String = rng()
.sample_iter(&Alphanumeric)
.take(length.saturating_sub(1))
.map(char::from)
.collect();
format!("{first}{rest}").to_lowercase()
}