From e0c96ad6b478140a4ce77b7db185b345fac5fe96 Mon Sep 17 00:00:00 2001 From: jtnunley Date: Wed, 29 Mar 2023 09:19:07 -0700 Subject: [PATCH] Dedup android_log --- android-activity/src/game_activity/mod.rs | 15 +-------------- android-activity/src/native_activity/glue.rs | 14 +------------- android-activity/src/util.rs | 15 +++++++++++++++ 3 files changed, 17 insertions(+), 27 deletions(-) diff --git a/android-activity/src/game_activity/mod.rs b/android-activity/src/game_activity/mod.rs index 842865f..4c920ab 100644 --- a/android-activity/src/game_activity/mod.rs +++ b/android-activity/src/game_activity/mod.rs @@ -5,7 +5,6 @@ use std::fs::File; use std::io::{BufRead, BufReader}; use std::marker::PhantomData; use std::ops::Deref; -use std::os::raw; use std::os::unix::prelude::*; use std::ptr::NonNull; use std::sync::{Arc, RwLock}; @@ -24,6 +23,7 @@ use ndk::asset::AssetManager; use ndk::configuration::Configuration; use ndk::native_window::NativeWindow; +use crate::util::android_log; use crate::{ util, AndroidApp, ConfigurationRef, InputStatus, MainEvent, PollEvent, Rect, WindowManagerFlags, }; @@ -595,19 +595,6 @@ pub unsafe extern "C" fn GameActivity_onCreate( GameActivity_onCreate_C(activity, saved_state, saved_state_size); } -fn android_log(level: Level, tag: &CStr, msg: &CStr) { - let prio = match level { - Level::Error => ndk_sys::android_LogPriority::ANDROID_LOG_ERROR, - Level::Warn => ndk_sys::android_LogPriority::ANDROID_LOG_WARN, - Level::Info => ndk_sys::android_LogPriority::ANDROID_LOG_INFO, - Level::Debug => ndk_sys::android_LogPriority::ANDROID_LOG_DEBUG, - Level::Trace => ndk_sys::android_LogPriority::ANDROID_LOG_VERBOSE, - }; - unsafe { - ndk_sys::__android_log_write(prio.0 as raw::c_int, tag.as_ptr(), msg.as_ptr()); - } -} - extern "Rust" { pub fn android_main(app: AndroidApp); } diff --git a/android-activity/src/native_activity/glue.rs b/android-activity/src/native_activity/glue.rs index 4e2b863..9c33ce4 100644 --- a/android-activity/src/native_activity/glue.rs +++ b/android-activity/src/native_activity/glue.rs @@ -15,6 +15,7 @@ use std::{ use log::Level; use ndk::{configuration::Configuration, input_queue::InputQueue, native_window::NativeWindow}; +use crate::util::android_log; use crate::ConfigurationRef; use super::{AndroidApp, Rect}; @@ -611,19 +612,6 @@ extern "Rust" { pub fn android_main(app: AndroidApp); } -fn android_log(level: Level, tag: &CStr, msg: &CStr) { - let prio = match level { - Level::Error => ndk_sys::android_LogPriority::ANDROID_LOG_ERROR, - Level::Warn => ndk_sys::android_LogPriority::ANDROID_LOG_WARN, - Level::Info => ndk_sys::android_LogPriority::ANDROID_LOG_INFO, - Level::Debug => ndk_sys::android_LogPriority::ANDROID_LOG_DEBUG, - Level::Trace => ndk_sys::android_LogPriority::ANDROID_LOG_VERBOSE, - }; - unsafe { - ndk_sys::__android_log_write(prio.0 as libc::c_int, tag.as_ptr(), msg.as_ptr()); - } -} - unsafe fn try_with_waitable_activity_ref( activity: *mut ndk_sys::ANativeActivity, closure: impl FnOnce(Arc), diff --git a/android-activity/src/util.rs b/android-activity/src/util.rs index 6069d7e..bd5b6b1 100644 --- a/android-activity/src/util.rs +++ b/android-activity/src/util.rs @@ -1,5 +1,7 @@ use std::{ffi::CStr, os::raw::c_char}; +use log::Level; + pub fn try_get_path_from_ptr(path: *const c_char) -> Option { if path.is_null() { return None; @@ -13,3 +15,16 @@ pub fn try_get_path_from_ptr(path: *const c_char) -> Option } Some(std::path::PathBuf::from(cstr)) } + +pub(crate) fn android_log(level: Level, tag: &CStr, msg: &CStr) { + let prio = match level { + Level::Error => ndk_sys::android_LogPriority::ANDROID_LOG_ERROR, + Level::Warn => ndk_sys::android_LogPriority::ANDROID_LOG_WARN, + Level::Info => ndk_sys::android_LogPriority::ANDROID_LOG_INFO, + Level::Debug => ndk_sys::android_LogPriority::ANDROID_LOG_DEBUG, + Level::Trace => ndk_sys::android_LogPriority::ANDROID_LOG_VERBOSE, + }; + unsafe { + ndk_sys::__android_log_write(prio.0 as libc::c_int, tag.as_ptr(), msg.as_ptr()); + } +}