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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
use log::{LevelFilter, Log, Metadata, Record};
use std::ops::{Deref};
use std::sync::atomic::{AtomicI32, AtomicI64, Ordering};

use crate::appender::{Command, FastLogRecord};
use crate::config::Config;
use crate::error::LogError;
use crate::filter::Filter;
use crate::{chan, spawn, Receiver, Sender};
use crossbeam_channel::SendError;
use crossbeam_utils::sync::WaitGroup;
use once_cell::sync::{Lazy, OnceCell};
use std::result::Result::Ok;
use std::sync::Arc;
use std::time::SystemTime;

pub struct Chan {
    pub filter: OnceCell<Box<dyn Filter>>,
    pub send: Sender<FastLogRecord>,
    pub recv: Receiver<FastLogRecord>,
}

impl Chan {
    pub fn new(len: Option<usize>) -> Self {
        let (s, r) = chan(len);
        Chan {
            filter: OnceCell::new(),
            send: s,
            recv: r,
        }
    }
    pub fn set_filter(&self, f: Box<dyn Filter>) {
        self.filter.get_or_init(|| f);
        self.filter.get();
    }
}

pub struct Logger {
    level: AtomicI32,
    pub chan: Chan,
}

impl Logger {
    pub fn set_level(&self, level: LevelFilter) {
        self.level
            .swap(level as i32, std::sync::atomic::Ordering::Relaxed);
        log::set_max_level(level);
    }

    pub fn get_level(&self) -> LevelFilter {
        match self.level.load(std::sync::atomic::Ordering::Relaxed) {
            0 => LevelFilter::Off,
            1 => LevelFilter::Error,
            2 => LevelFilter::Warn,
            3 => LevelFilter::Info,
            4 => LevelFilter::Debug,
            5 => LevelFilter::Trace,
            _ => panic!("error log level!"),
        }
    }

    /// print no other info
    pub fn print(&self, log: String) -> Result<(), SendError<FastLogRecord>> {
        let fast_log_record = FastLogRecord {
            command: Command::CommandRecord,
            level: log::Level::Info,
            target: "".to_string(),
            args: "".to_string(),
            module_path: "".to_string(),
            file: "".to_string(),
            line: None,
            now: SystemTime::now(),
            formated: log,
        };
        LOGGER.chan.send.send(fast_log_record)
    }

    pub fn wait(&self) {
        self.flush();
    }
}

impl Log for Logger {
    fn enabled(&self, metadata: &Metadata) -> bool {
        metadata.level() <= self.get_level()
    }
    fn log(&self, record: &Record) {
        //send
        let f = LOGGER.chan.filter.get();
        if f.is_some() {
            if !f.as_ref().unwrap().filter(record) {
                let fast_log_record = FastLogRecord {
                    command: Command::CommandRecord,
                    level: record.level(),
                    target: record.metadata().target().to_string(),
                    args: record.args().to_string(),
                    module_path: record.module_path().unwrap_or_default().to_string(),
                    file: record.file().unwrap_or_default().to_string(),
                    line: record.line().clone(),
                    now: SystemTime::now(),
                    formated: String::new(),
                };
                LOGGER.chan.send.send(fast_log_record);
            }
        }
    }
    fn flush(&self) {
        match flush() {
            Ok(v) => {
                v.wait();
            }
            Err(_) => {}
        }
    }
}

static CHAN_LEN: AtomicI64 = AtomicI64::new(-1);
pub static LOGGER: Lazy<Logger> = Lazy::new(|| Logger {
    level: AtomicI32::new(1),
    chan: Chan::new({
        let len = CHAN_LEN.load(Ordering::SeqCst);
        match len {
            -1 => None,
            v => Some(v as usize),
        }
    }),
});

