瀏覽代碼

Complete the API in LocalLogger.

Added static delegators so that nothing else needs to be imported.
Jing Yang 4 年之前
父節點
當前提交
614c678683
共有 2 個文件被更改,包括 19 次插入3 次删除
  1. 1 1
      test_utils/src/logging.rs
  2. 18 2
      test_utils/src/thread_local_logger.rs

+ 1 - 1
test_utils/src/logging.rs

@@ -6,7 +6,7 @@ use rand::Rng;
 #[macro_export]
 macro_rules! init_test_log {
     () => {
-        $crate::init_log(module_path!()).unwrap()
+        $crate::init_log(stdext::function_name!()).unwrap()
     };
 }
 

+ 18 - 2
test_utils/src/thread_local_logger.rs

@@ -3,6 +3,7 @@ use std::sync::{Arc, Once};
 
 use log::{Log, Metadata, Record};
 use std::fmt::Formatter;
+use std::ops::Deref;
 
 pub struct GlobalLogger;
 #[cfg(not(feature = "must-log"))]
@@ -35,7 +36,9 @@ pub fn thread_init<T: 'static + Log>(logger: T) {
 }
 
 pub fn get() -> LocalLogger {
-    LOCAL_LOGGER.with(|inner| inner.borrow().clone())
+    let result = LOCAL_LOGGER.with(|inner| inner.borrow().clone());
+    let _ = result.deref();
+    result
 }
 
 pub fn set(logger: LocalLogger) {
@@ -46,6 +49,16 @@ pub fn reset() {
     self::set(LocalLogger::default())
 }
 
+impl LocalLogger {
+    pub fn inherit() -> Self {
+        get()
+    }
+
+    pub fn attach(self) {
+        set(self)
+    }
+}
+
 impl Default for LocalLogger {
     fn default() -> Self {
         #[cfg(not(feature = "must-log"))]
@@ -66,7 +79,10 @@ impl std::ops::Deref for LocalLogger {
             self.0.deref()
         }
         #[cfg(feature = "must-log")]
-        self.0.as_ref().unwrap().as_ref()
+        self.0
+            .as_ref()
+            .expect("Local logger must be set before use")
+            .as_ref()
     }
 }