diff options
Diffstat (limited to 'crates')
-rw-r--r-- | crates/yt_dlp/src/lib.rs | 34 |
1 files changed, 33 insertions, 1 deletions
diff --git a/crates/yt_dlp/src/lib.rs b/crates/yt_dlp/src/lib.rs index 15d886c..b0e157c 100644 --- a/crates/yt_dlp/src/lib.rs +++ b/crates/yt_dlp/src/lib.rs @@ -22,7 +22,7 @@ use crate::{duration::Duration, logging::setup_logging, wrapper::info_json::Info use bytes::Bytes; use error::YtDlpError; -use log::{info, log_enabled, Level}; +use log::{debug, info, log_enabled, Level}; use pyo3::types::{PyString, PyTuple, PyTupleMethods}; use pyo3::{ pyfunction, @@ -53,6 +53,33 @@ pub fn add_logger_and_sig_handler<'a>( opts: Bound<'a, PyDict>, py: Python<'_>, ) -> PyResult<Bound<'a, PyDict>> { + /// Is the specified record to be logged? Returns false for no, + /// true for yes. Filters can either modify log records in-place or + /// return a completely different record instance which will replace + /// the original log record in any future processing of the event. + #[pyfunction] + fn filter_error_log(_py: Python<'_>, record: &Bound<'_, PyAny>) -> bool { + // Filter out all error logs (they are propagated as rust errors) + let levelname: String = record + .getattr("levelname") + .expect("This should exist") + .extract() + .expect("This should be a String"); + + let return_value = levelname.as_str() != "ERROR"; + + if log_enabled!(Level::Debug) && !return_value { + let message: String = record + .call_method0("getMessage") + .expect("This method exists") + .extract() + .expect("The message is a string"); + + debug!("Swollowed error message: '{message}'"); + } + return_value + } + setup_logging(py, "yt_dlp")?; let logging = PyModule::import(py, "logging")?; @@ -83,6 +110,11 @@ signal.signal(signal.SIGINT, signal.SIG_DFL)", .expect("This method exists"); }); + ytdl_logger.call_method1( + "addFilter", + (wrap_pyfunction!(filter_error_log, py).expect("This function can be wrapped"),), + )?; + // This was taken from `ytcc`, I don't think it is still applicable // ytdl_logger.setattr("propagate", false)?; // let logging_null_handler = logging.call_method0("NullHandler")?; |