2020-12-15 10:53:31 +00:00
|
|
|
// Take a look at the license at the top of the repository in the LICENSE file.
|
2017-12-20 17:52:57 +00:00
|
|
|
|
2020-01-10 11:33:03 +00:00
|
|
|
use thiserror::Error;
|
2017-12-20 17:52:57 +00:00
|
|
|
|
2021-04-26 12:15:53 +00:00
|
|
|
use glib::prelude::*;
|
2017-12-20 17:52:57 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
2020-12-20 15:09:22 +00:00
|
|
|
macro_rules! error_msg(
|
2017-12-20 17:52:57 +00:00
|
|
|
// Plain strings
|
|
|
|
($err:expr, ($msg:expr), [$dbg:expr]) => {
|
|
|
|
$crate::ErrorMessage::new(&$err, Some($msg),
|
|
|
|
Some($dbg),
|
|
|
|
file!(), module_path!(), line!())
|
|
|
|
};
|
|
|
|
($err:expr, ($msg:expr)) => {
|
|
|
|
$crate::ErrorMessage::new(&$err, Some($msg),
|
|
|
|
None,
|
|
|
|
file!(), module_path!(), line!())
|
|
|
|
};
|
|
|
|
($err:expr, [$dbg:expr]) => {
|
|
|
|
$crate::ErrorMessage::new(&$err, None,
|
|
|
|
Some($dbg),
|
|
|
|
file!(), module_path!(), line!())
|
|
|
|
};
|
|
|
|
|
|
|
|
// Format strings
|
|
|
|
($err:expr, ($($msg:tt)*), [$($dbg:tt)*]) => { {
|
|
|
|
$crate::ErrorMessage::new(&$err, Some(format!($($msg)*).as_ref()),
|
|
|
|
Some(format!($($dbg)*).as_ref()),
|
|
|
|
file!(), module_path!(), line!())
|
|
|
|
}};
|
|
|
|
($err:expr, ($($msg:tt)*)) => { {
|
|
|
|
$crate::ErrorMessage::new(&$err, Some(format!($($msg)*).as_ref()),
|
|
|
|
None,
|
|
|
|
file!(), module_path!(), line!())
|
|
|
|
}};
|
|
|
|
|
|
|
|
($err:expr, [$($dbg:tt)*]) => { {
|
|
|
|
$crate::ErrorMessage::new(&$err, None,
|
|
|
|
Some(format!($($dbg)*).as_ref()),
|
|
|
|
file!(), module_path!(), line!())
|
|
|
|
}};
|
|
|
|
);
|
|
|
|
|
2020-01-10 11:33:03 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq, Error)]
|
|
|
|
#[error("Error {:?} ({:?}) at {}:{}", .message, .debug, .filename, .line)]
|
2017-12-20 17:52:57 +00:00
|
|
|
pub struct ErrorMessage {
|
2018-07-30 08:46:40 +00:00
|
|
|
pub(crate) error_domain: glib::Quark,
|
2017-12-20 17:52:57 +00:00
|
|
|
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 {
|
2020-11-21 13:46:48 +00:00
|
|
|
pub fn new<T: crate::MessageErrorDomain>(
|
2017-12-20 17:52:57 +00:00
|
|
|
error: &T,
|
2019-05-23 18:19:24 +00:00
|
|
|
message: Option<&str>,
|
|
|
|
debug: Option<&str>,
|
2017-12-20 17:52:57 +00:00
|
|
|
filename: &'static str,
|
|
|
|
function: &'static str,
|
|
|
|
line: u32,
|
|
|
|
) -> ErrorMessage {
|
2020-03-22 14:18:47 +00:00
|
|
|
assert_initialized_main_thread!();
|
2018-07-20 07:21:06 +00:00
|
|
|
let error_domain = T::domain();
|
|
|
|
let error_code = error.code();
|
2017-12-20 17:52:57 +00:00
|
|
|
|
|
|
|
ErrorMessage {
|
2018-07-20 07:21:06 +00:00
|
|
|
error_domain,
|
|
|
|
error_code,
|
2017-12-20 17:52:57 +00:00
|
|
|
message: message.map(String::from),
|
|
|
|
debug: debug.map(String::from),
|
2018-07-20 07:21:06 +00:00
|
|
|
filename,
|
|
|
|
function,
|
|
|
|
line,
|
2017-12-20 17:52:57 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-01-24 19:33:31 +00:00
|
|
|
#[macro_export]
|
2020-12-20 15:09:22 +00:00
|
|
|
macro_rules! loggable_error(
|
2019-01-24 19:33:31 +00:00
|
|
|
// Plain strings
|
|
|
|
($cat:expr, $msg:expr) => {
|
2020-12-17 22:38:06 +00:00
|
|
|
$crate::LoggableError::new($cat.clone(), $crate::glib::bool_error!($msg))
|
2019-01-24 19:33:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Format strings
|
|
|
|
($cat:expr, $($msg:tt)*) => { {
|
2020-12-17 22:38:06 +00:00
|
|
|
$crate::LoggableError::new($cat.clone(), $crate::glib::bool_error!($($msg)*))
|
2019-01-24 19:33:31 +00:00
|
|
|
}};
|
|
|
|
);
|
|
|
|
|
|
|
|
#[macro_export]
|
2020-12-20 15:09:22 +00:00
|
|
|
macro_rules! result_from_gboolean(
|
2019-01-24 19:33:31 +00:00
|
|
|
// Plain strings
|
2020-11-21 13:46:48 +00:00
|
|
|
($ffi_bool:expr, $cat:expr, $msg:expr) => {
|
2020-12-17 22:38:06 +00:00
|
|
|
$crate::glib::result_from_gboolean!($ffi_bool, $msg)
|
2019-02-28 08:54:32 +00:00
|
|
|
.map_err(|bool_err| $crate::LoggableError::new($cat.clone(), bool_err))
|
2019-01-24 19:33:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Format strings
|
2020-11-21 13:46:48 +00:00
|
|
|
($ffi_bool:expr, $cat:expr, $($msg:tt)*) => { {
|
2020-12-17 22:38:06 +00:00
|
|
|
$crate::glib::result_from_gboolean!($ffi_bool, $($msg)*)
|
2019-02-28 08:54:32 +00:00
|
|
|
.map_err(|bool_err| $crate::LoggableError::new($cat.clone(), bool_err))
|
2019-01-24 19:33:31 +00:00
|
|
|
}};
|
|
|
|
);
|
|
|
|
|
2020-01-10 11:33:03 +00:00
|
|
|
#[derive(Debug, Clone, Error)]
|
2021-04-12 10:01:22 +00:00
|
|
|
#[error("Error {:?}: {:?} at {}:{}", .category.name(), .bool_error.message, .bool_error.filename, .bool_error.line)]
|
2019-01-24 19:33:31 +00:00
|
|
|
pub struct LoggableError {
|
2020-11-21 13:46:48 +00:00
|
|
|
category: crate::DebugCategory,
|
2019-01-24 19:33:31 +00:00
|
|
|
bool_error: glib::BoolError,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LoggableError {
|
2020-11-21 13:46:48 +00:00
|
|
|
pub fn new(category: crate::DebugCategory, bool_error: glib::BoolError) -> LoggableError {
|
2020-03-22 14:18:47 +00:00
|
|
|
assert_initialized_main_thread!();
|
2019-01-24 19:33:31 +00:00
|
|
|
LoggableError {
|
2019-02-28 08:54:32 +00:00
|
|
|
category,
|
2019-01-24 19:33:31 +00:00
|
|
|
bool_error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn log(&self) {
|
|
|
|
self.category.log(
|
2020-02-22 16:35:02 +00:00
|
|
|
None as Option<&glib::Object>,
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::DebugLevel::Error,
|
2019-01-24 19:33:31 +00:00
|
|
|
self.bool_error.filename,
|
|
|
|
self.bool_error.function,
|
|
|
|
self.bool_error.line,
|
|
|
|
format_args!("{}", self.bool_error.message),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-02-22 16:35:02 +00:00
|
|
|
pub fn log_with_object<O: IsA<glib::Object>>(&self, obj: &O) {
|
2019-01-24 19:33:31 +00:00
|
|
|
self.category.log(
|
|
|
|
Some(obj),
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::DebugLevel::Error,
|
2019-01-24 19:33:31 +00:00
|
|
|
self.bool_error.filename,
|
|
|
|
self.bool_error.function,
|
|
|
|
self.bool_error.line,
|
|
|
|
format_args!("{}", self.bool_error.message),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2022-10-09 08:49:28 +00:00
|
|
|
pub fn log_with_imp<I: glib::subclass::prelude::ObjectSubclass>(&self, imp: &I) {
|
|
|
|
use glib::subclass::prelude::*;
|
|
|
|
|
|
|
|
self.category.log(
|
|
|
|
Some(unsafe { imp.instance().unsafe_cast_ref::<glib::Object>() }),
|
|
|
|
crate::DebugLevel::Error,
|
|
|
|
self.bool_error.filename,
|
|
|
|
self.bool_error.function,
|
|
|
|
self.bool_error.line,
|
|
|
|
format_args!("{}", self.bool_error.message),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2020-11-21 13:46:48 +00:00
|
|
|
pub fn category(&self) -> crate::DebugCategory {
|
2019-01-24 19:33:31 +00:00
|
|
|
self.category
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<glib::BoolError> for LoggableError {
|
|
|
|
fn from(bool_error: glib::BoolError) -> Self {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2019-01-24 19:33:31 +00:00
|
|
|
LoggableError {
|
2020-11-21 13:46:48 +00:00
|
|
|
category: *crate::CAT_RUST,
|
2019-01-24 19:33:31 +00:00
|
|
|
bool_error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-10 13:11:04 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn error_message() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2020-04-11 16:57:09 +00:00
|
|
|
|
2020-01-10 13:11:04 +00:00
|
|
|
let e = ErrorMessage::new(
|
2020-11-21 13:46:48 +00:00
|
|
|
&crate::CoreError::Failed,
|
2020-01-10 13:11:04 +00:00
|
|
|
Some("message"),
|
|
|
|
Some("debug"),
|
|
|
|
"filename",
|
|
|
|
"function",
|
|
|
|
7,
|
|
|
|
);
|
|
|
|
assert_eq!(
|
|
|
|
format!("{}", e),
|
|
|
|
"Error Some(\"message\") (Some(\"debug\")) at filename:7"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn logabble_error() {
|
2020-11-21 13:46:48 +00:00
|
|
|
crate::init().unwrap();
|
2020-04-11 16:57:09 +00:00
|
|
|
|
2020-01-10 13:11:04 +00:00
|
|
|
let e: LoggableError = glib::BoolError::new("msg", "filename", "function", 7).into();
|
|
|
|
assert_eq!(
|
|
|
|
format!("{}", e),
|
|
|
|
"Error \"GST_RUST\": \"msg\" at filename:7"
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|