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 all commits
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
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()),
}
}
}
Loading