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_sync<L>(&self, data: &str, func: L, dir: &str) -> &Self
29    where
30        L: LogFuncTrait,
31    {
32        if self.is_disable() {
33            return self;
34        }
35        let out: String = func(data);
36        let path = get_log_path(dir, self.get_path(), self.get_limit_file_size());
37        let _ = append_to_file(&path, &out.as_bytes());
38        self
39    }
40
41    async fn write_async<L>(&self, data: &str, func: L, dir: &str) -> &Self
42    where
43        L: LogFuncTrait,
44    {
45        if self.is_disable() {
46            return self;
47        }
48        let out: String = func(data);
49        let path = get_log_path(dir, self.get_path(), self.get_limit_file_size());
50        let _ = async_append_to_file(&path, &out.as_bytes()).await;
51        self
52    }
53
54    pub fn error<L>(&self, data: &str, func: L) -> &Self
55    where
56        L: LogFuncTrait,
57    {
58        self.write_sync(data, func, ERROR_DIR)
59    }
60
61    pub async fn async_error<L>(&self, data: &str, func: L) -> &Self
62    where
63        L: LogFuncTrait,
64    {
65        self.write_async(data, func, ERROR_DIR).await
66    }
67
68    pub fn info<L>(&self, data: &str, func: L) -> &Self
69    where
70        L: LogFuncTrait,
71    {
72        self.write_sync(data, func, INFO_DIR)
73    }
74
75    pub async fn async_info<L>(&self, data: &str, func: L) -> &Self
76    where
77        L: LogFuncTrait,
78    {
79        self.write_async(data, func, INFO_DIR).await
80    }
81
82    pub fn debug<L>(&self, data: &str, func: L) -> &Self
83    where
84        L: LogFuncTrait,
85    {
86        self.write_sync(data, func, DEBUG_DIR)
87    }
88
89    pub async fn async_debug<L>(&self, data: &str, func: L) -> &Self
90    where
91        L: LogFuncTrait,
92    {
93        self.write_async(data, func, DEBUG_DIR).await
94    }
95}