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
use super::panic::*;
use crate::bug_report::*;
use cyfs_base::*;
use cyfs_util::*;

use backtrace::Backtrace;
use chrono::offset::Local;
use chrono::DateTime;
use std::panic;
use std::path::PathBuf;
use std::sync::Arc;
use once_cell::sync::OnceCell;

// 触发了panic
pub type FnOnPanic = dyn EventListenerAsyncRoutine<CyfsPanicInfo, ()>;
type OnPanicEventManager = SyncEventManagerSync<CyfsPanicInfo, ()>;

pub trait BugReportHandler: Send + Sync {
    fn notify(&self, product_name: &str, service_name: &str, panic_info: &CyfsPanicInfo) -> BuckyResult<()>;
}

struct PanicManagerImpl {
    product_name: String,
    service_name: String,

    log_to_file: bool,
    log_dir: PathBuf,

    exit_on_panic: bool,

    on_panic: OnPanicEventManager,

    // 上报器
    reporter: OnceCell<Box<dyn BugReportHandler>>,
}

impl PanicManagerImpl {
    pub fn new(builder: PanicBuilder) -> Self {
    
        Self {
            product_name: builder.product_name,
            service_name: builder.service_name,
            log_to_file: builder.log_to_file,
            log_dir: builder.log_dir,
            exit_on_panic: builder.exit_on_panic,

            on_panic: OnPanicEventManager::new(),
            reporter: OnceCell::new(),
        }
    }
}

#[derive(Clone)]
pub struct PanicManager(Arc<PanicManagerImpl>);

impl PanicManager {
    pub fn new(builder: PanicBuilder) -> Self {
        Self(Arc::new(PanicManagerImpl::new(builder)))
    }

    pub fn start(&self) {
        let this = self.clone();
        panic::set_hook(Box::new(move |info| {
            let backtrace = Backtrace::new();
            let pinfo = CyfsPanicInfo::new(backtrace, info);
            let this = this.clone();

            std::thread::spawn(move || {
                this.on_panic(pinfo);
            });
        }));
    }

    pub fn event(&self) ->  OnPanicEventManager {
        self.0.on_panic.clone()
    }

    fn on_panic(&self, info: CyfsPanicInfo) {
        if self.0.log_to_file {
            self.log_to_file(&info);
        }

        if let Some(reporter) = self.0.reporter.get() {
            let _ = reporter.notify(&self.0.product_name, &self.0.service_name, &info);
        }

        // 触发事件
        let _ = self.0.on_panic.emit(&info);

        if self.0.exit_on_panic {
            crate::CyfsLogger::flush();
            
            error!("process will exit on panic......");
            std::thread::sleep(std::time::Duration::from_secs(3));
            error!("process exit on panic......");
            std::process::exit(-1);
        }
    }

    fn log_to_file(&self, info: &CyfsPanicInfo) {
        if let Err(e) = std::fs::create_dir_all(&self.0.log_dir) {
            error!(
                "create panic dir failed! dir={}, {}",
                self.0.log_dir.display(),
                e
            );
            return;
        }

        let file_name = format!("{}_panic_{}.log", self.0.service_name, info.hash);

        let now = std::time::SystemTime::now();
        let datetime: DateTime<Local> = now.into();
        let now = datetime.format("%Y_%m_%d %H:%M:%S.%f");

        let content;
        #[cfg(debug_assertions)]
        {
            content = format!("{}\n{}", now, info.msg_with_symbol);
        }

        #[cfg(not(debug_assertions))]
        {
            content = format!("{}\n{}", now, info.msg);
        }

        let file_path = self.0.log_dir.join(file_name);
        if let Err(e) = std::fs::write(&file_path, content) {
            error!("write panic log failed! dir={}, {}", file_path.display(), e);
        }
    }
}

pub struct PanicBuilder {
    product_name: String,
    service_name: String,

    log_to_file: bool,
    log_dir: PathBuf,

    bug_reporter: Option<Box<dyn BugReportHandler>>,

    // panic后是否结束进程
    exit_on_panic: bool,
}

impl PanicBuilder {
    pub fn new(product_name: &str, service_name: &str) -> Self {
        assert!(!product_name.is_empty());
        assert!(!service_name.is_empty());

        let mut root = get_cyfs_root_path();
        root.push("log/panic");
        root.push(product_name);

        Self {
            product_name: product_name.to_owned(),
            service_name: service_name.to_owned(),
            log_to_file: true,
            log_dir: root,
            bug_reporter: None,
            exit_on_panic: false,
        }
    }

    // panic信息是否输出到日志文件,默认输出
    pub fn log_to_file(mut self, enable: bool) -> Self {
        self.log_to_file = enable;
        self
    }

    // panic输出到的日志目录,默认是{cyfs_root}/log/panic/{product_name}/
    pub fn log_dir(mut self, log_dir: impl Into<PathBuf>) -> Self {
        self.log_dir = log_dir.into();
        self
    }

    pub fn bug_report(mut self, handler: Box<dyn BugReportHandler>) -> Self {
        self.bug_reporter = Some(handler);
        self
    }

    // use default http bug_report impl for 
    pub fn http_bug_report(mut self, url: &str) -> Self {
        let handler = BugReporter::new(url);
        self.bug_reporter = Some(Box::new(handler));
        self
    }

    
    // panic后是否结束进程,默认不结束
    pub fn exit_on_panic(mut self, exit: bool) -> Self {
        self.exit_on_panic = exit;
        self
    }

    pub fn build(self) -> PanicManager {
        PanicManager::new(self)
    }
}