hyperlane_log/log/
impl.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
use super::{
    constant::*,
    r#trait::{LogDataTrait, LogFuncTrait},
    r#type::{Log, LogListArcLock},
    utils::*,
};
use crate::BoxLogFunc;
use file_operation::*;
use http_type::*;
use hyperlane_time::*;
use lazy_static::lazy_static;
use std::sync::{Arc, RwLock};

lazy_static! {
    static ref LOG_ERROR_QUEUE: LogListArcLock = Arc::new(RwLock::new(Vec::new()));
    static ref LOG_INFO_QUEUE: LogListArcLock = Arc::new(RwLock::new(Vec::new()));
    static ref LOG_DEBUG_QUEUE: LogListArcLock = Arc::new(RwLock::new(Vec::new()));
}

impl Default for Log {
    #[inline]
    fn default() -> Self {
        Self {
            path: DEFAULT_LOG_DIR.to_owned(),
            file_size: DEFAULT_LOG_FILE_SIZE,
        }
    }
}

impl Log {
    #[inline]
    pub fn new<T>(path: T, file_size: usize) -> Self
    where
        T: Into<String>,
    {
        Self {
            path: path.into(),
            file_size,
        }
    }

    #[inline]
    fn write(list: &mut Vec<(String, BoxLogFunc)>, path: &str) {
        for (log_string, func) in list.iter() {
            let out: String = func(log_string);
            let _ = write_to_file(path, &out.as_bytes());
        }
    }

    #[inline]
    fn add_data<T, L>(log_queue: &LogListArcLock, data: T, func: L)
    where
        T: LogDataTrait,
        L: LogFuncTrait,
    {
        let data_string: String = data.into();
        {
            if let Ok(mut queue) = log_queue.write() {
                queue.push((data_string, Box::new(func)));
            }
        }
    }

    #[inline]
    fn get_file_name(&self, idx: usize) -> String {
        format!(
            "{}{}{}{}{}{}",
            ROOT_PATH,
            current_date(),
            POINT,
            idx,
            POINT,
            LOG_EXTION
        )
    }

    #[inline]
    fn get_file_dir_name(&self) -> String {
        format!("{}{}", ROOT_PATH, current_date())
    }

    #[inline]
    fn get_log_path(&self, system_dir: &str) -> String {
        let base_path: &String = self.get_path();
        let mut combined_path: String = base_path.trim_end_matches(ROOT_PATH).to_string();
        if !system_dir.starts_with(ROOT_PATH) {
            combined_path.push_str(ROOT_PATH);
        }
        combined_path.push_str(
            system_dir
                .trim_start_matches(ROOT_PATH)
                .trim_end_matches(ROOT_PATH),
        );
        combined_path.push_str(&self.get_file_dir_name());
        let idx: usize = get_second_element_from_filename(&combined_path);
        let mut combined_path_clone: String = combined_path.clone();
        combined_path.push_str(&self.get_file_name(idx));
        let file_size: usize = get_file_size(&combined_path).unwrap_or_default() as usize;
        if file_size <= self.file_size {
            return combined_path;
        }
        combined_path_clone.push_str(&self.get_file_name(idx + 1));
        combined_path_clone
    }

    #[inline]
    pub(super) fn write_error(&self) {
        if let Ok(mut error) = LOG_ERROR_QUEUE.write() {
            Self::write(&mut *error, &self.get_log_path(ERROR_DIR));
            error.clear();
        }
    }

    #[inline]
    pub(super) fn write_info(&self) {
        if let Ok(mut info) = LOG_INFO_QUEUE.write() {
            Self::write(&mut *info, &self.get_log_path(INFO_DIR));
            info.clear();
        }
    }

    #[inline]
    pub(super) fn write_debug(&self) {
        if let Ok(mut debug) = LOG_DEBUG_QUEUE.write() {
            Self::write(&mut *debug, &self.get_log_path(DEBUG_DIR));
            debug.clear();
        }
    }

    #[inline]
    pub fn log_error<T, L>(&self, data: T, func: L)
    where
        T: LogDataTrait,
        L: LogFuncTrait,
    {
        Self::add_data(&LOG_ERROR_QUEUE, data, func);
    }

    #[inline]
    pub fn log_info<T, L>(&self, data: T, func: L)
    where
        T: LogDataTrait,
        L: LogFuncTrait,
    {
        Self::add_data(&LOG_INFO_QUEUE, data, func);
    }

    #[inline]
    pub fn log_debug<T, L>(&self, data: T, func: L)
    where
        T: LogDataTrait,
        L: LogFuncTrait,
    {
        Self::add_data(&LOG_DEBUG_QUEUE, data, func);
    }
}