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
// wengwengweng

use std::panic;
use std::panic::PanicInfo;

pub struct ErrorInfo {
	pub message: Option<String>,
	pub location: Option<ErrorLocation>,
}

pub struct ErrorLocation {
	pub file: String,
	pub line: u32,
}

pub fn set<F: 'static + Fn(ErrorInfo) + Send + Sync>(f: F) {

	panic::set_hook(Box::new(move |info: &PanicInfo| {

		let mut message = None;

		if let Some(s) = info.payload().downcast_ref::<&str>() {
			message = Some((*s).to_owned());
		} else if let Some(s) = info.payload().downcast_ref::<String>() {
			message = Some(s.clone());
		}

		let mut location = None;

		if let Some(loc) = info.location() {

			let file = loc.file();
			let line = loc.line();

			location = Some(ErrorLocation {
				file: file.to_owned(),
				line: line,
			});

		}

		f(ErrorInfo {
			message: message,
			location: location,
		});

	}));

}