From eb87e9406a20844f823fb048f187d6b1629b2504 Mon Sep 17 00:00:00 2001
From: ssrlive <30760636+ssrlive@users.noreply.github.com>
Date: Fri, 27 Dec 2024 22:52:46 +0800
Subject: [PATCH] enable_inner_logging feature

---
 Cargo.toml |  1 +
 src/log.rs | 14 +++++++++++---
 2 files changed, 12 insertions(+), 3 deletions(-)

diff --git a/Cargo.toml b/Cargo.toml
index 2d8d707..dce1d91 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -35,6 +35,7 @@ default = []
 async = ["blocking", "futures"]
 panic_on_unsent_packets = []
 verify_binary_signature = []
+enable_inner_logging = []
 
 [dependencies]
 blocking = { version = "1", optional = true }
diff --git a/src/log.rs b/src/log.rs
index d86d7c6..832d6a6 100644
--- a/src/log.rs
+++ b/src/log.rs
@@ -21,11 +21,13 @@ pub(crate) struct LogItem {
 }
 
 impl LogItem {
+    #[allow(dead_code)]
     pub(crate) fn new(level: log::Level, msg: String, timestamp: u64) -> Self {
         Self { level, msg, timestamp }
     }
 }
 
+#[cfg(feature = "enable_inner_logging")]
 static LOG_CONTAINER: std::sync::LazyLock<std::sync::Mutex<std::collections::VecDeque<LogItem>>> =
     std::sync::LazyLock::new(|| std::sync::Mutex::new(std::collections::VecDeque::new()));
 
@@ -35,13 +37,13 @@ static LOG_CONTAINER: std::sync::LazyLock<std::sync::Mutex<std::collections::Vec
 /// `message` must be a valid pointer that points to an aligned null terminated UTF-16 string
 pub unsafe extern "stdcall" fn default_logger(
     level: wintun_raw::WINTUN_LOGGER_LEVEL,
-    timestamp: wintun_raw::DWORD64,
+    _timestamp: wintun_raw::DWORD64,
     message: windows_sys::core::PCWSTR,
 ) {
     //Wintun will always give us a valid UTF16 null termineted string
     let utf8_msg = util::win_pwstr_to_string(message as *mut u16).unwrap_or_else(|e| e.to_string());
 
-    let l = match level {
+    let _l = match level {
         wintun_raw::WINTUN_LOGGER_LEVEL_WINTUN_LOG_INFO => {
             log::info!("WinTun: {}", utf8_msg);
             log::Level::Info
@@ -54,13 +56,15 @@ pub unsafe extern "stdcall" fn default_logger(
         _ => log::Level::Error,
     };
 
+    #[cfg(feature = "enable_inner_logging")]
     if let Err(e) = LOG_CONTAINER.lock().map(|mut log| {
-        log.push_back(LogItem::new(l, utf8_msg, timestamp));
+        log.push_back(LogItem::new(_l, utf8_msg, _timestamp));
     }) {
         log::error!("Failed to log message: {}", e);
     }
 }
 
+#[cfg(feature = "enable_inner_logging")]
 fn get_log() -> Vec<LogItem> {
     LOG_CONTAINER
         .lock()
@@ -68,6 +72,7 @@ fn get_log() -> Vec<LogItem> {
         .unwrap_or_else(|_e| Vec::new())
 }
 
+#[cfg(feature = "enable_inner_logging")]
 fn get_worst_log_msg(container: &[LogItem]) -> Option<&LogItem> {
     container.iter().max_by_key(|item| match item.level {
         log::Level::Error => 2,
@@ -78,6 +83,9 @@ fn get_worst_log_msg(container: &[LogItem]) -> Option<&LogItem> {
 }
 
 pub(crate) fn extract_wintun_log_error<T>(prifix: &str) -> Result<T, String> {
+    #[cfg(not(feature = "enable_inner_logging"))]
+    let info = "".to_string();
+    #[cfg(feature = "enable_inner_logging")]
     let info = get_worst_log_msg(&get_log())
         .map(|item| item.msg.clone())
         .unwrap_or_else(|| "No logs".to_string());