2017-12-20 17:52:57 +00:00
|
|
|
// Copyright (C) 2016-2017 Sebastian Dröge <sebastian@centricular.com>
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
|
|
|
|
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
|
|
|
|
// option. This file may not be copied, modified, or distributed
|
|
|
|
// except according to those terms.
|
|
|
|
|
|
|
|
use std::error::Error;
|
|
|
|
use std::fmt;
|
|
|
|
|
2018-07-30 08:46:40 +00:00
|
|
|
use glib;
|
2019-01-24 19:33:31 +00:00
|
|
|
use glib::IsA;
|
2017-12-20 17:52:57 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! gst_error_msg(
|
|
|
|
// 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!())
|
|
|
|
}};
|
|
|
|
);
|
|
|
|
|
2017-12-20 18:06:39 +00:00
|
|
|
#[derive(Clone, Debug, PartialEq, Eq)]
|
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 {
|
2019-05-23 18:19:24 +00:00
|
|
|
pub fn new<T: ::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 {
|
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
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for ErrorMessage {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Error {:?} ({:?}) at {}:{}",
|
|
|
|
self.message, self.debug, self.filename, self.line
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for ErrorMessage {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
"ErrorMessage"
|
|
|
|
}
|
|
|
|
}
|
2019-01-24 19:33:31 +00:00
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! gst_loggable_error(
|
|
|
|
// Plain strings
|
|
|
|
($cat:expr, $msg:expr) => {
|
2019-02-28 08:54:32 +00:00
|
|
|
$crate::LoggableError::new($cat.clone(), glib_bool_error!($msg))
|
2019-01-24 19:33:31 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
// Format strings
|
|
|
|
($cat:expr, $($msg:tt)*) => { {
|
2019-02-28 08:54:32 +00:00
|
|
|
$crate::LoggableError::new($cat.clone(), glib_bool_error!($($msg)*))
|
2019-01-24 19:33:31 +00:00
|
|
|
}};
|
|
|
|
);
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! gst_result_from_gboolean(
|
|
|
|
// Plain strings
|
2019-03-19 07:58:20 +00:00
|
|
|
($gst_sys_bool:expr, $cat:expr, $msg:expr) => {
|
|
|
|
glib_result_from_gboolean!($gst_sys_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
|
2019-03-19 07:58:20 +00:00
|
|
|
($gst_sys_bool:expr, $cat:expr, $($msg:tt)*) => { {
|
|
|
|
glib_result_from_gboolean!($gst_sys_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
|
|
|
}};
|
|
|
|
);
|
|
|
|
|
|
|
|
#[derive(Debug, Clone)]
|
|
|
|
pub struct LoggableError {
|
|
|
|
category: ::DebugCategory,
|
|
|
|
bool_error: glib::BoolError,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl LoggableError {
|
2019-02-28 08:54:32 +00:00
|
|
|
pub fn new(category: ::DebugCategory, bool_error: glib::BoolError) -> LoggableError {
|
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(
|
|
|
|
None as Option<&::Object>,
|
|
|
|
::DebugLevel::Error,
|
|
|
|
self.bool_error.filename,
|
|
|
|
self.bool_error.function,
|
|
|
|
self.bool_error.line,
|
|
|
|
format_args!("{}", self.bool_error.message),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn log_with_object<O: IsA<::Object>>(&self, obj: &O) {
|
|
|
|
self.category.log(
|
|
|
|
Some(obj),
|
|
|
|
::DebugLevel::Error,
|
|
|
|
self.bool_error.filename,
|
|
|
|
self.bool_error.function,
|
|
|
|
self.bool_error.line,
|
|
|
|
format_args!("{}", self.bool_error.message),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn category(&self) -> ::DebugCategory {
|
|
|
|
self.category
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<glib::BoolError> for LoggableError {
|
|
|
|
fn from(bool_error: glib::BoolError) -> Self {
|
|
|
|
LoggableError {
|
|
|
|
category: *::CAT_RUST,
|
|
|
|
bool_error,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl fmt::Display for LoggableError {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> {
|
|
|
|
write!(
|
|
|
|
f,
|
|
|
|
"Error {:?}: {:?} at {}:{}",
|
|
|
|
self.category.get_name(),
|
|
|
|
self.bool_error.message,
|
|
|
|
self.bool_error.filename,
|
|
|
|
self.bool_error.line
|
|
|
|
)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Error for LoggableError {
|
|
|
|
fn description(&self) -> &str {
|
|
|
|
self.bool_error.message.as_ref()
|
|
|
|
}
|
|
|
|
}
|