Skip to content

Commit

Permalink
src: logger: Fix gstreamer log integration
Browse files Browse the repository at this point in the history
  • Loading branch information
joaoantoniocardoso authored and patrickelectric committed Jun 1, 2024
1 parent 1d5ab66 commit b3dcd1e
Showing 1 changed file with 45 additions and 3 deletions.
48 changes: 45 additions & 3 deletions src/logger/manager.rs
Original file line number Diff line number Diff line change
Expand Up @@ -71,14 +71,14 @@ pub fn init() {
}
};

// Redirects all gstreamer logs to tracing.
// Configure GSTreamer logs integration
gst::log::remove_default_log_function();
// This fundamentally changes the CPU usage of our streams, so we are only enabling
// this integration if absolutely necessary.
if cli::manager::is_tracy() {
tracing_gstreamer::integrate_events(); // This must be called before any gst::init()
gst::init().unwrap();
tracing_gstreamer::integrate_spans(); // This must be called after gst::init(), this is necessary to have GStreamer on tracy
} else {
redirect_gstreamer_logs_to_tracing();
}

info!(
Expand All @@ -98,3 +98,45 @@ pub fn init() {
cli::manager::command_line()
);
}

fn redirect_gstreamer_logs_to_tracing() {
gst::log::add_log_function(
|category, gst_level, file, function, line, object, message| {
use gst::DebugLevel as GstLevel;
use tracing::Level as TracingLevel;

let tracing_level: TracingLevel = match gst_level {
GstLevel::Log | GstLevel::Info | GstLevel::None => TracingLevel::INFO,
GstLevel::Debug => TracingLevel::DEBUG,
GstLevel::Warning | GstLevel::Fixme => TracingLevel::WARN,
GstLevel::Error => TracingLevel::ERROR,
_ => TracingLevel::TRACE,
};

let object_name = object.map(|o| format!(":<{}>", o)).unwrap_or_default();
let category_name = category.name();
let message = message.get().map(|o| o.to_string()).unwrap_or_default();

// Quick workaround to use dynamic level, because the macro requires it to be static
#[macro_export]
macro_rules! dyn_event {
(target: $target:expr, parent: $parent:expr, $lvl:ident, $($fields:tt)*) => {
match $lvl {
TracingLevel::INFO => ::tracing::info!(target: $target, parent: $parent, $($fields)*),
TracingLevel::DEBUG => ::tracing::debug!(target: $target, parent: $parent, $($fields)*),
TracingLevel::WARN => ::tracing::warn!(target: $target, parent: $parent, $($fields)*),
TracingLevel::ERROR => ::tracing::error!(target: $target, parent: $parent, $($fields)*),
TracingLevel::TRACE => ::tracing::trace!(target: $target, parent: $parent, $($fields)*),
}
};
}

dyn_event!(
target: "gstreamer",
parent: None,
tracing_level,
"{category_name} {file}:{line}:{function}{object_name} {message}",
);
},
);
}

0 comments on commit b3dcd1e

Please sign in to comment.