diff options
Diffstat (limited to 'crates/yt_dlp/src/logging.rs')
-rw-r--r-- | crates/yt_dlp/src/logging.rs | 16 |
1 files changed, 11 insertions, 5 deletions
diff --git a/crates/yt_dlp/src/logging.rs b/crates/yt_dlp/src/logging.rs index 385255d..670fc1c 100644 --- a/crates/yt_dlp/src/logging.rs +++ b/crates/yt_dlp/src/logging.rs @@ -15,6 +15,8 @@ // The pyo3 `pyfunction` proc-macros call unsafe functions internally, which trigger this lint. #![allow(unsafe_op_in_unsafe_fn)] +use std::ffi::CString; + use log::{logger, Level, MetadataBuilder, Record}; use pyo3::{ prelude::{PyAnyMethods, PyListMethods, PyModuleMethods}, @@ -91,14 +93,17 @@ fn host_log(record: Bound<'_, PyAny>, rust_target: &str) -> PyResult<()> { /// Registers the `host_log` function in rust as the event handler for Python's logging logger /// This function needs to be called from within a pyo3 context as early as possible to ensure logging messages /// arrive to the rust consumer. +/// +/// # Panics +/// Only if internal assertions fail. #[allow(clippy::module_name_repetitions)] pub fn setup_logging(py: Python<'_>, target: &str) -> PyResult<()> { - let logging = py.import_bound("logging")?; + let logging = py.import("logging")?; logging.setattr("host_log", wrap_pyfunction!(host_log, &logging)?)?; - py.run_bound( - format!( + py.run( + CString::new(format!( r#" class HostHandler(Handler): def __init__(self, level=0): @@ -113,8 +118,9 @@ def basicConfig(*pargs, **kwargs): kwargs["handlers"] = [HostHandler()] return oldBasicConfig(*pargs, **kwargs) "# - ) - .as_str(), + )) + .expect("This is hardcoded") + .as_c_str(), Some(&logging.dict()), None, )?; |