2017-09-09 11:12:49 +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.
|
|
|
|
|
2019-07-14 15:15:15 +00:00
|
|
|
use DebugLevel;
|
|
|
|
|
2017-09-09 11:12:49 +00:00
|
|
|
use libc::c_char;
|
2019-10-02 09:01:44 +00:00
|
|
|
use std::borrow::Cow;
|
2017-09-09 11:12:49 +00:00
|
|
|
use std::ffi::CStr;
|
|
|
|
use std::fmt;
|
2018-04-01 08:30:03 +00:00
|
|
|
use std::ptr;
|
2017-09-09 11:12:49 +00:00
|
|
|
|
2020-01-22 17:38:13 +00:00
|
|
|
use once_cell::sync::Lazy;
|
|
|
|
|
2019-03-19 07:58:20 +00:00
|
|
|
use gobject_sys;
|
|
|
|
use gst_sys;
|
2017-09-09 11:12:49 +00:00
|
|
|
|
2020-04-05 14:52:56 +00:00
|
|
|
use glib::translate::*;
|
2018-04-25 08:10:06 +00:00
|
|
|
use glib::IsA;
|
2019-07-14 15:15:15 +00:00
|
|
|
use glib_sys::gpointer;
|
|
|
|
|
|
|
|
#[derive(PartialEq, Eq)]
|
|
|
|
pub struct DebugMessage(ptr::NonNull<gst_sys::GstDebugMessage>);
|
|
|
|
|
|
|
|
impl fmt::Debug for DebugMessage {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
|
|
|
f.debug_tuple("DebugMessage").field(&self.get()).finish()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl DebugMessage {
|
2019-10-02 09:01:44 +00:00
|
|
|
pub fn get(&self) -> Option<Cow<str>> {
|
2019-07-14 15:15:15 +00:00
|
|
|
unsafe {
|
|
|
|
let message = gst_sys::gst_debug_message_get(self.0.as_ptr());
|
|
|
|
|
|
|
|
if message.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
2019-10-02 09:01:44 +00:00
|
|
|
Some(CStr::from_ptr(message).to_string_lossy())
|
2019-07-14 15:15:15 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2017-09-09 11:12:49 +00:00
|
|
|
|
|
|
|
#[derive(PartialEq, Eq, Clone, Copy)]
|
2019-03-19 07:58:20 +00:00
|
|
|
pub struct DebugCategory(ptr::NonNull<gst_sys::GstDebugCategory>);
|
2017-09-09 11:12:49 +00:00
|
|
|
|
|
|
|
impl DebugCategory {
|
2019-05-23 18:19:24 +00:00
|
|
|
pub fn new(name: &str, color: ::DebugColorFlags, description: Option<&str>) -> DebugCategory {
|
2020-03-22 14:18:47 +00:00
|
|
|
assert_initialized_main_thread!();
|
2017-09-09 11:12:49 +00:00
|
|
|
extern "C" {
|
|
|
|
fn _gst_debug_category_new(
|
|
|
|
name: *const c_char,
|
2019-03-19 07:58:20 +00:00
|
|
|
color: gst_sys::GstDebugColorFlags,
|
2017-09-09 11:12:49 +00:00
|
|
|
description: *const c_char,
|
2019-03-19 07:58:20 +00:00
|
|
|
) -> *mut gst_sys::GstDebugCategory;
|
2017-09-09 11:12:49 +00:00
|
|
|
}
|
2017-12-16 15:56:23 +00:00
|
|
|
|
2017-09-09 11:12:49 +00:00
|
|
|
// Gets the category if it exists already
|
|
|
|
unsafe {
|
2018-04-01 08:29:15 +00:00
|
|
|
let ptr = _gst_debug_category_new(
|
2017-09-09 11:12:49 +00:00
|
|
|
name.to_glib_none().0,
|
|
|
|
color.to_glib(),
|
|
|
|
description.to_glib_none().0,
|
2018-04-01 08:29:15 +00:00
|
|
|
);
|
|
|
|
assert!(!ptr.is_null());
|
|
|
|
DebugCategory(ptr::NonNull::new_unchecked(ptr))
|
2017-09-09 11:12:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn get(name: &str) -> Option<DebugCategory> {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2017-09-09 11:12:49 +00:00
|
|
|
unsafe {
|
|
|
|
extern "C" {
|
2019-03-19 07:58:20 +00:00
|
|
|
fn _gst_debug_get_category(name: *const c_char) -> *mut gst_sys::GstDebugCategory;
|
2017-09-09 11:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let cat = _gst_debug_get_category(name.to_glib_none().0);
|
|
|
|
|
|
|
|
if cat.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
2018-04-01 08:29:15 +00:00
|
|
|
Some(DebugCategory(ptr::NonNull::new_unchecked(cat)))
|
2017-09-09 11:12:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 08:54:32 +00:00
|
|
|
pub fn get_threshold(self) -> ::DebugLevel {
|
2019-03-19 07:58:20 +00:00
|
|
|
from_glib(unsafe { gst_sys::gst_debug_category_get_threshold(self.0.as_ptr()) })
|
2017-09-09 11:12:49 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 08:54:32 +00:00
|
|
|
pub fn set_threshold(self, threshold: ::DebugLevel) {
|
2019-03-19 07:58:20 +00:00
|
|
|
unsafe { gst_sys::gst_debug_category_set_threshold(self.0.as_ptr(), threshold.to_glib()) }
|
2017-09-09 11:12:49 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 08:54:32 +00:00
|
|
|
pub fn reset_threshold(self) {
|
2019-03-19 07:58:20 +00:00
|
|
|
unsafe { gst_sys::gst_debug_category_reset_threshold(self.0.as_ptr()) }
|
2017-09-09 11:12:49 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 08:54:32 +00:00
|
|
|
pub fn get_color(self) -> ::DebugColorFlags {
|
2019-03-19 07:58:20 +00:00
|
|
|
unsafe { from_glib(gst_sys::gst_debug_category_get_color(self.0.as_ptr())) }
|
2017-09-09 11:12:49 +00:00
|
|
|
}
|
|
|
|
|
2019-02-28 08:54:32 +00:00
|
|
|
pub fn get_name<'a>(self) -> &'a str {
|
2017-09-09 11:12:49 +00:00
|
|
|
unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
CStr::from_ptr(gst_sys::gst_debug_category_get_name(self.0.as_ptr()))
|
2017-09-09 11:12:49 +00:00
|
|
|
.to_str()
|
|
|
|
.unwrap()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-02-28 08:54:32 +00:00
|
|
|
pub fn get_description<'a>(self) -> Option<&'a str> {
|
2017-12-16 15:56:23 +00:00
|
|
|
unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
let ptr = gst_sys::gst_debug_category_get_description(self.0.as_ptr());
|
2017-12-16 15:56:23 +00:00
|
|
|
|
|
|
|
if ptr.is_null() {
|
|
|
|
None
|
|
|
|
} else {
|
|
|
|
Some(CStr::from_ptr(ptr).to_str().unwrap())
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-16 10:27:36 +00:00
|
|
|
#[inline]
|
2020-02-22 16:35:02 +00:00
|
|
|
pub fn log<O: IsA<glib::Object>>(
|
2019-02-28 08:54:32 +00:00
|
|
|
self,
|
2017-09-09 11:12:49 +00:00
|
|
|
obj: Option<&O>,
|
|
|
|
level: ::DebugLevel,
|
|
|
|
file: &str,
|
|
|
|
module: &str,
|
|
|
|
line: u32,
|
|
|
|
args: fmt::Arguments,
|
|
|
|
) {
|
2018-03-16 10:27:36 +00:00
|
|
|
unsafe {
|
2018-04-01 08:29:15 +00:00
|
|
|
if level.to_glib() as i32 > self.0.as_ref().threshold {
|
2018-03-16 10:27:36 +00:00
|
|
|
return;
|
|
|
|
}
|
2017-09-09 11:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
let obj_ptr = match obj {
|
2019-03-19 07:58:20 +00:00
|
|
|
Some(obj) => obj.to_glib_none().0 as *mut gobject_sys::GObject,
|
2017-09-09 11:12:49 +00:00
|
|
|
None => ptr::null_mut(),
|
|
|
|
};
|
|
|
|
|
|
|
|
unsafe {
|
2019-03-19 07:58:20 +00:00
|
|
|
gst_sys::gst_debug_log(
|
2018-04-01 08:29:15 +00:00
|
|
|
self.0.as_ptr(),
|
2017-09-09 11:12:49 +00:00
|
|
|
level.to_glib(),
|
|
|
|
file.to_glib_none().0,
|
|
|
|
module.to_glib_none().0,
|
|
|
|
line as i32,
|
|
|
|
obj_ptr,
|
2020-03-18 21:54:46 +00:00
|
|
|
fmt::format(args).replace("%", "%%").to_glib_none().0,
|
2017-09-09 11:12:49 +00:00
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe impl Sync for DebugCategory {}
|
|
|
|
unsafe impl Send for DebugCategory {}
|
|
|
|
|
|
|
|
impl fmt::Debug for DebugCategory {
|
|
|
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
2017-12-01 09:21:20 +00:00
|
|
|
f.debug_tuple("DebugCategory")
|
|
|
|
.field(&self.get_name())
|
|
|
|
.finish()
|
2017-09-09 11:12:49 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-01-22 17:38:13 +00:00
|
|
|
pub static CAT_RUST: Lazy<DebugCategory> = Lazy::new(|| {
|
|
|
|
DebugCategory::new(
|
2019-01-15 22:20:44 +00:00
|
|
|
"GST_RUST",
|
|
|
|
::DebugColorFlags::UNDERLINE,
|
2019-05-23 18:19:24 +00:00
|
|
|
Some("GStreamer's Rust binding core"),
|
2020-01-22 17:38:13 +00:00
|
|
|
)
|
|
|
|
});
|
2019-01-15 22:20:44 +00:00
|
|
|
|
|
|
|
macro_rules! declare_debug_category_from_name(
|
|
|
|
($cat:ident, $cat_name:expr) => (
|
2020-01-22 17:38:13 +00:00
|
|
|
pub static $cat: Lazy<DebugCategory> = Lazy::new(|| DebugCategory::get($cat_name)
|
|
|
|
.expect(&format!("Unable to find `DebugCategory` with name {}", $cat_name)));
|
2019-01-15 22:20:44 +00:00
|
|
|
);
|
|
|
|
);
|
|
|
|
|
|
|
|
declare_debug_category_from_name!(CAT_DEFAULT, "default");
|
|
|
|
declare_debug_category_from_name!(CAT_GST_INIT, "GST_INIT");
|
|
|
|
declare_debug_category_from_name!(CAT_MEMORY, "GST_MEMORY");
|
|
|
|
declare_debug_category_from_name!(CAT_PARENTAGE, "GST_PARENTAGE");
|
|
|
|
declare_debug_category_from_name!(CAT_STATES, "GST_STATES");
|
|
|
|
declare_debug_category_from_name!(CAT_SCHEDULING, "GST_SCHEDULING");
|
|
|
|
declare_debug_category_from_name!(CAT_BUFFER, "GST_BUFFER");
|
|
|
|
declare_debug_category_from_name!(CAT_BUFFER_LIST, "GST_BUFFER_LIST");
|
|
|
|
declare_debug_category_from_name!(CAT_BUS, "GST_BUS");
|
|
|
|
declare_debug_category_from_name!(CAT_CAPS, "GST_CAPS");
|
|
|
|
declare_debug_category_from_name!(CAT_CLOCK, "GST_CLOCK");
|
|
|
|
declare_debug_category_from_name!(CAT_ELEMENT_PADS, "GST_ELEMENT_PADS");
|
|
|
|
declare_debug_category_from_name!(CAT_PADS, "GST_PADS");
|
|
|
|
declare_debug_category_from_name!(CAT_PERFORMANCE, "GST_PERFORMANCE");
|
|
|
|
declare_debug_category_from_name!(CAT_PIPELINE, "GST_PIPELINE");
|
|
|
|
declare_debug_category_from_name!(CAT_PLUGIN_LOADING, "GST_PLUGIN_LOADING");
|
|
|
|
declare_debug_category_from_name!(CAT_PLUGIN_INFO, "GST_PLUGIN_INFO");
|
|
|
|
declare_debug_category_from_name!(CAT_PROPERTIES, "GST_PROPERTIES");
|
|
|
|
declare_debug_category_from_name!(CAT_NEGOTIATION, "GST_NEGOTIATION");
|
|
|
|
declare_debug_category_from_name!(CAT_REFCOUNTING, "GST_REFCOUNTING");
|
|
|
|
declare_debug_category_from_name!(CAT_ERROR_SYSTEM, "GST_ERROR_SYSTEM");
|
|
|
|
declare_debug_category_from_name!(CAT_EVENT, "GST_EVENT");
|
|
|
|
declare_debug_category_from_name!(CAT_MESSAGE, "GST_MESSAGE");
|
|
|
|
declare_debug_category_from_name!(CAT_PARAMS, "GST_PARAMS");
|
|
|
|
declare_debug_category_from_name!(CAT_CALL_TRACE, "GST_CALL_TRACE");
|
|
|
|
declare_debug_category_from_name!(CAT_SIGNAL, "GST_SIGNAL");
|
|
|
|
declare_debug_category_from_name!(CAT_PROBE, "GST_PROBE");
|
|
|
|
declare_debug_category_from_name!(CAT_REGISTRY, "GST_REGISTRY");
|
|
|
|
declare_debug_category_from_name!(CAT_QOS, "GST_QOS");
|
|
|
|
declare_debug_category_from_name!(CAT_META, "GST_META");
|
|
|
|
declare_debug_category_from_name!(CAT_LOCKING, "GST_LOCKING");
|
|
|
|
declare_debug_category_from_name!(CAT_CONTEXT, "GST_CONTEXT");
|
|
|
|
|
2017-09-09 11:12:49 +00:00
|
|
|
#[macro_export]
|
|
|
|
macro_rules! gst_error(
|
|
|
|
($cat:expr, obj: $obj:expr, $($args:tt)*) => { {
|
2019-08-30 16:14:05 +00:00
|
|
|
$crate::gst_log_with_level!($cat.clone(), level: $crate::DebugLevel::Error, obj: $obj, $($args)*)
|
2017-09-09 11:12:49 +00:00
|
|
|
}};
|
|
|
|
($cat:expr, $($args:tt)*) => { {
|
2019-08-30 16:14:05 +00:00
|
|
|
$crate::gst_log_with_level!($cat.clone(), level: $crate::DebugLevel::Error, $($args)*)
|
2017-09-09 11:12:49 +00:00
|
|
|
}};
|
|
|
|
);
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! gst_warning(
|
|
|
|
($cat:expr, obj: $obj:expr, $($args:tt)*) => { {
|
2019-08-30 16:14:05 +00:00
|
|
|
$crate::gst_log_with_level!($cat.clone(), level: $crate::DebugLevel::Warning, obj: $obj, $($args)*)
|
2017-09-09 11:12:49 +00:00
|
|
|
}};
|
|
|
|
($cat:expr, $($args:tt)*) => { {
|
2019-08-30 16:14:05 +00:00
|
|
|
$crate::gst_log_with_level!($cat.clone(), level: $crate::DebugLevel::Warning, $($args)*)
|
2017-09-09 11:12:49 +00:00
|
|
|
}};
|
|
|
|
);
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! gst_fixme(
|
|
|
|
($cat:expr, obj: $obj:expr, $($args:tt)*) => { {
|
2019-08-30 16:14:05 +00:00
|
|
|
$crate::gst_log_with_level!($cat.clone(), level: $crate::DebugLevel::Fixme, obj: $obj, $($args)*)
|
2017-09-09 11:12:49 +00:00
|
|
|
}};
|
|
|
|
($cat:expr, $($args:tt)*) => { {
|
2019-08-30 16:14:05 +00:00
|
|
|
$crate::gst_log_with_level!($cat.clone(), level: $crate::DebugLevel::Fixme, $($args)*)
|
2017-09-09 11:12:49 +00:00
|
|
|
}};
|
|
|
|
);
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! gst_info(
|
|
|
|
($cat:expr, obj: $obj:expr, $($args:tt)*) => { {
|
2019-08-30 16:14:05 +00:00
|
|
|
$crate::gst_log_with_level!($cat.clone(), level: $crate::DebugLevel::Info, obj: $obj, $($args)*)
|
2017-09-09 11:12:49 +00:00
|
|
|
}};
|
|
|
|
($cat:expr, $($args:tt)*) => { {
|
2019-08-30 16:14:05 +00:00
|
|
|
$crate::gst_log_with_level!($cat.clone(), level: $crate::DebugLevel::Info, $($args)*)
|
2017-09-09 11:12:49 +00:00
|
|
|
}};
|
|
|
|
);
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! gst_debug(
|
|
|
|
($cat:expr, obj: $obj:expr, $($args:tt)*) => { {
|
2019-08-30 16:14:05 +00:00
|
|
|
$crate::gst_log_with_level!($cat.clone(), level: $crate::DebugLevel::Debug, obj: $obj, $($args)*)
|
2017-09-09 11:12:49 +00:00
|
|
|
}};
|
|
|
|
($cat:expr, $($args:tt)*) => { {
|
2019-08-30 16:14:05 +00:00
|
|
|
$crate::gst_log_with_level!($cat.clone(), level: $crate::DebugLevel::Debug, $($args)*)
|
2017-09-09 11:12:49 +00:00
|
|
|
}};
|
|
|
|
);
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! gst_log(
|
|
|
|
($cat:expr, obj: $obj:expr, $($args:tt)*) => { {
|
2019-08-30 16:14:05 +00:00
|
|
|
$crate::gst_log_with_level!($cat.clone(), level: $crate::DebugLevel::Log, obj: $obj, $($args)*)
|
2017-09-09 11:12:49 +00:00
|
|
|
}};
|
|
|
|
($cat:expr, $($args:tt)*) => { {
|
2019-08-30 16:14:05 +00:00
|
|
|
$crate::gst_log_with_level!($cat.clone(), level: $crate::DebugLevel::Log, $($args)*)
|
2017-09-09 11:12:49 +00:00
|
|
|
}};
|
|
|
|
);
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! gst_trace(
|
|
|
|
($cat:expr, obj: $obj:expr, $($args:tt)*) => { {
|
2019-08-30 16:14:05 +00:00
|
|
|
$crate::gst_log_with_level!($cat.clone(), level: $crate::DebugLevel::Trace, obj: $obj, $($args)*)
|
2017-09-09 11:12:49 +00:00
|
|
|
}};
|
|
|
|
($cat:expr, $($args:tt)*) => { {
|
2019-08-30 16:14:05 +00:00
|
|
|
$crate::gst_log_with_level!($cat.clone(), level: $crate::DebugLevel::Trace, $($args)*)
|
2017-09-09 11:12:49 +00:00
|
|
|
}};
|
|
|
|
);
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! gst_memdump(
|
|
|
|
($cat:expr, obj: $obj:expr, $($args:tt)*) => { {
|
2019-08-30 16:14:05 +00:00
|
|
|
$crate::gst_log_with_level!($cat.clone(), level: $crate::DebugLevel::Memdump, obj: $obj, $($args)*)
|
2017-09-09 11:12:49 +00:00
|
|
|
}};
|
|
|
|
($cat:expr, $($args:tt)*) => { {
|
2019-08-30 16:14:05 +00:00
|
|
|
$crate::gst_log_with_level!($cat.clone(), level: $crate::DebugLevel::Memdump, $($args)*)
|
2017-09-09 11:12:49 +00:00
|
|
|
}};
|
|
|
|
);
|
|
|
|
|
|
|
|
#[macro_export]
|
|
|
|
macro_rules! gst_log_with_level(
|
|
|
|
($cat:expr, level: $level:expr, obj: $obj:expr, $($args:tt)*) => { {
|
2019-02-28 08:54:32 +00:00
|
|
|
$crate::DebugCategory::log($cat.clone(), Some($obj), $level, file!(),
|
2017-09-09 11:12:49 +00:00
|
|
|
module_path!(), line!(), format_args!($($args)*))
|
|
|
|
}};
|
|
|
|
($cat:expr, level: $level:expr, $($args:tt)*) => { {
|
2020-02-22 16:35:02 +00:00
|
|
|
$crate::DebugCategory::log($cat.clone(), None as Option<&$crate::glib::Object>, $level, file!(),
|
2017-09-09 11:12:49 +00:00
|
|
|
module_path!(), line!(), format_args!($($args)*))
|
|
|
|
}};
|
|
|
|
);
|
|
|
|
|
2019-07-14 15:15:15 +00:00
|
|
|
unsafe extern "C" fn log_handler<T>(
|
|
|
|
category: *mut gst_sys::GstDebugCategory,
|
|
|
|
level: gst_sys::GstDebugLevel,
|
|
|
|
file: *const c_char,
|
|
|
|
function: *const c_char,
|
|
|
|
line: i32,
|
|
|
|
object: *mut gobject_sys::GObject,
|
|
|
|
message: *mut gst_sys::GstDebugMessage,
|
|
|
|
user_data: gpointer,
|
|
|
|
) where
|
|
|
|
T: Fn(DebugCategory, DebugLevel, &str, &str, u32, Option<&glib::Object>, &DebugMessage)
|
|
|
|
+ Send
|
|
|
|
+ Sync
|
|
|
|
+ 'static,
|
|
|
|
{
|
|
|
|
let category = DebugCategory(ptr::NonNull::new_unchecked(category));
|
|
|
|
let level = from_glib(level);
|
2019-10-02 09:01:44 +00:00
|
|
|
let file = CStr::from_ptr(file).to_string_lossy();
|
|
|
|
let function = CStr::from_ptr(function).to_string_lossy();
|
2019-07-14 15:15:15 +00:00
|
|
|
let line = line as u32;
|
2020-04-05 14:52:56 +00:00
|
|
|
let object: Borrowed<Option<glib::Object>> = from_glib_borrow(object);
|
2019-07-14 15:15:15 +00:00
|
|
|
let message = DebugMessage(ptr::NonNull::new_unchecked(message));
|
|
|
|
let handler = &*(user_data as *mut T);
|
|
|
|
(handler)(
|
|
|
|
category,
|
|
|
|
level,
|
2019-10-02 09:01:44 +00:00
|
|
|
&file,
|
|
|
|
&function,
|
2019-07-14 15:15:15 +00:00
|
|
|
line,
|
2020-04-05 14:52:56 +00:00
|
|
|
object.as_ref().as_ref(),
|
2019-07-14 15:15:15 +00:00
|
|
|
&message,
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
unsafe extern "C" fn log_handler_data_free<T>(data: gpointer) {
|
|
|
|
let data = Box::from_raw(data as *mut T);
|
|
|
|
drop(data);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Debug)]
|
|
|
|
pub struct DebugLogFunction(ptr::NonNull<std::os::raw::c_void>);
|
|
|
|
|
|
|
|
// The contained pointer is never dereferenced and has no thread affinity.
|
|
|
|
// It may be convenient to send it or share it between threads to allow cleaning
|
|
|
|
// up log functions from other threads than the one that created it.
|
|
|
|
unsafe impl Send for DebugLogFunction {}
|
|
|
|
unsafe impl Sync for DebugLogFunction {}
|
|
|
|
|
|
|
|
pub fn debug_add_log_function<T>(function: T) -> DebugLogFunction
|
|
|
|
where
|
|
|
|
T: Fn(DebugCategory, DebugLevel, &str, &str, u32, Option<&glib::Object>, &DebugMessage)
|
|
|
|
+ Send
|
|
|
|
+ Sync
|
|
|
|
+ 'static,
|
|
|
|
{
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2019-07-14 15:15:15 +00:00
|
|
|
unsafe {
|
|
|
|
let user_data = Box::new(function);
|
|
|
|
let user_data_ptr = Box::into_raw(user_data) as gpointer;
|
|
|
|
gst_sys::gst_debug_add_log_function(
|
|
|
|
Some(log_handler::<T>),
|
|
|
|
user_data_ptr,
|
|
|
|
Some(log_handler_data_free::<T>),
|
|
|
|
);
|
|
|
|
DebugLogFunction(ptr::NonNull::new_unchecked(user_data_ptr))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn debug_remove_default_log_function() {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2019-07-14 15:15:15 +00:00
|
|
|
unsafe {
|
|
|
|
gst_sys::gst_debug_remove_log_function(Some(gst_sys::gst_debug_log_default));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
pub fn debug_remove_log_function(log_fn: DebugLogFunction) {
|
2020-03-22 14:18:47 +00:00
|
|
|
skip_assert_initialized!();
|
2019-07-14 15:15:15 +00:00
|
|
|
unsafe {
|
|
|
|
let removed = gst_sys::gst_debug_remove_log_function_by_data(log_fn.0.as_ptr());
|
|
|
|
assert_eq!(removed, 1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-09-09 11:12:49 +00:00
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2019-07-14 15:15:15 +00:00
|
|
|
use std::sync::mpsc;
|
|
|
|
use std::sync::{Arc, Mutex};
|
2017-09-09 11:12:49 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn get_existing() {
|
|
|
|
::init().unwrap();
|
|
|
|
|
2019-01-15 22:20:44 +00:00
|
|
|
let perf_cat = DebugCategory::get("GST_PERFORMANCE")
|
|
|
|
.expect("Unable to find `DebugCategory` with name \"GST_PERFORMANCE\"");
|
|
|
|
assert_eq!(perf_cat.get_name(), CAT_PERFORMANCE.get_name());
|
2017-09-09 11:12:49 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn new_and_log() {
|
|
|
|
::init().unwrap();
|
|
|
|
|
|
|
|
let cat = DebugCategory::new(
|
|
|
|
"test-cat",
|
|
|
|
::DebugColorFlags::empty(),
|
2019-05-23 18:19:24 +00:00
|
|
|
Some("some debug category"),
|
2017-09-09 11:12:49 +00:00
|
|
|
);
|
|
|
|
|
|
|
|
gst_error!(cat, "meh");
|
|
|
|
gst_warning!(cat, "meh");
|
|
|
|
gst_fixme!(cat, "meh");
|
|
|
|
gst_info!(cat, "meh");
|
|
|
|
gst_debug!(cat, "meh");
|
|
|
|
gst_log!(cat, "meh");
|
|
|
|
gst_trace!(cat, "meh");
|
|
|
|
gst_memdump!(cat, "meh");
|
|
|
|
|
2019-03-19 07:58:20 +00:00
|
|
|
let obj = ::Bin::new(Some("meh"));
|
2017-09-11 11:18:32 +00:00
|
|
|
gst_error!(cat, obj: &obj, "meh");
|
|
|
|
gst_warning!(cat, obj: &obj, "meh");
|
|
|
|
gst_fixme!(cat, obj: &obj, "meh");
|
|
|
|
gst_info!(cat, obj: &obj, "meh");
|
|
|
|
gst_debug!(cat, obj: &obj, "meh");
|
|
|
|
gst_log!(cat, obj: &obj, "meh");
|
|
|
|
gst_trace!(cat, obj: &obj, "meh");
|
|
|
|
gst_memdump!(cat, obj: &obj, "meh");
|
2017-09-09 11:12:49 +00:00
|
|
|
}
|
2019-07-14 15:15:15 +00:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn log_handler() {
|
|
|
|
::init().unwrap();
|
|
|
|
|
|
|
|
let cat = DebugCategory::new(
|
|
|
|
"test-cat-log",
|
|
|
|
::DebugColorFlags::empty(),
|
|
|
|
Some("some debug category"),
|
|
|
|
);
|
|
|
|
cat.set_threshold(DebugLevel::Info);
|
|
|
|
let obj = ::Bin::new(Some("meh"));
|
|
|
|
|
|
|
|
let (sender, receiver) = mpsc::channel();
|
|
|
|
|
|
|
|
let sender = Arc::new(Mutex::new(sender));
|
|
|
|
|
|
|
|
let handler = move |category: DebugCategory,
|
|
|
|
level: DebugLevel,
|
|
|
|
_file: &str,
|
|
|
|
_function: &str,
|
|
|
|
_line: u32,
|
|
|
|
_object: Option<&glib::Object>,
|
|
|
|
message: &DebugMessage| {
|
|
|
|
let cat = DebugCategory::get("test-cat-log").unwrap();
|
2019-09-04 12:02:55 +00:00
|
|
|
|
|
|
|
if category != cat {
|
|
|
|
// This test can run in parallel with other tests, including new_and_log above.
|
|
|
|
// We cannot be certain we only see our own messages.
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2019-07-14 15:15:15 +00:00
|
|
|
assert_eq!(level, DebugLevel::Info);
|
2019-10-02 09:01:44 +00:00
|
|
|
assert_eq!(&message.get().unwrap(), "meh");
|
2019-07-14 15:15:15 +00:00
|
|
|
let _ = sender.lock().unwrap().send(());
|
|
|
|
};
|
|
|
|
|
|
|
|
debug_remove_default_log_function();
|
|
|
|
let log_fn = debug_add_log_function(handler);
|
|
|
|
gst_info!(cat, obj: &obj, "meh");
|
|
|
|
|
2019-12-22 07:59:23 +00:00
|
|
|
receiver.recv().unwrap();
|
2019-07-14 15:15:15 +00:00
|
|
|
|
|
|
|
debug_remove_log_function(log_fn);
|
|
|
|
|
|
|
|
gst_info!(cat, obj: &obj, "meh2");
|
|
|
|
assert!(receiver.recv().is_err());
|
|
|
|
}
|
2017-09-09 11:12:49 +00:00
|
|
|
}
|