diff --git a/interfaces/bundle.json b/interfaces/bundle.json index f6b5cdf4d839d335c573ff48b6d3ac247502a889..915f4e5d95b4244b9e3ef3320e0c1f1a653bca18 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 0000000000000000000000000000000000000000..0cae12f756628082a4154b6bf15dec6bcdfdb360 --- /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 0000000000000000000000000000000000000000..e10288fb4c84d7bb83e253b67c826289b5b6503f --- /dev/null +++ b/interfaces/rust/src/lib.rs @@ -0,0 +1,95 @@ +/* + * 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 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 0000000000000000000000000000000000000000..28f48fe17d0a10edf30accf97cb2bedb864cf6b2 --- /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 new file mode 100644 index 0000000000000000000000000000000000000000..589e8e18b86d4a30965c9e2c7c12623b998d0cda --- /dev/null +++ b/interfaces/rust/test/main.rs @@ -0,0 +1,34 @@ +/* + * 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, CString}; +extern crate hilog_rust; + +/// Hello world example for Rust. +fn main() { + 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