From 414f9918bdfd72e2ac892bad84354837fde4b801 Mon Sep 17 00:00:00 2001 From: B0ney <40839054+B0ney@users.noreply.github.com> Date: Sat, 10 Feb 2024 13:15:35 +0000 Subject: [PATCH 1/3] fix: allow the app to display logs when launced from the terminal on windows. --- Cargo.toml | 3 +++ src/main.rs | 19 +++++++++++++++++++ 2 files changed, 22 insertions(+) diff --git a/Cargo.toml b/Cargo.toml index da1382a..516dafd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -35,6 +35,9 @@ iced = { version = "^0.10.0", features = ["advanced", "image"] } flate2 = { version = "^1", optional = true } tar = { version = "^0.4", optional = true } +[target.'cfg(target_os = "windows")'.dependencies] +windows-sys = { version = "^0.52.0", features = ["Win32_System_Console", "Win32_Foundation"] } + [profile.release] opt-level = "s" lto = true diff --git a/src/main.rs b/src/main.rs index fac4894..5b95249 100644 --- a/src/main.rs +++ b/src/main.rs @@ -27,6 +27,8 @@ fn main() -> iced::Result { } pub fn setup_logger() -> Result<(), fern::InitError> { + attach_windows_console(); + let colors = ColoredLevelConfig::new().info(Color::Green); let make_formatter = |use_colors: bool| { @@ -75,3 +77,20 @@ pub fn setup_logger() -> Result<(), fern::InitError> { Ok(()) } + +/// (Windows) Allow the application to display logs to the terminal +/// regardless if it was compiled with `windows_subsystem = "windows"`. +/// +/// This is a no-op when compiled to non-windows targets. +fn attach_windows_console() { + #[cfg(target_os = "windows")] + { + use windows_sys::Win32::System::Console::{AttachConsole, ATTACH_PARENT_PROCESS}; + + // # SAFETY: + // According to the docs: https://learn.microsoft.com/en-us/windows/console/attachconsole + // + // AttachConsole doesn't have any footguns, so calling it like this is fine. + let _ = unsafe { AttachConsole(ATTACH_PARENT_PROCESS) }; + } +} From 4ce499553dc98ebb7e3863539c0bb62413be01a9 Mon Sep 17 00:00:00 2001 From: B0ney <40839054+B0ney@users.noreply.github.com> Date: Sat, 10 Feb 2024 15:54:22 +0000 Subject: [PATCH 2/3] use win32console instead of windows_sys for a safer abstraction. --- Cargo.toml | 2 +- src/main.rs | 11 ++++------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 516dafd..ba5dec6 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -36,7 +36,7 @@ flate2 = { version = "^1", optional = true } tar = { version = "^0.4", optional = true } [target.'cfg(target_os = "windows")'.dependencies] -windows-sys = { version = "^0.52.0", features = ["Win32_System_Console", "Win32_Foundation"] } +win32console = "^0.1.5" [profile.release] opt-level = "s" diff --git a/src/main.rs b/src/main.rs index 5b95249..ddef875 100644 --- a/src/main.rs +++ b/src/main.rs @@ -85,12 +85,9 @@ pub fn setup_logger() -> Result<(), fern::InitError> { fn attach_windows_console() { #[cfg(target_os = "windows")] { - use windows_sys::Win32::System::Console::{AttachConsole, ATTACH_PARENT_PROCESS}; - - // # SAFETY: - // According to the docs: https://learn.microsoft.com/en-us/windows/console/attachconsole - // - // AttachConsole doesn't have any footguns, so calling it like this is fine. - let _ = unsafe { AttachConsole(ATTACH_PARENT_PROCESS) }; + use win32console::console::WinConsole; + + const ATTACH_PARENT_PROCESS: u32 = 0xFFFFFFFF; + let _ = WinConsole::attach_console(ATTACH_PARENT_PROCESS); } } From 238a563f2bb66b399a9b826f58df7aea9d09ee60 Mon Sep 17 00:00:00 2001 From: Himadri Bhattacharjee <107522312+lavafroth@users.noreply.github.com> Date: Sun, 11 Feb 2024 11:06:12 +0530 Subject: [PATCH 3/3] lint: cargo fmt and clippy --- src/main.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/main.rs b/src/main.rs index ddef875..da920f0 100644 --- a/src/main.rs +++ b/src/main.rs @@ -28,7 +28,7 @@ fn main() -> iced::Result { pub fn setup_logger() -> Result<(), fern::InitError> { attach_windows_console(); - + let colors = ColoredLevelConfig::new().info(Color::Green); let make_formatter = |use_colors: bool| { @@ -80,13 +80,13 @@ pub fn setup_logger() -> Result<(), fern::InitError> { /// (Windows) Allow the application to display logs to the terminal /// regardless if it was compiled with `windows_subsystem = "windows"`. -/// +/// /// This is a no-op when compiled to non-windows targets. fn attach_windows_console() { #[cfg(target_os = "windows")] { use win32console::console::WinConsole; - + const ATTACH_PARENT_PROCESS: u32 = 0xFFFFFFFF; let _ = WinConsole::attach_console(ATTACH_PARENT_PROCESS); }