From fbe9fe4480a8cfdfa721720d40fbc58f1912cc59 Mon Sep 17 00:00:00 2001 From: buzhenwang Date: Mon, 26 Dec 2022 16:22:59 +0800 Subject: [PATCH 1/2] hilog rust ffi test Signed-off-by: buzhenwang --- interfaces/bundle.json | 3 ++- interfaces/rust/BUILD.gn | 48 ++++++++++++++++++++++++++++++++++++ interfaces/rust/src/lib.rs | 23 +++++++++++++++++ interfaces/rust/test/main.rs | 22 +++++++++++++++++ 4 files changed, 95 insertions(+), 1 deletion(-) create mode 100644 interfaces/rust/BUILD.gn create mode 100644 interfaces/rust/src/lib.rs create mode 100644 interfaces/rust/test/main.rs diff --git a/interfaces/bundle.json b/interfaces/bundle.json index f6b5cdf..915f4e5 100644 --- a/interfaces/bundle.json +++ b/interfaces/bundle.json @@ -45,7 +45,8 @@ ], "service_group": [ "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog_base", - "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog" + "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", + "//base/hiviewdfx/hilog/interfaces/rust:hello_world_rust" ] }, "inner_kits": [ diff --git a/interfaces/rust/BUILD.gn b/interfaces/rust/BUILD.gn new file mode 100644 index 0000000..47b4f6c --- /dev/null +++ b/interfaces/rust/BUILD.gn @@ -0,0 +1,48 @@ +# Copyright (c) 2022 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/ohos.gni") + +ohos_rust_executable("hello_world_rust") { + sources = [ "test/main.rs" ] + + deps = [ "//base/hiviewdfx/hilog/interfaces/rust:hilog_rust" ] + subsystem_name = "hiviewdfx" + part_name = "hilog_native" +} + +ohos_rust_shared_library("hilog_rust") { + sources = [ + "src/lib.rs", + ] + + deps = [ + "//base/hiviewdfx/hilog/interfaces/native/innerkits:libhilog", + "//build/rust:libstd.dylib.so" + ] + + crate_name = "hilog_rust" + crate_type = "dylib" + + install_images = [ system_base_dir ] + relative_install_dir = "chipset-pub-sdk" + subsystem_name = "hiviewdfx" + part_name = "hilog_native" +} + +group("rust_hilog_component") { + deps = [ + ":hilog_rust", + "//base/hiviewdfx/hilog/interfaces/rust:hello_world_rust", + ] +} diff --git a/interfaces/rust/src/lib.rs b/interfaces/rust/src/lib.rs new file mode 100644 index 0000000..78b4968 --- /dev/null +++ b/interfaces/rust/src/lib.rs @@ -0,0 +1,23 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//! dylib_crate example for Rust. +use std::ffi::{c_char}; + +#[link(name = "libhilog")] +extern "C"{ + ///test + pub fn HiLogIsLoggable(domain: u32, tag: *const c_char, level:u32) -> bool; +} \ No newline at end of file diff --git a/interfaces/rust/test/main.rs b/interfaces/rust/test/main.rs new file mode 100644 index 0000000..a833e45 --- /dev/null +++ b/interfaces/rust/test/main.rs @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +use crate::{hilog_rust}; + +//! Hello world example for Rust. + +fn main() { + let res = hilog_rust::HiLogIsLoggable(0xd003200, tag: *const c_char, 4); +} \ No newline at end of file -- Gitee From 3a0ac2715973a44dbc4ed81c39df211f8a1f808a Mon Sep 17 00:00:00 2001 From: buzhenwang Date: Tue, 3 Jan 2023 10:40:38 +0800 Subject: [PATCH 2/2] hilog rust test Signed-off-by: buzhenwang --- interfaces/rust/BUILD.gn | 2 +- interfaces/rust/src/lib.rs | 74 ++++++++++++++++++++++++++++++++++- interfaces/rust/src/macros.rs | 30 ++++++++++++++ interfaces/rust/test/main.rs | 20 ++++++++-- 4 files changed, 120 insertions(+), 6 deletions(-) create mode 100644 interfaces/rust/src/macros.rs diff --git a/interfaces/rust/BUILD.gn b/interfaces/rust/BUILD.gn index 47b4f6c..0cae12f 100644 --- a/interfaces/rust/BUILD.gn +++ b/interfaces/rust/BUILD.gn @@ -23,7 +23,7 @@ ohos_rust_executable("hello_world_rust") { ohos_rust_shared_library("hilog_rust") { sources = [ - "src/lib.rs", + "src/lib.rs" ] deps = [ diff --git a/interfaces/rust/src/lib.rs b/interfaces/rust/src/lib.rs index 78b4968..e10288f 100644 --- a/interfaces/rust/src/lib.rs +++ b/interfaces/rust/src/lib.rs @@ -16,8 +16,80 @@ //! dylib_crate example for Rust. use std::ffi::{c_char}; -#[link(name = "libhilog")] +/// log level +#[derive(Debug)] +pub enum LogLevel { + /// log level min + LogLevelMin = 0, + /// log level + LogDebug = 3, + /// log level + LogInfo = 4, + /// The "warn" level. + /// + /// Designates very serious errors. + LogWarn = 5, + /// The "error" level. + /// + /// Designates very serious errors. + LogError = 6, + /// log level + LogFatal = 7, + /// log level + LogLevelMax +} + +/// log type +#[derive(Debug)] +pub enum LogType { + /// log type for app log + LogApp = 0, + /// log type for init log + LogInit = 1, + /// log type for core log + LogCore = 3, + /// log type for kernel log + LogKmsg = 4, + /// log type max + LogTypeMax +} + +/// hilog label +#[derive(Debug)] +pub struct HiLogLabel<'a> { + /// log type + log_type: LogType, + /// log type + domain: u32, + /// log type + tag: &'a str, +} + +/// log macros + +#[macro_export] +macro_rules! hilog{ + ($args:ident, $level:expr, $($arg:tt)* ) => ( + let log_str = format!($($arg)*); + let res = unsafe { + $crate::HiLogPrint(args.log_type as u8, level as u8, args.domain as u32, args.tag.as_ptr() as *const c_char, log_str.as_ptr() as *const c_char) + }; + println!("{}", res); + ) +} + +/// log macros + +#[macro_export] +macro_rules! debug{ + ($args:ident, $($arg:tt)*) => (hilog!(target: $target, LogLevel::LogDebug, $crate::Level::Warn, $($arg)+)); +} + + +#[link(name = "hilog")] extern "C"{ ///test pub fn HiLogIsLoggable(domain: u32, tag: *const c_char, level:u32) -> bool; + ///test + pub fn HiLogPrint(logType: u8, level: u8, domain: u32, tag: *const c_char, fmt: *const c_char, ...) -> u32; } \ No newline at end of file diff --git a/interfaces/rust/src/macros.rs b/interfaces/rust/src/macros.rs new file mode 100644 index 0000000..28f48fe --- /dev/null +++ b/interfaces/rust/src/macros.rs @@ -0,0 +1,30 @@ +/* + * Copyright (C) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +//! dylib_crate example for Rust. +use std::ffi::{c_char}; + +/// log macros + +#[macro_export] +macro_rules! hilog{ + ($args:ident, $($arg:tt)* ) => ( + let log_str = format($($arg)*); + let res = unsafe { + $crate::HiLogPrint(args.log_type as u8, args.domain as u32, args.tag.as_ptr() as *const c_char, log_str.as_ptr() as *const c_char) + }; + println!("{}", res); + ) +} \ No newline at end of file diff --git a/interfaces/rust/test/main.rs b/interfaces/rust/test/main.rs index a833e45..589e8e1 100644 --- a/interfaces/rust/test/main.rs +++ b/interfaces/rust/test/main.rs @@ -13,10 +13,22 @@ * limitations under the License. */ -use crate::{hilog_rust}; - -//! Hello world example for Rust. + //! dylib_crate example for Rust. + +use std::ffi::{c_char, CString}; +extern crate hilog_rust; +/// Hello world example for Rust. fn main() { - let res = hilog_rust::HiLogIsLoggable(0xd003200, tag: *const c_char, 4); + let mut log_label = hilog_rust::HiLogLabel { + log_type: hilog_rust::LogType::LogCore, + domain: 0xd003200, + tag: "testTag" + }; + let c_to_print = CString::new("Hello, world!").unwrap(); + unsafe { + // hilog_rust::HiLogPrint(hilog_rust::LogType::LogCore as u8, hilog_rust::LogLevel::LogError as u8, 0xd002d00, + // "testTag".as_ptr() as *const c_char, c_to_print.as_ptr() as *const c_char); + hilog_rust::hilog!(log_label, hilog_rust::LogLevel::LogError, "hahha{}", "testlog"); + } } \ No newline at end of file -- Gitee