diff --git a/interfaces/bundle.json b/interfaces/bundle.json index f6b5cdf4d839d335c573ff48b6d3ac247502a889..b7f02cc641bdb218571a9d1079c97f7667fbb0f1 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:hilog_rust" ] }, "inner_kits": [ diff --git a/interfaces/rust/BUILD.gn b/interfaces/rust/BUILD.gn new file mode 100644 index 0000000000000000000000000000000000000000..d283d5ce671c8b94f650e1994465f96e8fa63474 --- /dev/null +++ b/interfaces/rust/BUILD.gn @@ -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. + +import("//build/ohos.gni") + +ohos_rust_shared_library("hilog_rust") { + sources = [ "src/lib.rs" ] + + external_deps = [ "hilog_native:libhilog" ] + + crate_name = "hilog_rust" + crate_type = "dylib" + + subsystem_name = "hiviewdfx" + part_name = "hilog_native" +} + +group("rust_hilog_component") { + deps = [ ":hilog_rust" ] +} diff --git a/interfaces/rust/src/lib.rs b/interfaces/rust/src/lib.rs new file mode 100644 index 0000000000000000000000000000000000000000..9603e1964a61115797c398322fb4cb1b68adb7a4 --- /dev/null +++ b/interfaces/rust/src/lib.rs @@ -0,0 +1,84 @@ +/* + * 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}; + +#[macro_use] +mod macros; + +/// log level +#[derive(Debug)] +pub enum LogLevel { + /// log level min + LogLevelMin = 0, + /// The "debug" level. + /// + /// Designates lower priority log. + Debug = 3, + /// The "info" level. + /// + /// Designates useful information. + Info = 4, + /// The "warn" level. + /// + /// Designates hazardous situations. + Warn = 5, + /// The "error" level. + /// + /// Designates very serious errors. + Error = 6, + /// The "fatal" level. + /// + /// Designates major fatal anomaly. + Fatal = 7, + /// max 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, + /// max log type + LogTypeMax +} + +/// hilog label +#[derive(Debug)] +pub struct HiLogLabel { + /// log type + pub log_type: LogType, + /// log domain + pub domain: u32, + /// log tag + pub tag: &'static str, +} + + +#[link(name = "hilog")] +extern "C"{ + /// hilog ffi interface HiLogIsLoggabel + pub fn HiLogIsLoggable(domain: u32, tag: *const c_char, level:u32) -> bool; + /// hilog ffi interface HiLogPrint + 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..ca00c0e2f13365e70537e010bf769abcf014483c --- /dev/null +++ b/interfaces/rust/src/macros.rs @@ -0,0 +1,146 @@ +/* + * 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. + +/// hilog macros + +#[macro_export] +macro_rules! hilog{ + ($log_label:ident, $level:expr, $($arg:tt)* ) => ( + let log_str = format!($($arg)*); + let res = unsafe { + $crate::HiLogPrint($log_label.log_type as u8, $level as u8, $log_label.domain as u32, + CString::new($log_label.tag).expect("default tag").as_ptr() as *const c_char, + CString::new(log_str).expect("default log").as_ptr() as *const c_char) + }; + res + ) +} + +/// printf log at the debug level. +/// +/// #Examples +/// +/// ``` +/// use hilog_rust::{debug, hilog, HiLogLabel, LogType}; +/// +/// # fn main() { +/// let log_label: HiLogLabel = HiLogLabel { +/// log_type: LogType::LogCore, +/// domain: 0xd003200, +/// tag: "testTag" +/// }; +/// debug!(LOG_LABEL,"testLog{}", "testargs"); +/// # } +/// ``` +#[macro_export] +macro_rules! debug{ + ($log_label:ident, $($arg:tt)*) => ( + hilog!(log_label: $log_label, $crate::LogLevel::Debug, $($arg)*) + ); +} + +/// printf log at the info level. +/// +/// #Examples +/// +/// ``` +/// use hilog_rust::{info, hilog, HiLogLabel, LogType}; +/// +/// # fn main() { +/// let log_label: HiLogLabel = HiLogLabel { +/// log_type: LogType::LogCore, +/// domain: 0xd003200, +/// tag: "testTag" +/// }; +/// info!(LOG_LABEL,"testLog{}", "testargs"); +/// # } +/// ``` +#[macro_export] +macro_rules! info{ + ($log_label:ident, $($arg:tt)*) => ( + hilog!($log_label, $crate::LogLevel::Info, $($arg)*) + ); +} + +/// printf log at the warn level. +/// +/// #Examples +/// +/// ``` +/// use hilog_rust::{warn, hilog, HiLogLabel, LogType}; +/// +/// # fn main() { +/// let log_label: HiLogLabel = HiLogLabel { +/// log_type: LogType::LogCore, +/// domain: 0xd003200, +/// tag: "testTag" +/// }; +/// warn!(LOG_LABEL,"testLog{}", "testargs"); +/// # } +/// ``` +#[macro_export] +macro_rules! warn{ + ($log_label:ident, $($arg:tt)*) => ( + hilog!($log_label, $crate::LogLevel::Warn, $($arg)*) + ); +} + +/// printf log at the error level. +/// +/// #Examples +/// +/// ``` +/// use hilog_rust::{error, hilog, HiLogLabel, LogType}; +/// +/// # fn main() { +/// let log_label: HiLogLabel = HiLogLabel { +/// log_type: LogType::LogCore, +/// domain: 0xd003200, +/// tag: "testTag" +/// }; +/// error!(LOG_LABEL,"testLog{}", "testargs"); +/// # } +/// ``` +#[macro_export] +macro_rules! error{ + ($log_label:ident, $($arg:tt)*) => ( + hilog!($log_label, $crate::LogLevel::Error, $($arg)*) + ); +} + +/// printf log at the fatal level. +/// +/// #Examples +/// +/// ``` +/// use hilog_rust::{fatal, hilog, HiLogLabel, LogType}; +/// +/// # fn main() { +/// let log_label: HiLogLabel = HiLogLabel { +/// log_type: LogType::LogCore, +/// domain: 0xd003200, +/// tag: "testTag" +/// }; +/// fatal!(LOG_LABEL,"testLog{}", "testargs"); +/// # } +/// ``` +#[macro_export] +macro_rules! fatal{ + ($log_label:ident, $($arg:tt)*) => ( + hilog!($log_label, $crate::LogLevel::Fatal, $($arg)*) + ); +} \ No newline at end of file