Skip to content

Commit

Permalink
consolidate the different logging wrapper functions into one
Browse files Browse the repository at this point in the history
function which accepts priority enum as parameter
  • Loading branch information
DJawna committed Oct 28, 2024
1 parent 0a4edf8 commit eb781c2
Showing 1 changed file with 10 additions and 67 deletions.
77 changes: 10 additions & 67 deletions src/sdl2/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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();
Expand All @@ -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()),
}
}
}

0 comments on commit eb781c2

Please sign in to comment.