sysmaster/backport-fix-add-two-helper-macroes-to-help-us-log-message-ou.patch
huyubiao 8936fa02c5 sync patches from upstream,change the path of the unit,modify permissions for some directories and files
(cherry picked from commit ce9ff469b57f60130621bc293783bd3ac1fc92f2)
2023-08-05 18:15:53 +08:00

50 lines
1.5 KiB
Diff

From e9ce7c44f15a6e03df5d5c4837f8b4d86841cd1f Mon Sep 17 00:00:00 2001
From: licunlong <licunlong1@huawei.com>
Date: Wed, 5 Jul 2023 16:42:08 +0800
Subject: [PATCH] fix: add two helper macroes to help us log message out when
we operate a file or directory easily
---
libs/basic/src/fs_util.rs | 25 +++++++++++++++++++++++++
1 file changed, 25 insertions(+)
diff --git a/libs/basic/src/fs_util.rs b/libs/basic/src/fs_util.rs
index ec814e2..3aa93e9 100644
--- a/libs/basic/src/fs_util.rs
+++ b/libs/basic/src/fs_util.rs
@@ -310,6 +310,31 @@ pub fn touch_file(
fchmod_and_chown(fd, path, mode, uid, gid)
}
+/// do some operations and log message out, returns Io error when fail
+#[macro_export]
+macro_rules! do_entry_or_return_io_error {
+ ($function:expr, $entry:ident, $action:literal) => {
+ match $function(&$entry) {
+ Err(e) => {
+ log::error!("Failed to {} {:?}: {e}", $action, $entry);
+ return Err(e).context(IoSnafu);
+ }
+ Ok(_) => log::debug!("{} {:?} succeeded", $action, $entry),
+ }
+ };
+}
+
+/// do some operations and log message out, skip the error
+#[macro_export]
+macro_rules! do_entry_log {
+ ($function:expr, $entry:ident, $action:literal) => {
+ match $function(&$entry) {
+ Err(e) => log::error!("Failed to {} {:?}: {e}", $action, $entry),
+ Ok(_) => log::debug!("{} {:?} succeeded", $action, $entry),
+ }
+ };
+}
+
#[cfg(test)]
mod tests {
use super::*;
--
2.33.0