Skip to main content

hilog_sys/
lib.rs

1//! hilog-sys
2//!
3//! Rust bindings for the `HiLog` logging framework of OpenHarmony.
4//! This crate should only be used on OpenHarmony (`target_env = "ohos"`).
5//! More information on hilog in native applications is available in the [hilog native guidelines].
6//! You can use the [hdc] tools [hilog command-line interface] to query the saved logs.
7//!
8//! ## Safety
9//!
10//! When using `OH_LOG_Print` from Rust you **must** ensure that the `fmt` parameter either
11//! - Does not contain any `printf` style format specifiers (like `%s`, `%d`) OR
12//! - `fmt` is `"${public}s\0"` and the actual string is passed as the following parameter.
13//!
14//! ## Crate Features
15//!
16//! * `log`: When the log feature is enabled, a `From<log::Level>` implementation is added
17//!          to easily convert from `log`s log level to HiLogs log level.
18//!
19//! [hilog native guidelines]: https://docs.openharmony.cn/pages/v5.0/en/application-dev/dfx/hilog-guidelines-ndk.md
20//! [hilog cli tool]: https://docs.openharmony.cn/pages/v5.0/en/application-dev/dfx/hilog.md
21//! [hdc]: https://docs.openharmony.cn/pages/v5.0/en/application-dev/dfx/hdc.md
22//!
23//! ## Feature flags
24#![cfg_attr(
25    feature = "document-features",
26    cfg_attr(doc, doc = ::document_features::document_features!())
27)]
28#![cfg_attr(docsrs, feature(doc_cfg))]
29
30#[link(name = "hilog_ndk.z")]
31extern "C" {}
32
33#[allow(non_snake_case)]
34mod hilog_ffi;
35
36pub use hilog_ffi::*;
37
38#[cfg(feature = "log")]
39impl From<log::Level> for LogLevel {
40    fn from(level: log::Level) -> Self {
41        // Note: log:::Level::Trace has no corresponding HiLog level.
42        match level {
43            log::Level::Error => LogLevel::LOG_ERROR,
44            log::Level::Warn => LogLevel::LOG_WARN,
45            log::Level::Info => LogLevel::LOG_INFO,
46            log::Level::Debug => LogLevel::LOG_DEBUG,
47            log::Level::Trace => LogLevel::LOG_DEBUG,
48        }
49    }
50}