use std::sync::RwLock;
use std::time::UNIX_EPOCH;
use std::time::SystemTime;
use crate::current_time;
lazy_static::lazy_static! {
static ref TRACER_CONFIG: RwLock<TracerConfiguration> = RwLock::new(Default::default());
}
pub fn set_tracer_config(config: TracerConfiguration) {
*TRACER_CONFIG.write().unwrap() = config;
}
pub fn get_tracer_config() -> TracerConfiguration {
(*TRACER_CONFIG.read().unwrap()).clone()
}
#[derive(Clone, Copy)]
pub enum TimestampType {
Chrono,
Unix,
}
#[derive(Clone, Default)]
pub struct TracerConfiguration {
pub context_enabled: Option<bool>,
pub timestamp_enabled: Option<bool>,
pub timestamp_type: Option<TimestampType>,
pub file_enabled: Option<bool>,
pub line_enabled: Option<bool>,
pub format: Option<String>,
}
impl TracerConfiguration {
pub fn new() -> Self {
Default::default()
}
}
pub trait Loggable {
fn log_behavior(&self) -> String;
}
pub trait TypeName {
fn type_name(&self) -> &'static str;
}
impl<T: 'static> TypeName for T {
fn type_name(&self) -> &'static str {
std::any::type_name::<T>()
}
}
pub trait TracerConfig {
fn now() -> String;
fn line() -> u32;
fn file() -> &'static str;
fn module_path() -> &'static str;
fn thread_id() -> std::thread::ThreadId;
fn timestamp() -> u128;
}
impl TracerConfig for () {
fn now() -> String {
current_time()
}
fn line() -> u32 {
line!()
}
fn file() -> &'static str {
file!()
}
fn module_path() -> &'static str {
module_path!()
}
fn thread_id() -> std::thread::ThreadId {
std::thread::current().id()
}
fn timestamp() -> u128 {
SystemTime::now()
.duration_since(UNIX_EPOCH)
.unwrap()
.as_micros()
}
}