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
// Take a look at the license at the top of the repository in the LICENSE file.

use glib::{prelude::*, translate::*};
use thiserror::Error;

#[macro_export]
macro_rules! error_msg(
    ($err:expr, ($($msg:tt)*), [$($dbg:tt)*]) =>  { {
        $crate::ErrorMessage::new(&$err, Some(format!($($msg)*).as_ref()),
                          Some(format!($($dbg)*).as_ref()),
                          file!(), $crate::glib::function_name!(), line!())
    }};
    ($err:expr, ($($msg:tt)*)) =>  { {
        $crate::ErrorMessage::new(&$err, Some(format!($($msg)*).as_ref()),
                          None,
                          file!(), $crate::glib::function_name!(), line!())
    }};

    ($err:expr, [$($dbg:tt)*]) =>  { {
        $crate::ErrorMessage::new(&$err, None,
                          Some(format!($($dbg)*).as_ref()),
                          file!(), $crate::glib::function_name!(), line!())
    }};
);

#[derive(Clone, Debug, PartialEq, Eq, Error)]
#[error("Error {:?} ({:?}) at {}:{}", .message, .debug, .filename, .line)]
pub struct ErrorMessage {
    pub(crate) error_domain: glib::Quark,
    pub(crate) error_code: i32,
    pub(crate) message: Option<String>,
    pub(crate) debug: Option<String>,
    pub(crate) filename: &'static str,
    pub(crate) function: &'static str,
    pub(crate) line: u32,
}

impl ErrorMessage {
    pub fn new<T: crate::MessageErrorDomain>(
        error: &T,
        message: Option<&str>,
        debug: Option<&str>,
        filename: &'static str,
        function: &'static str,
        line: u32,
    ) -> ErrorMessage {
        skip_assert_initialized!();
        let error_domain = T::domain();
        let error_code = error.code();

        ErrorMessage {
            error_domain,
            error_code,
            message: message.map(String::from),
            debug: debug.map(String::from),
            filename,
            function,
            line,
        }
    }
}

#[macro_export]
macro_rules! loggable_error(
    ($cat:expr, $($msg:tt)*) =>  { {
        $crate::LoggableError::new($cat.clone(), $crate::glib::bool_error!($($msg)*))
    }};
);

#[macro_export]
macro_rules! result_from_gboolean(
    ($ffi_bool:expr, $cat:expr, $($msg:tt)*) =>  { {
        $crate::glib::result_from_gboolean!($ffi_bool, $($msg)*)
            .map_err(|bool_err| $crate::LoggableError::new($cat.clone(), bool_err))
    }};
);

#[derive(Debug, Clone, Error)]
#[error("Error {:?}: {:?} at {}:{}", .category.name(), .bool_error.message, .bool_error.filename, .bool_error.line)]
pub struct LoggableError {
    category: crate::DebugCategory,
    bool_error: glib::BoolError,
}

impl LoggableError {
    pub fn new(category: crate::DebugCategory, bool_error: glib::BoolError) -> LoggableError {
        skip_assert_initialized!();
        LoggableError {
            category,
            bool_error,
        }
    }

    #[inline(never)]
    pub fn log(&self) {
        self.bool_error.filename.run_with_gstr(|filename| {
            self.category.log(
                None::<&glib::Object>,
                crate::DebugLevel::Error,
                filename,
                self.bool_error.function,
                self.bool_error.line,
                format_args!("{}", self.bool_error.message),
            );
        });
    }

    pub fn log_with_object(&self, obj: &impl IsA<glib::Object>) {
        self.log_with_object_internal(obj.as_ref());
    }

    #[inline(never)]
    fn log_with_object_internal(&self, obj: &glib::Object) {
        self.bool_error.filename.run_with_gstr(|filename| {
            self.category.log(
                Some(obj),
                crate::DebugLevel::Error,
                filename,
                self.bool_error.function,
                self.bool_error.line,
                format_args!("{}", self.bool_error.message),
            );
        });
    }

    pub fn log_with_imp(&self, imp: &impl glib::subclass::types::ObjectSubclass) {
        use glib::subclass::prelude::*;

        self.log_with_object_internal(unsafe { imp.obj().unsafe_cast_ref::<glib::Object>() });
    }

    pub fn category(&self) -> crate::DebugCategory {
        self.category
    }
}

impl From<glib::BoolError> for LoggableError {
    fn from(bool_error: glib::BoolError) -> Self {
        skip_assert_initialized!();
        LoggableError {
            category: *crate::CAT_RUST,
            bool_error,
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn error_message() {
        crate::init().unwrap();

        let e = ErrorMessage::new(
            &crate::CoreError::Failed,
            Some("message"),
            Some("debug"),
            "filename",
            "function",
            7,
        );
        assert_eq!(
            format!("{e}"),
            "Error Some(\"message\") (Some(\"debug\")) at filename:7"
        );
    }

    #[test]
    fn logabble_error() {
        crate::init().unwrap();

        let e: LoggableError = glib::BoolError::new("msg", "filename", "function", 7).into();
        assert_eq!(format!("{e}"), "Error \"GST_RUST\": \"msg\" at filename:7");
    }
}