Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add additional Logging level functions to the logging module #1441

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 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,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){
DJawna marked this conversation as resolved.
Show resolved Hide resolved
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);
}