logger_rust/tracer_config/
mod.rs

1//! It is not already used, it just an template for future using in log_trace! macro.
2/// I'll be used as a tracing configuration:
3/// ```rust
4/// log_trace_conf(LogTrace::TracerConfiguration::new(
5///   Context: enabled;
6///   Timestamp: enabled;
7///   Timestamp_Type: chrono;
8///   File: enabled;
9///   Line: enabled;
10///   Format: "{}:{} - used: {} ->> ({:?}): {:?} ->> Thread ID: {:?} Timestamp: {}{}";
11/// ));
12/// ```
13use std::sync::RwLock;
14use std::time::UNIX_EPOCH;
15use std::time::SystemTime;
16
17use crate::current_time;
18
19lazy_static::lazy_static! {
20    /// A global instance of `TracerConfiguration` that can be accessed and modified using the `set_tracer_config` and `get_tracer_config` functions.
21    static ref TRACER_CONFIG: RwLock<TracerConfiguration> = RwLock::new(Default::default());
22}
23
24/// Sets the global tracer configuration to the given value.
25///
26/// # Arguments
27///
28/// * `config` - The new tracer configuration.
29pub fn set_tracer_config(config: TracerConfiguration) {
30    *TRACER_CONFIG.write().unwrap() = config;
31}
32
33/// Returns a copy of the current global tracer configuration.
34pub fn get_tracer_config() -> TracerConfiguration {
35    (*TRACER_CONFIG.read().unwrap()).clone()
36}
37
38/// An enum representing the different types of timestamps that can be used in log messages.
39#[derive(Clone, Copy)]
40pub enum TimestampType {
41    /// A timestamp in the format "YYYY-MM-DD HH:MM:SS".
42    Chrono,
43    /// A Unix timestamp (the number of seconds since January 1, 1970).
44    Unix,
45}
46
47/// A struct representing the configuration options for the tracer.
48#[derive(Clone, Default)]
49pub struct TracerConfiguration {
50    /// Whether to include context information in log messages. If `None`, the default behavior is used.
51    pub context_enabled: Option<bool>,
52    /// Whether to include a timestamp in log messages. If `None`, the default behavior is used.
53    pub timestamp_enabled: Option<bool>,
54    /// The type of timestamp to use in log messages. If `None`, the default behavior is used.
55    pub timestamp_type: Option<TimestampType>,
56    /// Whether to include the file name in log messages. If `None`, the default behavior is used.
57    pub file_enabled: Option<bool>,
58    /// Whether to include the line number in log messages. If `None`, the default behavior is used.
59    pub line_enabled: Option<bool>,
60    /// The format string to use when generating log messages. If `None`, the default behavior is used.
61    pub format: Option<String>,
62}
63
64impl TracerConfiguration {
65    /// Creates a new `TracerConfiguration` with default values.
66    pub fn new() -> Self {
67        Default::default()
68    }
69}
70
71/// A trait for types that can be logged using the tracer.
72pub trait Loggable {
73    /// Returns a string representation of the value that can be included in a log message.
74    fn log_behavior(&self) -> String;
75}
76
77/// A trait for types that have a name that can be included in log messages.
78pub trait TypeName {
79    /// Returns the name of the type as a string.
80    fn type_name(&self) -> &'static str;
81}
82
83impl<T: 'static> TypeName for T {
84    fn type_name(&self) -> &'static str {
85        std::any::type_name::<T>()
86    }
87}
88
89/// A trait for accessing information about the current state of the program that can be included in log messages.
90pub trait TracerConfig {
91    /// Returns a string representation of the current time.
92    fn now() -> String;
93    /// Returns the current line number.
94    fn line() -> u32;
95    /// Returns the name of the current file.
96    fn file() -> &'static str;
97    /// Returns the path of the current module.
98    fn module_path() -> &'static str;
99    /// Returns the ID of the current thread.
100    fn thread_id() -> std::thread::ThreadId;
101    /// Returns a timestamp representing the current time.
102    fn timestamp() -> u128;
103}
104
105impl TracerConfig for () {
106    fn now() -> String {
107        current_time()
108    }
109
110    fn line() -> u32 {
111        line!()
112    }
113
114    fn file() -> &'static str {
115        file!()
116    }
117
118    fn module_path() -> &'static str {
119        module_path!()
120    }
121
122    fn thread_id() -> std::thread::ThreadId {
123        std::thread::current().id()
124    }
125
126    fn timestamp() -> u128 {
127        SystemTime::now()
128            .duration_since(UNIX_EPOCH)
129            .unwrap()
130            .as_micros()
131    }
132}