From 1b76b49486d2ec962bd5d9441eaf2ca635529bec Mon Sep 17 00:00:00 2001 From: Daniel Jawna Date: Tue, 22 Oct 2024 12:27:34 +0200 Subject: [PATCH 1/4] Add additional Logging level functions to the logging module --- src/sdl2/log.rs | 93 +++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) diff --git a/src/sdl2/log.rs b/src/sdl2/log.rs index a48e07bcfc..3c48ce6bc6 100644 --- a/src/sdl2/log.rs +++ b/src/sdl2/log.rs @@ -1,4 +1,5 @@ use crate::sys; +use std::convert::TryInto; use std::ffi::{CStr, CString}; use std::ptr::null_mut; @@ -42,6 +43,22 @@ impl Category { Category::Custom } } + + fn into_ll(value: Category) -> u32 { + return match value { + Category::Application => sys::SDL_LogCategory::SDL_LOG_CATEGORY_APPLICATION as u32, + Category::Error => sys::SDL_LogCategory::SDL_LOG_CATEGORY_ERROR as u32, + Category::Assert => sys::SDL_LogCategory::SDL_LOG_CATEGORY_ASSERT as u32, + Category::System => sys::SDL_LogCategory::SDL_LOG_CATEGORY_SYSTEM as u32, + Category::Audio => sys::SDL_LogCategory::SDL_LOG_CATEGORY_AUDIO as u32, + Category::Video => sys::SDL_LogCategory::SDL_LOG_CATEGORY_VIDEO as u32, + Category::Render => sys::SDL_LogCategory::SDL_LOG_CATEGORY_RENDER as u32, + Category::Input => sys::SDL_LogCategory::SDL_LOG_CATEGORY_INPUT as u32, + Category::Test => sys::SDL_LogCategory::SDL_LOG_CATEGORY_TEST as u32, + Category::Custom => sys::SDL_LogCategory::SDL_LOG_CATEGORY_CUSTOM as u32, + Category::Unknown => sys::SDL_LogCategory::SDL_LOG_CATEGORY_APPLICATION as u32 + } + } } #[derive(Debug, Copy, Clone, PartialEq, Eq, Hash)] @@ -104,3 +121,79 @@ pub fn log(message: &str) { crate::sys::SDL_Log(message.as_ptr()); } } + +/// Critical log function which takes as priority CRITICAL +#[doc(alias = "SDL_LogCritical")] +pub fn log_critical(message: &str, category: Category) { + log_with_category(message, category, sdl_log_critical); +} + +/// Debug log function which takes as priority DEBUG +#[doc(alias = "SDL_LogDebug")] +pub fn log_debug(message: &str, category: Category) { + log_with_category(message, category, sdl_log_debug); +} + +/// Error log function which takes as priority ERROR +#[doc(alias = "SDL_LogError")] +pub fn log_error(message: &str, category: Category) { + log_with_category(message, category, sdl_log_error); +} + +/// Info log function which takes as priority INFO +#[doc(alias = "SDL_LogInfo")] +pub fn log_info(message: &str, category: Category) { + log_with_category(message, category, sdl_log_info); +} + +/// Verbose log function which takes as priority VERBOSE +#[doc(alias = "SDL_LogVerbose")] +pub fn log_verbose(message: &str, category: Category) { + log_with_category(message, category, sdl_log_verbose); +} + +/// Warn log function which takes as priority WARN +#[doc(alias = "SDL_LogWarn")] +pub fn log_warn(message: &str, category: Category) { + log_with_category(message, category, sdl_log_warn); +} + +/// uses the sdl_log_func to log the message, when category cannot be converted, then Category: Application will be used +fn log_with_category(message: &str, category: Category, sdl_log_func: unsafe fn(category: libc::c_int, fmt: *const libc::c_char)) { + let message = message.replace('%', "%%"); + let message = CString::new(message).unwrap(); + let uccategory = Category::into_ll(category).try_into(); + let ccategory = match uccategory { + Err(_) => Category::into_ll(Category::Application).try_into().unwrap(), + Ok(success) => success + }; + + unsafe { + sdl_log_func(ccategory, message.as_ptr()); + } +} + +unsafe fn sdl_log_critical(category: libc::c_int, fmt: *const libc::c_char){ + crate::sys::SDL_LogCritical(category,fmt); +} + +unsafe fn sdl_log_debug(category: libc::c_int, fmt: *const libc::c_char){ + crate::sys::SDL_LogDebug(category,fmt); +} + +unsafe fn sdl_log_error(category: libc::c_int, fmt: *const libc::c_char){ + crate::sys::SDL_LogError(category,fmt); +} + +unsafe fn sdl_log_info(category: libc::c_int, fmt: *const libc::c_char){ + crate::sys::SDL_LogInfo(category,fmt); +} + + +unsafe fn sdl_log_verbose(category: libc::c_int, fmt: *const libc::c_char){ + crate::sys::SDL_LogVerbose(category,fmt); +} + +unsafe fn sdl_log_warn(category: libc::c_int, fmt: *const libc::c_char){ + crate::sys::SDL_LogWarn(category,fmt); +} \ No newline at end of file From 0a4edf8e592d4e875cd0d0b89059d5c5229233f3 Mon Sep 17 00:00:00 2001 From: Daniel Jawna Date: Sat, 26 Oct 2024 21:29:45 +0200 Subject: [PATCH 2/4] Replace the log level specific wrapper functions with inline lambdas cargo fmt the code --- src/sdl2/log.rs | 75 +++++++++++++++++++++++++------------------------ 1 file changed, 39 insertions(+), 36 deletions(-) diff --git a/src/sdl2/log.rs b/src/sdl2/log.rs index 3c48ce6bc6..d9b8278bbb 100644 --- a/src/sdl2/log.rs +++ b/src/sdl2/log.rs @@ -56,8 +56,8 @@ impl Category { Category::Input => sys::SDL_LogCategory::SDL_LOG_CATEGORY_INPUT as u32, Category::Test => sys::SDL_LogCategory::SDL_LOG_CATEGORY_TEST as u32, Category::Custom => sys::SDL_LogCategory::SDL_LOG_CATEGORY_CUSTOM as u32, - Category::Unknown => sys::SDL_LogCategory::SDL_LOG_CATEGORY_APPLICATION as u32 - } + Category::Unknown => sys::SDL_LogCategory::SDL_LOG_CATEGORY_APPLICATION as u32, + }; } } @@ -125,75 +125,78 @@ pub fn log(message: &str) { /// Critical log function which takes as priority CRITICAL #[doc(alias = "SDL_LogCritical")] pub fn log_critical(message: &str, category: Category) { - log_with_category(message, category, sdl_log_critical); + unsafe { + log_with_category(message, category, |category, fmt| { + crate::sys::SDL_LogCritical(category, fmt) + }); + } } /// Debug log function which takes as priority DEBUG #[doc(alias = "SDL_LogDebug")] pub fn log_debug(message: &str, category: Category) { - log_with_category(message, category, sdl_log_debug); + unsafe { + log_with_category(message, category, |category, fmt| { + crate::sys::SDL_LogDebug(category, fmt) + }); + } } /// Error log function which takes as priority ERROR #[doc(alias = "SDL_LogError")] pub fn log_error(message: &str, category: Category) { - log_with_category(message, category, sdl_log_error); + unsafe { + log_with_category(message, category, |category, fmt| { + crate::sys::SDL_LogError(category, fmt) + }); + } } /// Info log function which takes as priority INFO #[doc(alias = "SDL_LogInfo")] pub fn log_info(message: &str, category: Category) { - log_with_category(message, category, sdl_log_info); + unsafe { + log_with_category(message, category, |category, fmt| { + crate::sys::SDL_LogInfo(category, fmt) + }); + } } /// Verbose log function which takes as priority VERBOSE #[doc(alias = "SDL_LogVerbose")] pub fn log_verbose(message: &str, category: Category) { - log_with_category(message, category, sdl_log_verbose); + unsafe { + log_with_category(message, category, |category, fmt| { + crate::sys::SDL_LogVerbose(category, fmt) + }); + } } /// Warn log function which takes as priority WARN #[doc(alias = "SDL_LogWarn")] pub fn log_warn(message: &str, category: Category) { - log_with_category(message, category, sdl_log_warn); + unsafe { + log_with_category(message, category, |category, fmt| { + crate::sys::SDL_LogWarn(category, fmt) + }); + } } /// uses the sdl_log_func to log the message, when category cannot be converted, then Category: Application will be used -fn log_with_category(message: &str, category: Category, sdl_log_func: unsafe fn(category: libc::c_int, fmt: *const libc::c_char)) { +fn log_with_category( + message: &str, + category: Category, + sdl_log_func: unsafe fn(category: libc::c_int, fmt: *const libc::c_char), +) { let message = message.replace('%', "%%"); let message = CString::new(message).unwrap(); - let uccategory = Category::into_ll(category).try_into(); + let uccategory = Category::into_ll(category).try_into(); let ccategory = match uccategory { Err(_) => Category::into_ll(Category::Application).try_into().unwrap(), - Ok(success) => success + Ok(success) => success, }; unsafe { sdl_log_func(ccategory, message.as_ptr()); } } - -unsafe fn sdl_log_critical(category: libc::c_int, fmt: *const libc::c_char){ - crate::sys::SDL_LogCritical(category,fmt); -} - -unsafe fn sdl_log_debug(category: libc::c_int, fmt: *const libc::c_char){ - crate::sys::SDL_LogDebug(category,fmt); -} - -unsafe fn sdl_log_error(category: libc::c_int, fmt: *const libc::c_char){ - crate::sys::SDL_LogError(category,fmt); -} - -unsafe fn sdl_log_info(category: libc::c_int, fmt: *const libc::c_char){ - crate::sys::SDL_LogInfo(category,fmt); -} - - -unsafe fn sdl_log_verbose(category: libc::c_int, fmt: *const libc::c_char){ - crate::sys::SDL_LogVerbose(category,fmt); -} - -unsafe fn sdl_log_warn(category: libc::c_int, fmt: *const libc::c_char){ - crate::sys::SDL_LogWarn(category,fmt); -} \ No newline at end of file From eb781c2902d103ee13f174d9ec5d75c8ab36269f Mon Sep 17 00:00:00 2001 From: Daniel Jawna Date: Mon, 28 Oct 2024 16:44:48 +0100 Subject: [PATCH 3/4] consolidate the different logging wrapper functions into one function which accepts priority enum as parameter --- src/sdl2/log.rs | 77 +++++++------------------------------------------ 1 file changed, 10 insertions(+), 67 deletions(-) diff --git a/src/sdl2/log.rs b/src/sdl2/log.rs index d9b8278bbb..e9fd9e4e45 100644 --- a/src/sdl2/log.rs +++ b/src/sdl2/log.rs @@ -122,72 +122,8 @@ pub fn log(message: &str) { } } -/// Critical log function which takes as priority CRITICAL -#[doc(alias = "SDL_LogCritical")] -pub fn log_critical(message: &str, category: Category) { - unsafe { - log_with_category(message, category, |category, fmt| { - crate::sys::SDL_LogCritical(category, fmt) - }); - } -} - -/// Debug log function which takes as priority DEBUG -#[doc(alias = "SDL_LogDebug")] -pub fn log_debug(message: &str, category: Category) { - unsafe { - log_with_category(message, category, |category, fmt| { - crate::sys::SDL_LogDebug(category, fmt) - }); - } -} - -/// Error log function which takes as priority ERROR -#[doc(alias = "SDL_LogError")] -pub fn log_error(message: &str, category: Category) { - unsafe { - log_with_category(message, category, |category, fmt| { - crate::sys::SDL_LogError(category, fmt) - }); - } -} - -/// Info log function which takes as priority INFO -#[doc(alias = "SDL_LogInfo")] -pub fn log_info(message: &str, category: Category) { - unsafe { - log_with_category(message, category, |category, fmt| { - crate::sys::SDL_LogInfo(category, fmt) - }); - } -} - -/// Verbose log function which takes as priority VERBOSE -#[doc(alias = "SDL_LogVerbose")] -pub fn log_verbose(message: &str, category: Category) { - unsafe { - log_with_category(message, category, |category, fmt| { - crate::sys::SDL_LogVerbose(category, fmt) - }); - } -} - -/// Warn log function which takes as priority WARN -#[doc(alias = "SDL_LogWarn")] -pub fn log_warn(message: &str, category: Category) { - unsafe { - log_with_category(message, category, |category, fmt| { - crate::sys::SDL_LogWarn(category, fmt) - }); - } -} - -/// uses the sdl_log_func to log the message, when category cannot be converted, then Category: Application will be used -fn log_with_category( - message: &str, - category: Category, - sdl_log_func: unsafe fn(category: libc::c_int, fmt: *const libc::c_char), -) { +/// Log function where Category and Priority can be specified +pub fn log_with_category(message: &str, category: Category, priority: Priority) { let message = message.replace('%', "%%"); let message = CString::new(message).unwrap(); let uccategory = Category::into_ll(category).try_into(); @@ -197,6 +133,13 @@ fn log_with_category( }; unsafe { - sdl_log_func(ccategory, message.as_ptr()); + match priority { + Priority::Critical => crate::sys::SDL_LogCritical(ccategory, message.as_ptr()), + Priority::Debug => crate::sys::SDL_LogDebug(ccategory, message.as_ptr()), + Priority::Error => crate::sys::SDL_LogError(ccategory, message.as_ptr()), + Priority::Info => crate::sys::SDL_LogInfo(ccategory, message.as_ptr()), + Priority::Verbose => crate::sys::SDL_LogVerbose(ccategory, message.as_ptr()), + Priority::Warn => crate::sys::SDL_LogWarn(ccategory, message.as_ptr()), + } } } From 383eb6eb0d7b42126770b6eabc01e72998191a6f Mon Sep 17 00:00:00 2001 From: Daniel Jawna Date: Mon, 28 Oct 2024 18:19:55 +0100 Subject: [PATCH 4/4] Introduced Logging priority based loggin functions which call the generic logging function --- src/sdl2/log.rs | 36 ++++++++++++++++++++++++++++++++++++ 1 file changed, 36 insertions(+) diff --git a/src/sdl2/log.rs b/src/sdl2/log.rs index e9fd9e4e45..6bb07cf0fb 100644 --- a/src/sdl2/log.rs +++ b/src/sdl2/log.rs @@ -122,6 +122,42 @@ pub fn log(message: &str) { } } +/// Log function which takes as priority CRITICAL and category APPLICATION +#[doc(alias = "SDL_LogCritial")] +pub fn log_critical(message: &str) { + log_with_category(message, Category::Application, Priority::Critical); +} + +/// Log function which takes as priority DEBUG and category APPLICATION +#[doc(alias = "SDL_LogDebug")] +pub fn log_debug(message: &str) { + log_with_category(message, Category::Application, Priority::Debug); +} + +/// Log function which takes as priority ERROR and category APPLICATION +#[doc(alias = "SDL_LogError")] +pub fn log_error(message: &str) { + log_with_category(message, Category::Application, Priority::Error); +} + +/// Log function which takes as priority INFO and category APPLICATION +#[doc(alias = "SDL_LogInfo")] +pub fn log_info(message: &str) { + log_with_category(message, Category::Application, Priority::Info); +} + +/// Log function which takes as priority VERBOSE and category APPLICATION +#[doc(alias = "SDL_LogVerbose")] +pub fn log_verbose(message: &str) { + log_with_category(message, Category::Application, Priority::Verbose); +} + +/// Log function which takes as priority WARN and category APPLICATION +#[doc(alias = "SDL_LogWarn")] +pub fn log_warn(message: &str) { + log_with_category(message, Category::Application, Priority::Warn); +} + /// Log function where Category and Priority can be specified pub fn log_with_category(message: &str, category: Category, priority: Priority) { let message = message.replace('%', "%%");