hyperlane_log/log/
impl.rs

1use crate::*;
2
3impl Default for Log {
4    fn default() -> Self {
5        Self {
6            path: DEFAULT_LOG_DIR.to_owned(),
7            limit_file_size: DEFAULT_LOG_FILE_SIZE,
8        }
9    }
10}
11
12impl Log {
13    pub fn new(path: &str, limit_file_size: usize) -> Self {
14        Self {
15            path: path.into(),
16            limit_file_size,
17        }
18    }
19
20    pub fn is_enable(&self) -> bool {
21        self.get_limit_file_size() != &DISABLE_LOG_FILE_SIZE
22    }
23
24    pub fn is_disable(&self) -> bool {
25        !self.is_enable()
26    }
27
28    fn write_data<L>(&self, data: &str, func: L, path: String, is_sync: bool) -> &Self
29    where
30        L: LogFuncTrait,
31    {
32        if self.is_disable() {
33            return self;
34        }
35        let out: String = func(data);
36        if is_sync {
37            let _ = r#sync::run_function(move || {
38                let _ = append_to_file(&path, &out.as_bytes());
39            });
40        } else {
41            let _ = r#async::run_function(move || async move {
42                let _ = async_append_to_file(&path, &out.as_bytes()).await;
43            });
44        }
45        self
46    }
47
48    fn common_error<L>(&self, data: &str, func: L, is_sync: bool) -> &Self
49    where
50        L: LogFuncTrait,
51    {
52        self.write_data(
53            data,
54            func,
55            get_log_path(ERROR_DIR, self.get_path(), self.get_limit_file_size()),
56            is_sync,
57        )
58    }
59
60    fn common_info<L>(&self, data: &str, func: L, is_sync: bool) -> &Self
61    where
62        L: LogFuncTrait,
63    {
64        self.write_data(
65            data,
66            func,
67            get_log_path(INFO_DIR, self.get_path(), self.get_limit_file_size()),
68            is_sync,
69        )
70    }
71
72    fn common_debug<L>(&self, data: &str, func: L, is_sync: bool) -> &Self
73    where
74        L: LogFuncTrait,
75    {
76        self.write_data(
77            data,
78            func,
79            get_log_path(DEBUG_DIR, self.get_path(), self.get_limit_file_size()),
80            is_sync,
81        )
82    }
83
84    pub fn error<L>(&self, data: &str, func: L) -> &Self
85    where
86        L: LogFuncTrait,
87    {
88        self.common_error(data, func, true)
89    }
90
91    pub fn async_error<L>(&self, data: &str, func: L) -> &Self
92    where
93        L: LogFuncTrait,
94    {
95        self.common_error(data, func, false)
96    }
97
98    pub fn info<L>(&self, data: &str, func: L) -> &Self
99    where
100        L: LogFuncTrait,
101    {
102        self.common_info(data, func, true)
103    }
104
105    pub fn async_info<L>(&self, data: &str, func: L) -> &Self
106    where
107        L: LogFuncTrait,
108    {
109        self.common_info(data, func, false)
110    }
111
112    pub fn debug<L>(&self, data: &str, func: L) -> &Self
113    where
114        L: LogFuncTrait,
115    {
116        self.common_debug(data, func, true)
117    }
118
119    pub fn async_debug<L>(&self, data: &str, func: L) -> &Self
120    where
121        L: LogFuncTrait,
122    {
123        self.common_debug(data, func, false)
124    }
125}