diff --git a/bundle.json b/bundle.json index e1cad2cf76530ca46c7a74a2e9897ee33b2d4aad..b606b4f2c1f3ad8fc21419eb6d3c7125ef5c9aef 100644 --- a/bundle.json +++ b/bundle.json @@ -26,7 +26,8 @@ "ram": "~200KB", "deps": { "components": [ - "ffrt" + "ffrt", + "hilog" ], "third_party": [] }, diff --git a/test/BUILD.gn b/test/BUILD.gn index 1d96d52b985164c21afe245e66cfb8dadb3676f1..cce647557f44e14a919bdfef0815ccac46a824e2 100644 --- a/test/BUILD.gn +++ b/test/BUILD.gn @@ -24,8 +24,10 @@ ohos_rust_unittest("rust_ylong_runtime_test_ut") { "--cfg=feature=\"sync\"", "--cfg=feature=\"signal\"", "--cfg=feature=\"time\"", + "--cfg=feature=\"hilog\"", ] + external_deps = [ "hilog:hilog_rust" ] sources = [ "../ylong_runtime/src/lib.rs" ] deps = [ "../ylong_io:ylong_io", @@ -47,6 +49,7 @@ ohos_rust_systemtest("rust_ylong_runtime_test_sdv") { "--cfg=feature=\"sync\"", "--cfg=feature=\"signal\"", "--cfg=feature=\"time\"", + "--cfg=feature=\"hilog\"", ] sources = [ "../ylong_runtime/tests/entry.rs" ] diff --git a/ylong_io/BUILD.gn b/ylong_io/BUILD.gn index 0cab989b44e5f4c18d4775e14af395dff1ef4010..ca0e83422f1b3b26760f4031c23ee5493acb0fb1 100644 --- a/ylong_io/BUILD.gn +++ b/ylong_io/BUILD.gn @@ -23,8 +23,10 @@ ohos_rust_static_library("ylong_io") { features = [ "tcp", "udp", + "hilog", ] + external_deps = [ "hilog:hilog_rust" ] sources = [ "src/lib.rs" ] deps = [ "//third_party/rust/crates/libc:lib" ] } diff --git a/ylong_io/Cargo.toml b/ylong_io/Cargo.toml index cab5af6812351ffee102780a4501395eb82f15f9..e0e33420b04c0e9c16e2d6cc14175174a8af7b72 100644 --- a/ylong_io/Cargo.toml +++ b/ylong_io/Cargo.toml @@ -11,6 +11,8 @@ keywords = ["ylong", "io", "epoll"] default = ["tcp", "udp"] udp = [] tcp = [] +# This feature only available in OH +hilog = [] [dependencies] libc = "0.2.134" diff --git a/ylong_io/src/lib.rs b/ylong_io/src/lib.rs index 690d446be9c446ffb75bb69844be90e34772daf4..59126b157f301fb27beff66faf17267d66b03db9 100644 --- a/ylong_io/src/lib.rs +++ b/ylong_io/src/lib.rs @@ -13,6 +13,9 @@ //! Event-driven nonblocking net-io components. +#[macro_use] +mod log; + mod poll; pub use poll::Poll; diff --git a/ylong_io/src/log.rs b/ylong_io/src/log.rs new file mode 100644 index 0000000000000000000000000000000000000000..5c1f9b2cbe6b3a145d0d5bee3c72e4efb98afbb6 --- /dev/null +++ b/ylong_io/src/log.rs @@ -0,0 +1,101 @@ +// Copyright (c) 2023 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. + +#![allow(unused_macros)] + +#[cfg(feature = "hilog")] +extern crate hilog_rust; + +#[cfg(feature = "hilog")] +pub(crate) const LOG_LABEL: hilog_rust::HiLogLabel = hilog_rust::HiLogLabel { + log_type: hilog_rust::LogType::LogCore, + domain: 0xD003F02, + tag: "ylong_io" +}; + +/// Fatal +/// Only hilog has this level. +/// In the no hilog environment, this level is same as Error. +macro_rules! fatal { + ($($arg:tt)+) => ( + #[cfg(feature = "hilog")] + { + use hilog_rust::hilog; + use std::ffi::{c_char, CString}; + use $crate::log::LOG_LABEL; + + let msg = format!($($arg)+); + hilog_rust::fatal!(LOG_LABEL, "{}", @public(msg)); + } + ) +} + +/// Error +macro_rules! error { + ($($arg:tt)+) => ( + #[cfg(feature = "hilog")] + { + use hilog_rust::hilog; + use std::ffi::{c_char, CString}; + use $crate::log::LOG_LABEL; + + let msg = format!($($arg)+); + hilog_rust::error!(LOG_LABEL, "{}", @public(msg)); + } + ) +} + +/// Warn +macro_rules! warn { + ($($arg:tt)+) => ( + #[cfg(feature = "hilog")] + { + use hilog_rust::hilog; + use std::ffi::{c_char, CString}; + use $crate::log::LOG_LABEL; + + let msg = format!($($arg)+); + hilog_rust::warn!(LOG_LABEL, "{}", @public(msg)); + } + ) +} + +/// Info +macro_rules! info { + ($($arg:tt)+) => ( + #[cfg(feature = "hilog")] + { + use hilog_rust::hilog; + use std::ffi::{c_char, CString}; + use $crate::log::LOG_LABEL; + + let msg = format!($($arg)+); + hilog_rust::info!(LOG_LABEL, "{}", @public(msg)); + } + ) +} + +/// Debug +macro_rules! debug { + ($($arg:tt)+) => ( + #[cfg(feature = "hilog")] + { + use hilog_rust::hilog; + use std::ffi::{c_char, CString}; + use $crate::log::LOG_LABEL; + + let msg = format!($($arg)+); + hilog_rust::debug!(LOG_LABEL, "{}", @public(msg)); + } + ) +} diff --git a/ylong_io/src/sys/unix/epoll.rs b/ylong_io/src/sys/unix/epoll.rs index a49621355cb0e14a6d5de00c595cd119a3a31c28..886e26201fe6bfa3e58ab3748ab3ffcffe842cc3 100644 --- a/ylong_io/src/sys/unix/epoll.rs +++ b/ylong_io/src/sys/unix/epoll.rs @@ -40,6 +40,7 @@ impl Selector { let ep = match syscall!(epoll_create1(libc::EPOLL_CLOEXEC)) { Ok(ep_sys) => ep_sys, Err(err) => { + error!("libc::epoll_create1 system call failed: {:?}.", err); return Err(err); } }; @@ -66,6 +67,7 @@ impl Selector { )) { Ok(n_events) => unsafe { events.set_len(n_events as usize) }, Err(err) => { + error!("libc::epoll_wait system call failed: {:?}.", err); return Err(err); } } @@ -81,7 +83,10 @@ impl Selector { match syscall!(epoll_ctl(self.ep, libc::EPOLL_CTL_ADD, fd, &mut sys_event)) { Ok(_) => Ok(()), - Err(err) => Err(err), + Err(err) => { + error!("register libc::epoll_ctl system call failed: {:?}.", err); + Err(err) + }, } } @@ -94,7 +99,10 @@ impl Selector { match syscall!(epoll_ctl(self.ep, libc::EPOLL_CTL_MOD, fd, &mut sys_event)) { Ok(_) => Ok(()), - Err(err) => Err(err), + Err(err) => { + error!("reregister libc::epoll_ctl system call failed: {:?}.", err); + Err(err) + }, } } @@ -107,7 +115,10 @@ impl Selector { std::ptr::null_mut() )) { Ok(_) => Ok(()), - Err(err) => Err(err), + Err(err) => { + error!("deregister libc::epoll_ctl system call failed: {:?}.", err); + Err(err) + }, } } } @@ -115,7 +126,7 @@ impl Selector { impl Drop for Selector { fn drop(&mut self) { if let Err(_err) = syscall!(close(self.ep)) { - // todo: log the error + error!("syscall: libc::close system call failed: {}.", _err); } } } diff --git a/ylong_runtime/BUILD.gn b/ylong_runtime/BUILD.gn index a96ead4403719d5c6abb8b1cfca6faddbcab642f..09d07a44772613d6040fcb735dccfd81a2ad08cc 100644 --- a/ylong_runtime/BUILD.gn +++ b/ylong_runtime/BUILD.gn @@ -28,8 +28,10 @@ ohos_rust_shared_library("ylong_runtime") { "signal", "sync", "time", + "hilog", ] + external_deps = [ "hilog:hilog_rust" ] sources = [ "src/lib.rs" ] deps = [ "../ylong_io:ylong_io", diff --git a/ylong_runtime/Cargo.toml b/ylong_runtime/Cargo.toml index 0514ed2bf99142747b7c85246305e536e53ffb72..41c2b4f8618a82a58e3f1416b1d3369ca5235faf 100644 --- a/ylong_runtime/Cargo.toml +++ b/ylong_runtime/Cargo.toml @@ -69,6 +69,9 @@ metrics = [] # Process component process = ["signal"] +# This feature only available in OH +hilog = [] + [dependencies] libc = "0.2.134" ylong_signal = { path = "../ylong_signal", optional = true } diff --git a/ylong_runtime/src/builder/common_builder.rs b/ylong_runtime/src/builder/common_builder.rs index bf46efdcd67c018533274c3e3393f93c1dfe08da..8a26c59acb937e34dd3c6f34aa8924434298a3bd 100644 --- a/ylong_runtime/src/builder/common_builder.rs +++ b/ylong_runtime/src/builder/common_builder.rs @@ -61,6 +61,20 @@ pub(crate) struct CommonBuilder { pub(crate) before_stop: Option, } +impl std::fmt::Debug for CommonBuilder { + fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { + f.debug_struct("CommonBuilder") + .field("worker_name",&self.worker_name) + .field("is_affinity",&self.is_affinity) + .field("keep_alive_time",&self.keep_alive_time) + .field("max_blocking_pool_size",&self.max_blocking_pool_size) + .field("schedule_algo",&self.schedule_algo) + .field("blocking_permanent_thread_num",&self.blocking_permanent_thread_num) + .field("stack_size",&self.stack_size) + .finish() + } +} + #[cfg(feature = "ffrt")] impl CommonBuilder { pub(crate) fn new() -> Self { diff --git a/ylong_runtime/src/builder/current_thread_builder.rs b/ylong_runtime/src/builder/current_thread_builder.rs index f8f9a2e9bc004df9489826d23037093fdb5d3c93..5a8f15229ac7f35128b0fc910ee885fd98542c44 100644 --- a/ylong_runtime/src/builder/current_thread_builder.rs +++ b/ylong_runtime/src/builder/current_thread_builder.rs @@ -33,6 +33,7 @@ impl CurrentThreadBuilder { /// Initializes the runtime and returns its instance. pub fn build(&mut self) -> io::Result { let async_spawner = CurrentThreadSpawner::new(); + info!("CurrentThread Runtime init success!"); Ok(Runtime { async_spawner: AsyncHandle::CurrentThread(async_spawner), }) diff --git a/ylong_runtime/src/builder/multi_thread_builder.rs b/ylong_runtime/src/builder/multi_thread_builder.rs index 397ab68b64cbc0390aa78a068c5d1fdd4db9a914..44bcf687d77fe1aabd99c09476db47a4c0eb8c67 100644 --- a/ylong_runtime/src/builder/multi_thread_builder.rs +++ b/ylong_runtime/src/builder/multi_thread_builder.rs @@ -32,6 +32,7 @@ pub(crate) static GLOBAL_BUILDER: Mutex> = Mutex::new /// Runtime builder that configures a multi-threaded runtime, or the global /// runtime. +#[derive(Debug)] pub struct MultiThreadBuilder { pub(crate) common: CommonBuilder, @@ -63,6 +64,7 @@ impl MultiThreadBuilder { pub fn build_global(self) -> io::Result<()> { let mut builder = GLOBAL_BUILDER.lock().unwrap(); if builder.is_some() { + error!("GLOBAL_BUILDER has been set as {:?}, can't call build_global again!", builder); return Err(io::ErrorKind::AlreadyExists.into()); } @@ -77,6 +79,7 @@ impl MultiThreadBuilder { } } + info!("GLOBAL_BUILDER set as {:?}", self); *builder = Some(self); Ok(()) } @@ -226,7 +229,13 @@ impl MultiThreadBuilder { #[cfg(feature = "multi_instance_runtime")] pub fn build(&mut self) -> io::Result { use crate::builder::initialize_async_spawner; - let async_spawner = initialize_async_spawner(self)?; + #[allow(clippy::map_identity)] + let async_spawner = initialize_async_spawner(self).map_err(|e| { + error!("MultiThreadBuilder build initialize_async_spawner failed: {e:?}"); + e + })?; + + info!("MultiThread Runtime init success with builder: {:?}", self); Ok(Runtime { async_spawner: AsyncHandle::MultiThread(async_spawner), diff --git a/ylong_runtime/src/executor/mod.rs b/ylong_runtime/src/executor/mod.rs index 65cf0bac112bbf90e6ba56b70470a5671618965d..e2e096e2549e390b7e6e84d1f85520a7bbbe61fc 100644 --- a/ylong_runtime/src/executor/mod.rs +++ b/ylong_runtime/src/executor/mod.rs @@ -113,13 +113,17 @@ pub(crate) fn global_default_async() -> &'static Runtime { Ok(s) => Runtime { async_spawner: AsyncHandle::MultiThread(s), }, - Err(e) => panic!("initialize runtime failed: {e:?}"), + Err(e) => { + error!("Global Async Runtime init failed: {e:?}"); + panic!("initialize runtime failed: {e:?}"); + }, }; #[cfg(feature = "ffrt")] let runtime = Runtime { async_spawner: AsyncHandle::FfrtMultiThread, }; GLOBAL_DEFAULT_ASYNC = MaybeUninit::new(runtime); + info!("Global Async Runtime init success with builder: {:?}", global_builder); }); &*GLOBAL_DEFAULT_ASYNC.as_ptr() } @@ -141,8 +145,12 @@ pub(crate) fn global_default_blocking() -> &'static BlockPoolSpawner { // is safe match initialize_blocking_spawner(&global_builder.as_ref().unwrap_unchecked().common) { Ok(bps) => GLOBAL_DEFAULT_BLOCKING = MaybeUninit::new(bps), - Err(e) => panic!("initialize blocking pool failed: {e:?}"), + Err(e) => { + error!("Global Blocking Spawner init failed: {e:?}"); + panic!("initialize blocking pool failed: {e:?}") + }, } + info!("Global Blocking Spawner init success with builder: {:?}", global_builder); }); &*GLOBAL_DEFAULT_BLOCKING.as_ptr() } diff --git a/ylong_runtime/src/lib.rs b/ylong_runtime/src/lib.rs index 528ae8621740ed95657fedab3c9ebc9b2d9e447f..ffea86004331a9192c22d58c0fc6c3351cc1aabd 100644 --- a/ylong_runtime/src/lib.rs +++ b/ylong_runtime/src/lib.rs @@ -33,6 +33,9 @@ extern crate core; #[macro_use] mod macros; +#[macro_use] +mod log; + pub mod builder; pub mod error; pub mod executor; diff --git a/ylong_runtime/src/log.rs b/ylong_runtime/src/log.rs new file mode 100644 index 0000000000000000000000000000000000000000..b7cb182e63fbeb216fcf56e05ab32e963f207b97 --- /dev/null +++ b/ylong_runtime/src/log.rs @@ -0,0 +1,101 @@ +// Copyright (c) 2023 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. + +#![allow(unused_macros)] + +#[cfg(feature = "hilog")] +extern crate hilog_rust; + +#[cfg(feature = "hilog")] +pub(crate) const LOG_LABEL: hilog_rust::HiLogLabel = hilog_rust::HiLogLabel { + log_type: hilog_rust::LogType::LogCore, + domain: 0xD003F02, + tag: "ylong_runtime" +}; + +/// Fatal +/// Only hilog has this level. +/// In the no hilog environment, this level is same as Error. +macro_rules! fatal { + ($($arg:tt)+) => ( + #[cfg(feature = "hilog")] + { + use hilog_rust::hilog; + use std::ffi::{c_char, CString}; + use $crate::log::LOG_LABEL; + + let msg = format!($($arg)+); + hilog_rust::fatal!(LOG_LABEL, "{}", @public(msg)); + } + ) +} + +/// Error +macro_rules! error { + ($($arg:tt)+) => ( + #[cfg(feature = "hilog")] + { + use hilog_rust::hilog; + use std::ffi::{c_char, CString}; + use $crate::log::LOG_LABEL; + + let msg = format!($($arg)+); + hilog_rust::error!(LOG_LABEL, "{}", @public(msg)); + } + ) +} + +/// Warn +macro_rules! warn { + ($($arg:tt)+) => ( + #[cfg(feature = "hilog")] + { + use hilog_rust::hilog; + use std::ffi::{c_char, CString}; + use $crate::log::LOG_LABEL; + + let msg = format!($($arg)+); + hilog_rust::warn!(LOG_LABEL, "{}", @public(msg)); + } + ) +} + +/// Info +macro_rules! info { + ($($arg:tt)+) => ( + #[cfg(feature = "hilog")] + { + use hilog_rust::hilog; + use std::ffi::{c_char, CString}; + use $crate::log::LOG_LABEL; + + let msg = format!($($arg)+); + hilog_rust::info!(LOG_LABEL, "{}", @public(msg)); + } + ) +} + +/// Debug +macro_rules! debug { + ($($arg:tt)+) => ( + #[cfg(feature = "hilog")] + { + use hilog_rust::hilog; + use std::ffi::{c_char, CString}; + use $crate::log::LOG_LABEL; + + let msg = format!($($arg)+); + hilog_rust::debug!(LOG_LABEL, "{}", @public(msg)); + } + ) +} diff --git a/ylong_runtime/src/net/driver.rs b/ylong_runtime/src/net/driver.rs index 4472f3dd20c025b29c073a92b95012e7d796b3fc..234b54c0faf66fb60df6adf032024d3f74aa2874 100644 --- a/ylong_runtime/src/net/driver.rs +++ b/ylong_runtime/src/net/driver.rs @@ -195,9 +195,15 @@ impl IoDriver { impl IoDriver { pub(crate) fn initialize() -> (IoHandle, IoDriver) { let poll = - Poll::new().unwrap_or_else(|e| panic!("IO poller initialize failed, error: {e}")); + Poll::new().unwrap_or_else(|e| { + error!("Poll new failed: e"); + panic!("IO poller initialize failed, error: {e}") + }); let waker = ylong_io::Waker::new(&poll, WAKE_TOKEN) - .unwrap_or_else(|e| panic!("ylong_io waker construction failed, error: {e}")); + .unwrap_or_else(|e| { + error!("Waker new failed: e"); + panic!("ylong_io waker construction failed, error: {e}") + }); let arc_poll = Arc::new(poll); let events = Events::with_capacity(EVENTS_MAX_CAPACITY); let slab = Slab::new(); diff --git a/ylong_runtime/src/process/command.rs b/ylong_runtime/src/process/command.rs index 64de42223fbdd692201fdbd85dd686eb23cd54cc..d317a819ade76f435dff2956ae27e9aca41b5927 100644 --- a/ylong_runtime/src/process/command.rs +++ b/ylong_runtime/src/process/command.rs @@ -330,7 +330,11 @@ impl Command { /// } /// ``` pub fn spawn(&mut self) -> io::Result { - let mut child = self.std.spawn()?; + #[allow(clippy::map_identity)] + let mut child = self.std.spawn().map_err(|e| { + error!("Command spawn fail: {:?}. Command: {:?}", e, self); + e + })?; let stdin = child .stdin .take() diff --git a/ylong_runtime/src/process/pty_process/command.rs b/ylong_runtime/src/process/pty_process/command.rs index 29d5e7a29a5d87d03001168f3c3ef7bb4910779c..6ec945192312cf9c0201be5c527f6fc057bf7b9d 100644 --- a/ylong_runtime/src/process/pty_process/command.rs +++ b/ylong_runtime/src/process/pty_process/command.rs @@ -12,6 +12,7 @@ // limitations under the License. use std::ffi::OsStr; +use std::fmt::Formatter; use std::io; use std::path::Path; use std::process::{CommandArgs, CommandEnvs, Stdio}; @@ -28,6 +29,17 @@ pub struct PtyCommand { f: Option io::Result<()> + Send + Sync + 'static>>, } +impl std::fmt::Debug for PtyCommand { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + f.debug_struct("PtyCommand") + .field("command",&self.command) + .field("stdin",&self.stdin) + .field("stdout",&self.stdout) + .field("stderr",&self.stderr) + .finish() + } +} + impl PtyCommand { /// Constructs a new Command for launching the program at path program, with /// the following default configuration: @@ -310,7 +322,11 @@ impl PtyCommand { } } - self.command.spawn() + #[allow(clippy::map_identity)] + self.command.spawn().map_err(|e| { + error!("PtyCommand spawn fail: {:?}. PtyCommand: {:?}", e, self); + e + }) } /// Returns the path to the program that was given to Command::new.