From 665cd94aec10bf16fad1f0c891cb2f911214ec4d Mon Sep 17 00:00:00 2001 From: euzu Date: Mon, 9 Dec 2024 19:26:39 +0100 Subject: [PATCH] refactored debug log --- src/api/api_utils.rs | 19 ++++------- src/api/xtream_api.rs | 15 +++------ src/processing/affix_processor.rs | 8 ++--- src/processing/playlist_processor.rs | 49 +++++++++++----------------- src/repository/epg_repository.rs | 15 +++------ src/utils/mod.rs | 17 +++++++++- src/utils/request_utils.rs | 21 ++++-------- 7 files changed, 61 insertions(+), 83 deletions(-) diff --git a/src/api/api_utils.rs b/src/api/api_utils.rs index 98ea0b8ec..825dd27d6 100644 --- a/src/api/api_utils.rs +++ b/src/api/api_utils.rs @@ -12,7 +12,7 @@ use async_std::prelude::Stream; use async_std::stream::StreamExt; use bytes::Bytes; use chrono::Utc; -use log::{debug, error, log_enabled, Level}; +use log::{debug, error, Level}; use std::collections::HashMap; use std::path::Path; use std::sync::Arc; @@ -20,6 +20,7 @@ use std::time::Duration; use tokio::sync::broadcast; use tokio_stream::wrappers::BroadcastStream; use url::Url; +use crate::debug_if_enabled; use crate::model::playlist::PlaylistItemType; pub async fn serve_file(file_path: &Path, req: &HttpRequest, mime_type: mime::Mime) -> HttpResponse { @@ -121,9 +122,7 @@ async fn create_shared_stream( } actix_web::rt::time::sleep(Duration::from_millis(20)).await; } else { - if log_enabled!(Level::Debug) { - debug!("No active subscribers. Closing stream {}", mask_sensitive_info(&streaming_url)); - } + debug_if_enabled!("No active subscribers. Closing stream {}", mask_sensitive_info(&streaming_url)); // Cleanup for removing unused shared streams let mut shared_streams = shared_streams_map.lock().await; shared_streams.remove(&streaming_url); @@ -136,9 +135,7 @@ async fn create_shared_stream( pub async fn stream_response(app_state: &AppState, stream_url: &str, req: &HttpRequest, input: Option<&ConfigInput>, share_stream: bool) -> HttpResponse { let req_headers: HashMap<&str, &[u8]> = req.headers().iter().map(|(k, v)| (k.as_str(), v.as_bytes())).collect(); - if log_enabled!(Level::Debug) { - debug!("Try to open stream {}", mask_sensitive_info(stream_url)); - } + debug_if_enabled!("Try to open stream {}", mask_sensitive_info(stream_url)); if share_stream { if let Some(value) = shared_stream_response(app_state, stream_url).await { return value; @@ -171,9 +168,7 @@ pub async fn stream_response(app_state: &AppState, stream_url: &str, req: &HttpR return response_builder.body(actix_web::body::BodyStream::new(response.bytes_stream())); } } - if log_enabled!(Level::Debug) { - debug!("Failed to open stream got status {} for {}", status, mask_sensitive_info(stream_url)); - } + debug_if_enabled!("Failed to open stream got status {} for {}", status, mask_sensitive_info(stream_url)); } Err(err) => { error!("Received failure from server {}: {}", mask_sensitive_info(stream_url), err); @@ -187,9 +182,7 @@ pub async fn stream_response(app_state: &AppState, stream_url: &str, req: &HttpR async fn shared_stream_response(app_state: &AppState, stream_url: &str) -> Option { if let Some(stream) = create_notify_stream(app_state, stream_url).await { - if log_enabled!(Level::Debug) { - debug!("Using shared channel {}", mask_sensitive_info(stream_url)); - } + debug_if_enabled!("Using shared channel {}", mask_sensitive_info(stream_url)); // return HttpResponse::Ok().body(actix_web::body::BodyStream::new(stream)); if let Some(shared_stream) = app_state.shared_streams.lock().await.get(stream_url) { let mut response_builder = HttpResponse::Ok(); diff --git a/src/api/xtream_api.rs b/src/api/xtream_api.rs index eff4253b9..12cf6c7b8 100644 --- a/src/api/xtream_api.rs +++ b/src/api/xtream_api.rs @@ -10,13 +10,14 @@ use actix_web::{web, HttpRequest, HttpResponse}; use bytes::Bytes; use futures::stream::{self, StreamExt}; use futures::Stream; -use log::{debug, error, log_enabled, warn, Level}; +use log::{debug, error, warn, Level}; use serde_json::{Map, Value}; use crate::api::api_utils::{get_user_server_info, get_user_target, get_user_target_by_credentials, is_stream_share_enabled, serve_file, stream_response}; use crate::api::model::app_state::AppState; use crate::api::model::request::UserApiRequest; use crate::api::model::xtream::XtreamAuthorizationResponse; +use crate::debug_if_enabled; use crate::m3u_filter_error::{M3uFilterError, M3uFilterErrorKind}; use crate::model::api_proxy::{ProxyType, ProxyUserCredentials}; use crate::model::config::TargetType; @@ -212,9 +213,7 @@ async fn xtream_player_api_stream( if pli.item_type == PlaylistItemType::LiveHls { let stream_url = pli.url.to_string(); - if log_enabled!(Level::Debug) { - debug!("Redirecting stream request to {}", mask_sensitive_info(&stream_url)); - } + debug_if_enabled!("Redirecting stream request to {}", mask_sensitive_info(&stream_url)); return HttpResponse::Found().insert_header(("Location", stream_url)).finish(); } @@ -225,16 +224,12 @@ async fn xtream_player_api_stream( }; if user.proxy == ProxyType::Redirect { - if log_enabled!(Level::Debug) { - debug!("Redirecting stream request to {}", mask_sensitive_info(&pli.url)); - } + debug_if_enabled!("Redirecting stream request to {}", mask_sensitive_info(&pli.url)); return HttpResponse::Found().insert_header(("Location", mask_sensitive_info(pli.url.as_str()))).finish(); } let stream_url = try_option_bad_request!(get_xtream_player_api_stream_url(input, stream_req.context.to_string().as_str(), &query_path, pli.url.as_str()), true, format!("Cant find stream url for target {target_name}, context {}, stream_id {virtual_id}", stream_req.context)); - if log_enabled!(Level::Debug) { - debug!("Streaming stream request from {}", mask_sensitive_info(&stream_url)); - } + debug_if_enabled!("Streaming stream request from {}", mask_sensitive_info(&stream_url)); let share_live_streams = is_stream_share_enabled(pli.item_type, target); stream_response(app_state, &stream_url, req, Some(input), share_live_streams).await } diff --git a/src/processing/affix_processor.rs b/src/processing/affix_processor.rs index c1ab4c84c..7ce3e435a 100644 --- a/src/processing/affix_processor.rs +++ b/src/processing/affix_processor.rs @@ -1,7 +1,7 @@ use crate::model::config::{ConfigInput, InputAffix, AFFIX_FIELDS}; use crate::model::playlist::{FetchedPlaylist, FieldAccessor, PlaylistItem}; -use crate::valid_property; -use log::{debug, log_enabled, Level}; +use crate::{debug_if_enabled, valid_property}; +use log::{Level}; type AffixProcessor<'a> = Box; @@ -13,9 +13,7 @@ fn create_affix_processor(affix: &InputAffix, is_prefix: bool) -> AffixProcessor } else { format!("{}{}", field_value.as_str(), &affix.value) }); - if log_enabled!(Level::Debug) { - debug!("Applying input {}: {}={}", if is_prefix {"prefix"} else {"suffix"}, &affix.field, &value); - } + debug_if_enabled!("Applying input {}: {}={}", if is_prefix {"prefix"} else {"suffix"}, &affix.field, &value); header.set_field(&affix.field, value.as_str()); }) } diff --git a/src/processing/playlist_processor.rs b/src/processing/playlist_processor.rs index 8d0495af2..9c0200b7e 100644 --- a/src/processing/playlist_processor.rs +++ b/src/processing/playlist_processor.rs @@ -1,12 +1,13 @@ extern crate unidecode; +use crate::debug_if_enabled; +use async_std::sync::Mutex; use core::cmp::Ordering; use std::cell::RefCell; use std::collections::{HashMap, HashSet}; use std::rc::Rc; -use std::sync::{Arc}; +use std::sync::Arc; use std::thread; -use async_std::sync::{Mutex}; use actix_rt::System; use log::{debug, error, info, log_enabled, trace, Level}; @@ -29,8 +30,8 @@ use crate::processing::xtream_processor::playlist_resolve_series; use crate::repository::playlist_repository::persist_playlist; use crate::utils::default_utils::default_as_default; use crate::utils::download; -use crate::{get_errors_notify_message, model::config, Config}; use crate::utils::request_utils::mask_sensitive_info; +use crate::{get_errors_notify_message, model::config, Config}; fn is_valid(pli: &PlaylistItem, target: &ConfigTarget) -> bool { let provider = ValueProvider { pli: RefCell::new(pli) }; @@ -135,9 +136,7 @@ fn exec_rename(pli: &PlaylistItem, rename: Option<&Vec>) { for r in renames { let value = get_field_value(result, &r.field); let cap = r.re.as_ref().unwrap().replace_all(value.as_str(), &r.new_name); - if log_enabled!(Level::Debug) { - debug!("Renamed {}={} to {}", &r.field, value, cap); - } + debug_if_enabled!("Renamed {}={} to {}", &r.field, value, cap); let value = cap.into_owned(); set_field_value(result, &r.field, Rc::new(value)); } @@ -155,9 +154,7 @@ fn rename_playlist(playlist: &mut [PlaylistGroup], target: &ConfigTarget) -> Opt for r in renames { if matches!(r.field, ItemField::Group) { let cap = r.re.as_ref().unwrap().replace_all(&grp.title, &r.new_name); - if log_enabled!(Level::Debug) { - debug!("Renamed group {} to {} for {}", &grp.title, cap, target.name); - } + debug_if_enabled!("Renamed group {} to {} for {}", &grp.title, cap, target.name); grp.title = Rc::new(cap.into_owned()); } } @@ -338,14 +335,10 @@ async fn process_source(cfg: Arc, source_idx: usize, user_targets: Arc

()); - } + debug_if_enabled!("Source has {} groups", source_playlists.iter().map(|fpl| fpl.playlistgroups.len()).sum::()); for target in &source.targets { if is_target_enabled(target, &user_targets) { match process_playlist(&mut source_playlists, target, &cfg, &mut stats, &mut errors).await { @@ -465,14 +458,12 @@ fn flatten_groups(playlistgroups: Vec) -> Vec { } async fn process_playlist(playlists: &mut [FetchedPlaylist<'_>], - target: &ConfigTarget, - cfg: &Config, - stats: &mut HashMap, - errors: &mut Vec) -> Result<(), Vec> { + target: &ConfigTarget, + cfg: &Config, + stats: &mut HashMap, + errors: &mut Vec) -> Result<(), Vec> { let pipe = get_processing_pipe(target); - if log_enabled!(Level::Debug) { - debug!("Processing order is {}", &target.processing_order); - } + debug_if_enabled!("Processing order is {}", &target.processing_order); let mut new_fetched_playlists: Vec = vec![]; for fpl in playlists.iter_mut() { @@ -502,15 +493,13 @@ async fn process_playlist(playlists: &mut [FetchedPlaylist<'_>], .filter_map(|c| c.header.borrow().epg_channel_id.clone()).collect(); new_playlist.append(&mut fp.playlistgroups); - if !epg_channel_ids.is_empty() { - if let Some(tv_guide) = fp.epg { - debug!("found epg information for {}", &target.name); - if let Some(epg) = tv_guide.filter(&epg_channel_ids) { - new_epg.push(epg); - } + if epg_channel_ids.is_empty() { + debug_if_enabled!("channel ids are empty"); + } else if let Some(tv_guide) = fp.epg { + debug!("found epg information for {}", &target.name); + if let Some(epg) = tv_guide.filter(&epg_channel_ids) { + new_epg.push(epg); } - } else if log_enabled!(Level::Debug) { - debug!("channel ids are empty"); } } diff --git a/src/repository/epg_repository.rs b/src/repository/epg_repository.rs index 02fca5d9d..4c21f5cf8 100644 --- a/src/repository/epg_repository.rs +++ b/src/repository/epg_repository.rs @@ -1,8 +1,9 @@ use std::fs::File; use std::io::{Cursor, Write}; use std::path::{Path}; -use log::{debug, log_enabled, Level}; +use log::{Level}; use quick_xml::{Writer}; +use crate::debug_if_enabled; use crate::m3u_filter_error::{M3uFilterError, M3uFilterErrorKind}; use crate::model::config::{Config, ConfigTarget, TargetOutput}; use crate::model::config::TargetType; @@ -24,9 +25,7 @@ fn epg_write_file(target: &ConfigTarget, epg: &Epg, path: &Path) -> Result<(), M } match epg_file.write_all(&result) { Ok(()) => { - if log_enabled!(Level::Debug) { - debug!("Epg for target {} written to {}", target.name, path.to_str().unwrap_or("?")); - } + debug_if_enabled!("Epg for target {} written to {}", target.name, path.to_str().unwrap_or("?")); } Err(err) => return Err(M3uFilterError::new( M3uFilterErrorKind::Notify, format!("failed to write epg: {} - {}", path.to_str().unwrap_or("?"), err))), @@ -52,18 +51,14 @@ pub fn epg_write(target: &ConfigTarget, cfg: &Config, target_path: &Path, epg: O format!("write epg for target {} failed: No filename set", target.name))); } let path = m3u_get_epg_file_path(target_path); - if log_enabled!(Level::Debug) { - debug!("writing m3u epg to {}", path.to_str().unwrap_or("?")); - } + debug_if_enabled!("writing m3u epg to {}", path.to_str().unwrap_or("?")); epg_write_file(target, epg_data, &path)?; } TargetType::Xtream => { match xtream_get_storage_path(cfg, &target.name) { Some(path) => { let epg_path = xtream_get_epg_file_path(&path); - if log_enabled!(Level::Debug) { - debug!("writing xtream epg to {}", epg_path.to_str().unwrap_or("?")); - } + debug_if_enabled!("writing xtream epg to {}", epg_path.to_str().unwrap_or("?")); epg_write_file(target, epg_data, &epg_path)?; } None => return Err(M3uFilterError::new( diff --git a/src/utils/mod.rs b/src/utils/mod.rs index c168debae..a0a0790c4 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -9,4 +9,19 @@ pub mod multi_file_reader; pub mod file_lock_manager; pub mod compressed_file_reader; mod compression_utils; -pub mod directed_graph; \ No newline at end of file +pub mod directed_graph; + +#[macro_export] +macro_rules! debug_if_enabled { + ($fmt:expr, $( $args:expr ),*) => { + if log::log_enabled!(log::Level::Debug) { + log::log!(Level::Debug, $fmt, $($args),*); + } + }; + + ($txt:expr) => { + if log::log_enabled!(log::Level::Debug) { + log::log!(Level::Debug, $txt); + } + }; +} diff --git a/src/utils/request_utils.rs b/src/utils/request_utils.rs index b2bb09ec5..251c89033 100644 --- a/src/utils/request_utils.rs +++ b/src/utils/request_utils.rs @@ -14,7 +14,6 @@ use reqwest::header::CONTENT_ENCODING; use reqwest::header::{HeaderMap, HeaderName, HeaderValue}; use url::Url; -use crate::create_m3u_filter_error_result; use crate::m3u_filter_error::{M3uFilterError, M3uFilterErrorKind}; use crate::model::config::ConfigInput; use crate::model::stats::format_elapsed_time; @@ -22,16 +21,14 @@ use crate::repository::storage::get_input_storage_path; use crate::repository::xtream_repository::FILE_EPG; use crate::utils::compression_utils::{is_deflate, is_gzip, ENCODING_DEFLATE, ENCODING_GZIP}; use crate::utils::file_utils::{get_file_path, persist_file}; +use crate::{create_m3u_filter_error_result, debug_if_enabled}; pub const fn bytes_to_megabytes(bytes: u64) -> u64 { bytes / 1_048_576 } pub async fn get_input_text_content_as_file(input: &ConfigInput, working_dir: &str, url_str: &str, persist_filepath: Option) -> Result { - if log_enabled!(Level::Debug) { - debug!("getting input text content working_dir: {}, url: {}", working_dir, mask_sensitive_info(url_str)); - } - + debug_if_enabled!("getting input text content working_dir: {}, url: {}", working_dir, mask_sensitive_info(url_str)); if url_str.parse::().is_ok() { match download_text_content_as_file(input, url_str, working_dir, persist_filepath).await { Ok(content) => Ok(content), @@ -77,9 +74,7 @@ pub async fn get_input_text_content_as_file(input: &ConfigInput, working_dir: &s pub async fn get_input_text_content(input: &ConfigInput, working_dir: &String, url_str: &str, persist_filepath: Option) -> Result { - if log_enabled!(Level::Debug) { - debug!("getting input text content working_dir: {}, url: {}", working_dir, mask_sensitive_info(url_str)); - } + debug_if_enabled!("getting input text content working_dir: {}, url: {}", working_dir, mask_sensitive_info(url_str)); if url_str.parse::().is_ok() { match download_text_content(input, url_str, persist_filepath).await { @@ -143,12 +138,12 @@ pub fn get_request_headers(defined_headers: Option<&HashMap>, cu let header_keys: HashSet = headers.keys().map(|k| k.as_str().to_lowercase()).collect(); for (key, value) in custom { let key_lc = key.to_lowercase(); - if !("host" == key_lc || header_keys.contains(key_lc.as_str())) { + if "host" == key_lc || header_keys.contains(key_lc.as_str()) { + debug_if_enabled!("Ignoring request header {}={}", key_lc, String::from_utf8_lossy(value)); + } else { headers.insert( HeaderName::from_bytes(key.as_bytes()).unwrap(), HeaderValue::from_bytes(value).unwrap()); - } else if log_enabled!(Level::Debug) { - debug!("Ignoring request header {}={}", key_lc, String::from_utf8_lossy(value)); } } } @@ -323,9 +318,7 @@ pub async fn download_text_content(input: &ConfigInput, url_str: &str, persist_f } async fn download_json_content(input: &ConfigInput, url: &str, persist_filepath: Option) -> Result { - if log_enabled!(Level::Debug) { - debug!("downloading json content from {}", mask_sensitive_info(url)); - } + debug_if_enabled!("downloading json content from {}", mask_sensitive_info(url)); match download_text_content(input, url, persist_filepath).await { Ok(content) => { match serde_json::from_str::(&content) {