pub fn init(config: Config) -> Result<&'static Logger, LogError> {
    if config.appends.is_empty() {
        return Err(LogError::from("[fast_log] appenders can not be empty!"));
    }
    match config.chan_len {
        None => {
            CHAN_LEN.store(-1, Ordering::SeqCst);
        }
        Some(v) => {
            CHAN_LEN.store(v as i64, Ordering::SeqCst);
        }
    }
    LOGGER.set_level(config.level);
    LOGGER.chan.set_filter(config.filter);
    //main recv data
    let appenders = config.appends;
    let format = Arc::new(config.format);
    let level = config.level;
    let chan_len = config.chan_len;
    std::thread::spawn(move || {
        let mut recever_vec = vec![];
        let mut sender_vec: Vec<Sender<Arc<Vec<FastLogRecord>>>> = vec![];
        for a in appenders {
            let (s, r) = chan(chan_len);
            sender_vec.push(s);
            recever_vec.push((r, a));
        }
        for (recever, appender) in recever_vec {
            spawn(move || {
                let mut exit = false;
                loop {
                    //batch fetch
                    if let Ok(msg) = recever.recv() {
                        appender.do_logs(msg.as_ref());
                        for x in msg.iter() {
                            match x.command {
                                Command::CommandRecord => {}
                                Command::CommandExit => {
                                    exit = true;
                                    continue;
                                }
                                Command::CommandFlush(_) => {
                                    appender.flush();
                                    continue;
                                }
                            }
                        }
                    }
                    if exit {
                        break;
                    }
                }
            });
        }
        loop {
            //recv
            let data = LOGGER.chan.recv.recv();
            if let Ok(data) = data {
                let mut remain;
                if LOGGER.chan.recv.len() > 0 {
                    remain = Vec::with_capacity(LOGGER.chan.recv.len() + 1);
                    remain.push(data);
                    recv_all(&mut remain, &LOGGER.chan.recv);
                } else {
                    remain = vec![data];
                }
                let mut exit = false;
                for x in &mut remain {
                    if x.formated.is_empty() {
                        x.formated = format.do_format(x);
                    }
                    if x.command.eq(&Command::CommandExit) {
                        exit = true;
                    }
                }
                let data = Arc::new(remain);
                for x in &sender_vec {
                    x.send(data.clone());
                }
                if exit {
                    break;
                }
            }
        }
    });
    let r = log::set_logger(LOGGER.deref()).map(|()| log::set_max_level(level));
    if r.is_err() {
        return Err(LogError::from(r.err().unwrap()));
    } else {
        return Ok(LOGGER.deref());
    }
}

fn recv_all<T>(data: &mut Vec<T>, recver: &Receiver<T>) {
    loop {
        match recver.try_recv() {
            Ok(v) => {
                data.push(v);
            }
            Err(_) => {
                break;
            }
        }
    }
}

pub fn exit() -> Result<(), LogError> {
    let fast_log_record = FastLogRecord {
        command: Command::CommandExit,
        level: log::Level::Info,
        target: String::new(),
        args: String::new(),
        module_path: String::new(),
        file: String::new(),
        line: None,
        now: SystemTime::now(),
        formated: String::new(),
    };
    let result = LOGGER.chan.send.send(fast_log_record);
    match result {
        Ok(()) => {
            return Ok(());
        }
        _ => {}
    }
    return Err(LogError::E("[fast_log] exit fail!".to_string()));
}

pub fn flush() -> Result<WaitGroup, LogError> {
    let wg = WaitGroup::new();
    let fast_log_record = FastLogRecord {
        command: Command::CommandFlush(wg.clone()),
        level: log::Level::Info,
        target: String::new(),
        args: String::new(),
        module_path: String::new(),
        file: String::new(),
        line: None,
        now: SystemTime::now(),
        formated: String::new(),
    };
    let result = LOGGER.chan.send.send(fast_log_record);
    match result {
        Ok(()) => {
            return Ok(wg);
        }
        _ => {}
    }
    return Err(LogError::E("[fast_log] flush fail!".to_string()));
}

pub fn print(log: String) -> Result<(), SendError<FastLogRecord>> {
    LOGGER.print(log)
}