Skip to content

Commit e97bc53

Browse files
rsglobaltyranron
andauthored
Fix buf_id issues with older SDK API version (rust-mobile#67, rust-mobile#66)
Signed-off-by: Roman Stratiienko <[email protected]> Co-authored-by: Kai Ren <[email protected]>
1 parent f85999f commit e97bc53

File tree

2 files changed

+50
-20
lines changed

2 files changed

+50
-20
lines changed

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,21 @@ All user visible changes to this project will be documented in this file. This p
66

77

88

9+
## [0.13.1] · 2023-03-??
10+
[0.13.1]: /../../tree/v0.13.1
11+
12+
[Diff](/../../compare/v0.13.0...v0.13.1)
13+
14+
### Fixed
15+
16+
- Missing logs on [Android] API 26 and earlier. ([#66], [#67])
17+
18+
[#66]: /../../issues/66
19+
[#67]: /../../pull/67
20+
21+
22+
23+
924
## [0.13.0] · 2023-02-14
1025
[0.13.0]: /../../tree/v0.13.0
1126

src/lib.rs

Lines changed: 35 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -118,32 +118,47 @@ pub enum LogId {
118118

119119
#[cfg(target_os = "android")]
120120
impl LogId {
121-
fn to_native(log_id: Option<Self>) -> log_ffi::log_id_t {
121+
const fn to_native(log_id: Option<Self>) -> Option<log_ffi::log_id_t> {
122122
match log_id {
123-
Some(Self::Main) => log_ffi::log_id_t::MAIN,
124-
Some(Self::Radio) => log_ffi::log_id_t::RADIO,
125-
Some(Self::Events) => log_ffi::log_id_t::EVENTS,
126-
Some(Self::System) => log_ffi::log_id_t::SYSTEM,
127-
Some(Self::Crash) => log_ffi::log_id_t::CRASH,
128-
Some(Self::Kernel) => log_ffi::log_id_t::KERNEL,
129-
Some(Self::Security) => log_ffi::log_id_t::SECURITY,
130-
Some(Self::Stats) => log_ffi::log_id_t::STATS,
131-
None => log_ffi::log_id_t::DEFAULT,
123+
Some(Self::Main) => Some(log_ffi::log_id_t::MAIN),
124+
Some(Self::Radio) => Some(log_ffi::log_id_t::RADIO),
125+
Some(Self::Events) => Some(log_ffi::log_id_t::EVENTS),
126+
Some(Self::System) => Some(log_ffi::log_id_t::SYSTEM),
127+
Some(Self::Crash) => Some(log_ffi::log_id_t::CRASH),
128+
Some(Self::Kernel) => Some(log_ffi::log_id_t::KERNEL),
129+
Some(Self::Security) => Some(log_ffi::log_id_t::SECURITY),
130+
Some(Self::Stats) => Some(log_ffi::log_id_t::STATS),
131+
None => None,
132132
}
133133
}
134134
}
135135

136136
/// Outputs log to Android system.
137137
#[cfg(target_os = "android")]
138-
fn android_log(buf_id: log_ffi::log_id_t, prio: log_ffi::LogPriority, tag: &CStr, msg: &CStr) {
139-
unsafe {
140-
log_ffi::__android_log_buf_write(
141-
buf_id as log_ffi::c_int,
142-
prio as log_ffi::c_int,
143-
tag.as_ptr() as *const log_ffi::c_char,
144-
msg.as_ptr() as *const log_ffi::c_char,
145-
);
146-
};
138+
fn android_log(
139+
buf_id: Option<log_ffi::log_id_t>,
140+
prio: log_ffi::LogPriority,
141+
tag: &CStr,
142+
msg: &CStr,
143+
) {
144+
if let Some(buf_id) = buf_id {
145+
unsafe {
146+
log_ffi::__android_log_buf_write(
147+
buf_id as log_ffi::c_int,
148+
prio as log_ffi::c_int,
149+
tag.as_ptr() as *const log_ffi::c_char,
150+
msg.as_ptr() as *const log_ffi::c_char,
151+
);
152+
};
153+
} else {
154+
unsafe {
155+
log_ffi::__android_log_write(
156+
prio as log_ffi::c_int,
157+
tag.as_ptr() as *const log_ffi::c_char,
158+
msg.as_ptr() as *const log_ffi::c_char,
159+
);
160+
};
161+
}
147162
}
148163

149164
/// Dummy output placeholder for tests.
@@ -337,7 +352,7 @@ pub struct PlatformLogWriter<'a> {
337352
#[cfg(not(target_os = "android"))]
338353
priority: Level,
339354
#[cfg(target_os = "android")]
340-
buf_id: log_ffi::log_id_t,
355+
buf_id: Option<log_ffi::log_id_t>,
341356
#[cfg(not(target_os = "android"))]
342357
buf_id: Option<LogId>,
343358
len: usize,

0 commit comments

Comments
 (0)