From 32471a685113788c436c023e1f73f1ee73641634 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=BE=90=E6=9C=AA=E6=9D=A5?= Date: Mon, 18 Sep 2023 11:18:44 +0800 Subject: [PATCH] =?UTF-8?q?log=E6=97=A5=E5=BF=97=E6=A8=A1=E5=9D=97?= =?UTF-8?q?=E5=BC=80=E5=8F=91?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: 徐未来 --- bundle.json | 3 +- test/BUILD.gn | 3 + ylong_io/BUILD.gn | 2 + ylong_io/Cargo.toml | 2 + ylong_io/src/lib.rs | 3 + ylong_io/src/log.rs | 101 ++++++++++++++++++ ylong_io/src/sys/unix/epoll.rs | 19 +++- ylong_runtime/BUILD.gn | 2 + ylong_runtime/Cargo.toml | 3 + ylong_runtime/src/builder/common_builder.rs | 14 +++ .../src/builder/current_thread_builder.rs | 1 + .../src/builder/multi_thread_builder.rs | 11 +- ylong_runtime/src/executor/mod.rs | 12 ++- ylong_runtime/src/lib.rs | 3 + ylong_runtime/src/log.rs | 101 ++++++++++++++++++ ylong_runtime/src/net/driver.rs | 10 +- ylong_runtime/src/process/command.rs | 6 +- .../src/process/pty_process/command.rs | 18 +++- 18 files changed, 302 insertions(+), 12 deletions(-) create mode 100644 ylong_io/src/log.rs create mode 100644 ylong_runtime/src/log.rs diff --git a/bundle.json b/bundle.json index e1cad2c..b606b4f 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 1d96d52..cce6475 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 0cab989..ca0e834 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 cab5af6..e0e3342 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 690d446..59126b1 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 0000000..5c1f9b2 --- /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 a496213..886e262 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 a96ead4..09d07a4 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 0514ed2..41c2b4f 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 bf46efd..8a26c59 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 f8f9a2e..5a8f152 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 397ab68..44bcf68 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 65cf0ba..e2e096e 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 528ae86..ffea860 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 0000000..b7cb182 --- /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 4472f3d..234b54c 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 64de422..d317a81 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 29d5e7a..6ec9451 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. -- Gitee