about summary refs log tree commit diff stats
path: root/crates
diff options
context:
space:
mode:
authorBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-02-16 09:44:28 +0100
committerBenedikt Peetz <benedikt.peetz@b-peetz.de>2025-02-16 09:44:28 +0100
commitd1022c5c5c82557d8c2c45fad88b67cc3e6582e3 (patch)
treededd2b1a07433695070b5ac1f44f50990cdd8a3a /crates
parentfix(crates/yt_dlp/progress_hook): Print the progress to stderr (diff)
downloadyt-d1022c5c5c82557d8c2c45fad88b67cc3e6582e3.zip
fix(crates/yt_dlp/lib): Swallow all error logs from yt_dlp
These are already returned as `PythonError`s and thus often printed
twice. As such, removing the python print gives the consumer more
liberty of how to handle the error.
Diffstat (limited to 'crates')
-rw-r--r--crates/yt_dlp/src/lib.rs34
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")?;