Skip to content

Commit

Permalink
fix: allow the app to display logs when launced from the terminal on …
Browse files Browse the repository at this point in the history
…windows.
  • Loading branch information
B0ney committed Feb 10, 2024
1 parent 84e87f7 commit 806a9e1
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 0 deletions.
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
19 changes: 19 additions & 0 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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| {
Expand Down Expand Up @@ -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) };
}
}

0 comments on commit 806a9e1

Please sign in to comment.