use std::cell::RefCell;
use std::env;
use std::path::Path;
use std::sync::Once;
static INIT_LOG: Once = Once::new();
thread_local! {
static TEST_ID: RefCell<&'static str> = RefCell::new("UNINITIALIZED_TEST_ID");
}
pub fn set_test_id(test_id: &'static str) {
TEST_ID.with(|id| *id.borrow_mut() = test_id);
}
pub fn get_test_id() -> &'static str {
TEST_ID.with(|id| *id.borrow())
}
#[macro_export]
macro_rules! log_error {
($($args:tt)+) => {
error!("[{}] {}", $crate::bb_common::get_test_id(), format!($($args)+))
}
}
#[macro_export]
macro_rules! log_info {
($($args:tt)+) => {
info!("[{}] {}", $crate::bb_common::get_test_id(), format!($($args)+));
}
}
#[macro_export]
macro_rules! log_debug {
($($args:tt)+) => {
debug!("[{}] {}", $crate::bb_common::get_test_id(), format!($($args)+));
}
}
fn _init_log() {
color_backtrace::install();
env_logger::init();
}
pub fn init_log(test_id: &'static str) {
INIT_LOG.call_once(_init_log);
set_test_id(test_id);
}
pub fn path_to_asset_file(asset_name: &str) -> String {
let path =
Path::new(env::var("CARGO_MANIFEST_DIR").expect("CARGO_MANIFEST_DIR not set").as_str())
.join(format!("tests/assets/{}", asset_name));
String::from(path.to_str().unwrap())
}