hyperlane_log/log/
thread.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
use crate::{Log, LogArcLock};
use lazy_static::lazy_static;
use recoverable_spawn::*;
use std::sync::{Arc, RwLock};

lazy_static! {
    static ref LOG: LogArcLock = Arc::new(RwLock::new(Log::default()));
}

#[inline]
pub fn log_run(log: &Log) -> JoinHandle<()> {
    let log_clone: Log = log.clone();
    {
        if let Ok(mut log) = LOG.write() {
            *log = log_clone;
        }
    }
    let log_thread: JoinHandle<()> = recoverable_spawn(move || {
        let error_log_thread: JoinHandle<()> = recoverable_spawn(move || loop {
            if let Ok(log) = LOG.read() {
                log.write_error();
            }
        });
        let info_log_thread: JoinHandle<()> = recoverable_spawn(move || loop {
            if let Ok(log) = LOG.read() {
                log.write_info();
            }
        });
        let debug_log_thread: JoinHandle<()> = recoverable_spawn(move || loop {
            if let Ok(log) = LOG.read() {
                log.write_debug();
            }
        });
        let _ = error_log_thread.join();
        let _ = info_log_thread.join();
        let _ = debug_log_thread.join();
    });
    log_thread
}