Skip to content

Commit

Permalink
Merge pull request #1441 from DJawna/AdditionalLogPrios
Browse files Browse the repository at this point in the history
Add additional Logging level functions to the logging module
  • Loading branch information
Cobrand authored Feb 18, 2025
2 parents 7236c51 + 383eb6e commit 07ae517
Showing 1 changed file with 75 additions and 0 deletions.
75 changes: 75 additions & 0 deletions src/sdl2/log.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::sys;
use std::convert::TryInto;
use std::ffi::{CStr, CString};
use std::ptr::null_mut;

Expand Down Expand Up @@ -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)]
Expand Down Expand Up @@ -104,3 +121,61 @@ pub fn log(message: &str) {
crate::sys::SDL_Log(message.as_ptr());
}
}

/// 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('%', "%%");
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 {
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()),
}
}
}

0 comments on commit 07ae517

Please sign in to comment.