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

refactor: use hilog-sys to implement it #6

Merged
merged 6 commits into from
Jun 4, 2024
Merged
Show file tree
Hide file tree
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
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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-hilogs-sys = { version = "0.0.1", path = "sys/ohos-hilogs-sys" }
3 changes: 2 additions & 1 deletion crates/hilog/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[package]
name = "ohos-hilog-binding"
version = "0.0.1"
version = "0.0.2"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "hilog binding for rust"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
ohos-hilogs-sys = { workspace = true }
46 changes: 38 additions & 8 deletions crates/hilog/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,37 @@
use ohos_hilogs_sys::OH_LOG_Print;
use std::ffi::CString;
use sys::{LogLevel, LogType, OH_LOG_Print};

mod sys;
pub enum LogType {
LogApp,
}

impl From<LogType> for ohos_hilogs_sys::LogType {
fn from(value: LogType) -> Self {
match value {
LogType::LogApp => ohos_hilogs_sys::LogType_LOG_APP,
}
}
}

pub enum LogLevel {
LogDebug,
LogInfo,
LogWarn,
LogError,
LogFatal,
}

impl From<LogLevel> for ohos_hilogs_sys::LogLevel {
fn from(value: LogLevel) -> Self {
match value {
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,
}
}
}

#[derive(Default)]
pub struct LogOptions<'a> {
Expand All @@ -27,7 +57,7 @@ macro_rules! log_factory {

unsafe {
OH_LOG_Print(
LogType::LogApp,
LogType::LogApp.into(),
$level_enum,
domain,
tag.as_ptr(),
Expand All @@ -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 {
Expand Down
28 changes: 0 additions & 28 deletions crates/hilog/src/sys.rs

This file was deleted.

7 changes: 0 additions & 7 deletions crates/init/build.rs

This file was deleted.

8 changes: 8 additions & 0 deletions sys/ohos-hilogs-sys/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
[package]
name = "ohos-hilogs-sys"
version = "0.0.1"
edition = "2021"
license = "MIT OR Apache-2.0"
description = "OpenHarmony's hilog binding for rust"

[dependencies]
3 changes: 1 addition & 2 deletions crates/hilog/build.rs → sys/ohos-hilogs-sys/build.rs
Original file line number Diff line number Diff line change
@@ -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");
}
46 changes: 46 additions & 0 deletions sys/ohos-hilogs-sys/src/lib.rs
Original file line number Diff line number Diff line change
@@ -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);
}
5 changes: 5 additions & 0 deletions tools/generate/build.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,11 @@ static CONFIG: Lazy<Vec<SysConfig>> = Lazy::new(|| {
name: "ohos-init-sys",
headers: vec!["syscap_ndk.h"],
},
// ohos-hilog-sys already exists
SysConfig {
name: "ohos-hilogs-sys",
headers: vec!["hilog/log.h"],
},
]
});

Expand Down