From e7e9542ca1fbde36984c6ffcb8f5ccd91be361ba Mon Sep 17 00:00:00 2001 From: southorange0929 Date: Tue, 4 Jun 2024 09:26:12 +0800 Subject: [PATCH 1/6] refactor: use ohos-hilog-sys to impl hilog binding --- Cargo.toml | 2 + crates/hilog/Cargo.toml | 1 + crates/hilog/src/lib.rs | 46 +++++++++++++++---- crates/hilog/src/sys.rs | 28 ----------- crates/init/build.rs | 7 --- sys/ohos-hilog-sys/Cargo.toml | 8 ++++ {crates/hilog => sys/ohos-hilog-sys}/build.rs | 3 +- sys/ohos-hilog-sys/src/lib.rs | 46 +++++++++++++++++++ tools/generate/build.rs | 4 ++ 9 files changed, 100 insertions(+), 45 deletions(-) delete mode 100644 crates/hilog/src/sys.rs delete mode 100644 crates/init/build.rs create mode 100644 sys/ohos-hilog-sys/Cargo.toml rename {crates/hilog => sys/ohos-hilog-sys}/build.rs (76%) create mode 100644 sys/ohos-hilog-sys/src/lib.rs diff --git a/Cargo.toml b/Cargo.toml index bf343f6..d606b2d 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -5,6 +5,8 @@ resolver = "2" [workspace.dependencies] ohos-bundle-binding = { version = "0.0.1", path = "crates/bundle" } ohos-init-binding = { version = "0.0.3", path = "crates/init" } +ohos-hilog-binding = { version = "0.0.2", path = "crates/hilog" } ohos-bundle-sys = { version = "0.0.1", path = "sys/ohos-bundle-sys" } ohos-init-sys = { version = "0.0.1", path = "sys/ohos-init-sys" } +ohos-hilog-sys = { version = "0.0.1", path = "sys/ohos-hilog-sys" } diff --git a/crates/hilog/Cargo.toml b/crates/hilog/Cargo.toml index 9ddae51..5b7b2fd 100644 --- a/crates/hilog/Cargo.toml +++ b/crates/hilog/Cargo.toml @@ -8,3 +8,4 @@ description = "hilog binding for rust" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] +ohos-hilog-sys = { workspace = true } diff --git a/crates/hilog/src/lib.rs b/crates/hilog/src/lib.rs index 5804655..38efd9c 100644 --- a/crates/hilog/src/lib.rs +++ b/crates/hilog/src/lib.rs @@ -1,7 +1,37 @@ +use ohos_hilog_sys::OH_LOG_Print; use std::ffi::CString; -use sys::{LogLevel, LogType, OH_LOG_Print}; -mod sys; +pub enum LogType { + LogApp, +} + +impl From for ohos_hilog_sys::LogType { + fn from(value: LogType) -> Self { + match value { + LogType::LogApp => ohos_hilog_sys::LogType_LOG_APP, + } + } +} + +pub enum LogLevel { + LogDebug, + LogInfo, + LogWarn, + LogError, + LogFatal, +} + +impl From for ohos_hilog_sys::LogLevel { + fn from(value: LogLevel) -> Self { + match value { + LogLevel::LogDebug => ohos_hilog_sys::LogLevel_LOG_DEBUG, + LogLevel::LogInfo => ohos_hilog_sys::LogLevel_LOG_INFO, + LogLevel::LogWarn => ohos_hilog_sys::LogLevel_LOG_WARN, + LogLevel::LogError => ohos_hilog_sys::LogLevel_LOG_ERROR, + LogLevel::LogFatal => ohos_hilog_sys::LogLevel_LOG_FATAL, + } + } +} #[derive(Default)] pub struct LogOptions<'a> { @@ -27,7 +57,7 @@ macro_rules! log_factory { unsafe { OH_LOG_Print( - LogType::LogApp, + LogType::LogApp.into(), $level_enum, domain, tag.as_ptr(), @@ -38,11 +68,11 @@ macro_rules! log_factory { }; } -log_factory!(debug, LogLevel::LogDebug); -log_factory!(info, LogLevel::LogInfo); -log_factory!(warn, LogLevel::LogWarn); -log_factory!(error, LogLevel::LogError); -log_factory!(fatal, LogLevel::LogFatal); +log_factory!(debug, LogLevel::LogDebug.into()); +log_factory!(info, LogLevel::LogInfo.into()); +log_factory!(warn, LogLevel::LogWarn.into()); +log_factory!(error, LogLevel::LogError.into()); +log_factory!(fatal, LogLevel::LogFatal.into()); #[macro_export] macro_rules! hilog_debug { diff --git a/crates/hilog/src/sys.rs b/crates/hilog/src/sys.rs deleted file mode 100644 index efd721a..0000000 --- a/crates/hilog/src/sys.rs +++ /dev/null @@ -1,28 +0,0 @@ -use std::ffi::c_char; - -/// 打印日志类型 第三方应用仅有此类型 -#[repr(C)] -pub enum LogType { - LogApp, -} - -/// 日志等级 -#[repr(C)] -pub enum LogLevel { - LogDebug = 3, - LogInfo, - LogWarn, - LogError, - LogFatal, -} - -extern "C" { - // 接受format之后的&str参数 - pub fn OH_LOG_Print( - type_: LogType, - level: LogLevel, - domain: u32, - tag: *const c_char, - fmt: *const c_char, - ); -} diff --git a/crates/init/build.rs b/crates/init/build.rs deleted file mode 100644 index 7bd4510..0000000 --- a/crates/init/build.rs +++ /dev/null @@ -1,7 +0,0 @@ -use std::env; - -pub fn main() { - let _ndk = env::var("OHOS_NDK_HOME").expect("OHOS_NDK_HOME not set"); - // link libdeviceinfo_ndk.z.so - println!("cargo:rustc-link-lib=dylib=deviceinfo_ndk.z"); -} diff --git a/sys/ohos-hilog-sys/Cargo.toml b/sys/ohos-hilog-sys/Cargo.toml new file mode 100644 index 0000000..f41c516 --- /dev/null +++ b/sys/ohos-hilog-sys/Cargo.toml @@ -0,0 +1,8 @@ +[package] +name = "ohos-hilog-sys" +version = "0.0.1" +edition = "2021" +license = "MIT OR Apache-2.0" +description = "OpenHarmony's hilog binding for rust" + +[dependencies] diff --git a/crates/hilog/build.rs b/sys/ohos-hilog-sys/build.rs similarity index 76% rename from crates/hilog/build.rs rename to sys/ohos-hilog-sys/build.rs index f8dfa21..25c8223 100644 --- a/crates/hilog/build.rs +++ b/sys/ohos-hilog-sys/build.rs @@ -1,7 +1,6 @@ use std::env; -pub fn main() { +fn main() { let _ndk = env::var("OHOS_NDK_HOME").expect("OHOS_NDK_HOME not set"); - // link libhilog_ndk.z.so println!("cargo:rustc-link-lib=dylib=hilog_ndk.z"); } diff --git a/sys/ohos-hilog-sys/src/lib.rs b/sys/ohos-hilog-sys/src/lib.rs new file mode 100644 index 0000000..d32b10b --- /dev/null +++ b/sys/ohos-hilog-sys/src/lib.rs @@ -0,0 +1,46 @@ +/* automatically generated by rust-bindgen 0.65.1 */ + +pub const __GNUC_VA_LIST: u32 = 1; +pub const __bool_true_false_are_defined: u32 = 1; +pub const true_: u32 = 1; +pub const false_: u32 = 0; +pub const LOG_DOMAIN: u32 = 0; +pub type va_list = [u64; 4usize]; +pub type __gnuc_va_list = [u64; 4usize]; +pub const LogType_LOG_APP: LogType = 0; +pub type LogType = ::std::os::raw::c_uint; +pub const LogLevel_LOG_DEBUG: LogLevel = 3; +pub const LogLevel_LOG_INFO: LogLevel = 4; +pub const LogLevel_LOG_WARN: LogLevel = 5; +pub const LogLevel_LOG_ERROR: LogLevel = 6; +pub const LogLevel_LOG_FATAL: LogLevel = 7; +pub type LogLevel = ::std::os::raw::c_uint; +extern "C" { + pub fn OH_LOG_Print( + type_: LogType, + level: LogLevel, + domain: ::std::os::raw::c_uint, + tag: *const ::std::os::raw::c_char, + fmt: *const ::std::os::raw::c_char, + ... + ) -> ::std::os::raw::c_int; +} +extern "C" { + pub fn OH_LOG_IsLoggable( + domain: ::std::os::raw::c_uint, + tag: *const ::std::os::raw::c_char, + level: LogLevel, + ) -> bool; +} +pub type LogCallback = ::std::option::Option< + unsafe extern "C" fn( + type_: LogType, + level: LogLevel, + domain: ::std::os::raw::c_uint, + tag: *const ::std::os::raw::c_char, + msg: *const ::std::os::raw::c_char, + ), +>; +extern "C" { + pub fn OH_LOG_SetCallback(callback: LogCallback); +} diff --git a/tools/generate/build.rs b/tools/generate/build.rs index 2cfeda6..4bd0198 100644 --- a/tools/generate/build.rs +++ b/tools/generate/build.rs @@ -21,6 +21,10 @@ static CONFIG: Lazy> = Lazy::new(|| { name: "ohos-init-sys", headers: vec!["syscap_ndk.h"], }, + SysConfig { + name: "ohos-hilog-sys", + headers: vec!["hilog/log.h"], + }, ] }); From 4856446c4a84009c867946427485e6aac246295b Mon Sep 17 00:00:00 2001 From: southorange0929 Date: Tue, 4 Jun 2024 09:34:50 +0800 Subject: [PATCH 2/6] chore: rename ohos-hilog-sys to ohos-hilogs-sys --- Cargo.toml | 2 +- crates/hilog/Cargo.toml | 4 ++-- sys/{ohos-hilog-sys => ohos-hilogs-sys}/Cargo.toml | 4 ++-- sys/{ohos-hilog-sys => ohos-hilogs-sys}/build.rs | 0 sys/{ohos-hilog-sys => ohos-hilogs-sys}/src/lib.rs | 0 tools/generate/build.rs | 3 ++- 6 files changed, 7 insertions(+), 6 deletions(-) rename sys/{ohos-hilog-sys => ohos-hilogs-sys}/Cargo.toml (71%) rename sys/{ohos-hilog-sys => ohos-hilogs-sys}/build.rs (100%) rename sys/{ohos-hilog-sys => ohos-hilogs-sys}/src/lib.rs (100%) diff --git a/Cargo.toml b/Cargo.toml index d606b2d..a55bb3e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -9,4 +9,4 @@ ohos-hilog-binding = { version = "0.0.2", path = "crates/hilog" } ohos-bundle-sys = { version = "0.0.1", path = "sys/ohos-bundle-sys" } ohos-init-sys = { version = "0.0.1", path = "sys/ohos-init-sys" } -ohos-hilog-sys = { version = "0.0.1", path = "sys/ohos-hilog-sys" } +ohos-hilogs-sys = { version = "0.0.1", path = "sys/ohos-hilogs-sys" } diff --git a/crates/hilog/Cargo.toml b/crates/hilog/Cargo.toml index 5b7b2fd..0e7a8f4 100644 --- a/crates/hilog/Cargo.toml +++ b/crates/hilog/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "ohos-hilog-binding" +name = "ohos-hilogs-binding" version = "0.0.1" edition = "2021" license = "MIT OR Apache-2.0" @@ -8,4 +8,4 @@ description = "hilog binding for rust" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html [dependencies] -ohos-hilog-sys = { workspace = true } +ohos-hilogs-sys = { workspace = true } diff --git a/sys/ohos-hilog-sys/Cargo.toml b/sys/ohos-hilogs-sys/Cargo.toml similarity index 71% rename from sys/ohos-hilog-sys/Cargo.toml rename to sys/ohos-hilogs-sys/Cargo.toml index f41c516..5cb6fb2 100644 --- a/sys/ohos-hilog-sys/Cargo.toml +++ b/sys/ohos-hilogs-sys/Cargo.toml @@ -1,6 +1,6 @@ [package] -name = "ohos-hilog-sys" -version = "0.0.1" +name = "ohos-hilogs-sys" +version = "0.1.0" edition = "2021" license = "MIT OR Apache-2.0" description = "OpenHarmony's hilog binding for rust" diff --git a/sys/ohos-hilog-sys/build.rs b/sys/ohos-hilogs-sys/build.rs similarity index 100% rename from sys/ohos-hilog-sys/build.rs rename to sys/ohos-hilogs-sys/build.rs diff --git a/sys/ohos-hilog-sys/src/lib.rs b/sys/ohos-hilogs-sys/src/lib.rs similarity index 100% rename from sys/ohos-hilog-sys/src/lib.rs rename to sys/ohos-hilogs-sys/src/lib.rs diff --git a/tools/generate/build.rs b/tools/generate/build.rs index 4bd0198..5da880f 100644 --- a/tools/generate/build.rs +++ b/tools/generate/build.rs @@ -21,8 +21,9 @@ static CONFIG: Lazy> = Lazy::new(|| { name: "ohos-init-sys", headers: vec!["syscap_ndk.h"], }, + // ohos-hilog-sys already exists SysConfig { - name: "ohos-hilog-sys", + name: "ohos-hilogs-sys", headers: vec!["hilog/log.h"], }, ] From 34a911d72992f447d17d4ca0e0bbd18378693f32 Mon Sep 17 00:00:00 2001 From: southorange0929 Date: Tue, 4 Jun 2024 09:35:28 +0800 Subject: [PATCH 3/6] chore: bump version --- sys/ohos-hilogs-sys/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/sys/ohos-hilogs-sys/Cargo.toml b/sys/ohos-hilogs-sys/Cargo.toml index 5cb6fb2..1c9e688 100644 --- a/sys/ohos-hilogs-sys/Cargo.toml +++ b/sys/ohos-hilogs-sys/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ohos-hilogs-sys" -version = "0.1.0" +version = "0.0.1" edition = "2021" license = "MIT OR Apache-2.0" description = "OpenHarmony's hilog binding for rust" From 8bbaaf4de87191abe905ebf3ffcd93856c0fcc22 Mon Sep 17 00:00:00 2001 From: southorange0929 Date: Tue, 4 Jun 2024 09:36:50 +0800 Subject: [PATCH 4/6] fix: binding import failed --- crates/hilog/src/lib.rs | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/crates/hilog/src/lib.rs b/crates/hilog/src/lib.rs index 38efd9c..ea9e8c2 100644 --- a/crates/hilog/src/lib.rs +++ b/crates/hilog/src/lib.rs @@ -1,14 +1,14 @@ -use ohos_hilog_sys::OH_LOG_Print; +use ohos_hilogs_sys::OH_LOG_Print; use std::ffi::CString; pub enum LogType { LogApp, } -impl From for ohos_hilog_sys::LogType { +impl From for ohos_hilogs_sys::LogType { fn from(value: LogType) -> Self { match value { - LogType::LogApp => ohos_hilog_sys::LogType_LOG_APP, + LogType::LogApp => ohos_hilogs_sys::LogType_LOG_APP, } } } @@ -21,14 +21,14 @@ pub enum LogLevel { LogFatal, } -impl From for ohos_hilog_sys::LogLevel { +impl From for ohos_hilogs_sys::LogLevel { fn from(value: LogLevel) -> Self { match value { - LogLevel::LogDebug => ohos_hilog_sys::LogLevel_LOG_DEBUG, - LogLevel::LogInfo => ohos_hilog_sys::LogLevel_LOG_INFO, - LogLevel::LogWarn => ohos_hilog_sys::LogLevel_LOG_WARN, - LogLevel::LogError => ohos_hilog_sys::LogLevel_LOG_ERROR, - LogLevel::LogFatal => ohos_hilog_sys::LogLevel_LOG_FATAL, + LogLevel::LogDebug => ohos_hilogs_sys::LogLevel_LOG_DEBUG, + LogLevel::LogInfo => ohos_hilogs_sys::LogLevel_LOG_INFO, + LogLevel::LogWarn => ohos_hilogs_sys::LogLevel_LOG_WARN, + LogLevel::LogError => ohos_hilogs_sys::LogLevel_LOG_ERROR, + LogLevel::LogFatal => ohos_hilogs_sys::LogLevel_LOG_FATAL, } } } From af8e20c3b4d9fe2944a28141f791aa6d3d81b755 Mon Sep 17 00:00:00 2001 From: southorange0929 Date: Tue, 4 Jun 2024 09:37:22 +0800 Subject: [PATCH 5/6] chore: bump version --- crates/hilog/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/hilog/Cargo.toml b/crates/hilog/Cargo.toml index 0e7a8f4..a443b7e 100644 --- a/crates/hilog/Cargo.toml +++ b/crates/hilog/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ohos-hilogs-binding" -version = "0.0.1" +version = "0.0.2" edition = "2021" license = "MIT OR Apache-2.0" description = "hilog binding for rust" From cbc880fb624beeccb6261fee725861e35b400446 Mon Sep 17 00:00:00 2001 From: southorange0929 Date: Tue, 4 Jun 2024 09:39:11 +0800 Subject: [PATCH 6/6] chore: use hilog binding --- crates/hilog/Cargo.toml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/crates/hilog/Cargo.toml b/crates/hilog/Cargo.toml index a443b7e..42e801b 100644 --- a/crates/hilog/Cargo.toml +++ b/crates/hilog/Cargo.toml @@ -1,5 +1,5 @@ [package] -name = "ohos-hilogs-binding" +name = "ohos-hilog-binding" version = "0.0.2" edition = "2021" license = "MIT OR Apache-2.0"