From 8a8a690cfae14aaa844b0e5629d7cc950c5e22c3 Mon Sep 17 00:00:00 2001 From: IQuant Date: Sun, 17 Nov 2024 20:37:12 +0300 Subject: [PATCH] Disable console and write logs to a file instead on Windows. --- noita-proxy/Cargo.lock | 25 +++++++++++++++++++++++++ noita-proxy/Cargo.toml | 3 ++- noita-proxy/src/main.rs | 25 +++++++++++++++++++++++++ 3 files changed, 52 insertions(+), 1 deletion(-) diff --git a/noita-proxy/Cargo.lock b/noita-proxy/Cargo.lock index 47b09dac..d4c14217 100644 --- a/noita-proxy/Cargo.lock +++ b/noita-proxy/Cargo.lock @@ -2077,6 +2077,7 @@ dependencies = [ "thiserror 2.0.2", "tokio", "tracing", + "tracing-appender", "tracing-subscriber", "tungstenite", "unic-langid", @@ -3518,10 +3519,12 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5dfd88e563464686c916c7e46e623e520ddc6d79fa6641390f2e3fa86e83e885" dependencies = [ "deranged", + "itoa", "num-conv", "powerfmt", "serde", "time-core", + "time-macros", ] [[package]] @@ -3530,6 +3533,16 @@ version = "0.1.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef927ca75afb808a4d64dd374f00a2adf8d0fcff8e7b184af886c3c87ec4a3f3" +[[package]] +name = "time-macros" +version = "0.2.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f252a68540fde3a3877aeea552b832b40ab9a69e318efd078774a01ddee1ccf" +dependencies = [ + "num-conv", + "time-core", +] + [[package]] name = "tiny-skia" version = "0.11.4" @@ -3695,6 +3708,18 @@ dependencies = [ "tracing-core", ] +[[package]] +name = "tracing-appender" +version = "0.2.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3566e8ce28cc0a3fe42519fc80e6b4c943cc4c8cef275620eb8dac2d3d4e06cf" +dependencies = [ + "crossbeam-channel", + "thiserror 1.0.69", + "time", + "tracing-subscriber", +] + [[package]] name = "tracing-attributes" version = "0.1.27" diff --git a/noita-proxy/Cargo.toml b/noita-proxy/Cargo.toml index ea4497e1..01096e66 100644 --- a/noita-proxy/Cargo.toml +++ b/noita-proxy/Cargo.toml @@ -49,6 +49,7 @@ quick-xml = { version = "0.37.0", features = ["serialize"] } dashmap = "6.0.1" eyre = "0.6.12" tokio = { version = "1.40.0", features = ["macros", "rt-multi-thread"] } +tracing-appender = "0.2.3" [build-dependencies] winresource = "0.1.17" @@ -61,4 +62,4 @@ lto = true strip = true [profile.release-lto] -inherits = "release" \ No newline at end of file +inherits = "release" diff --git a/noita-proxy/src/main.rs b/noita-proxy/src/main.rs index 413b8fb5..49cc0af1 100644 --- a/noita-proxy/src/main.rs +++ b/noita-proxy/src/main.rs @@ -1,3 +1,10 @@ +#![windows_subsystem = "windows"] + +use std::{ + fs::File, + io::{self, BufWriter}, +}; + use eframe::{ egui::{IconData, ViewportBuilder}, NativeOptions, @@ -9,13 +16,31 @@ use tracing_subscriber::EnvFilter; #[allow(clippy::needless_return)] #[tokio::main(worker_threads = 2)] async fn main() { + let log_to_file = cfg!(windows); + let writer = { + if log_to_file { + let file = File::create("ew_log.txt"); + println!("Creating a log file"); + match file { + Ok(file) => Box::new(BufWriter::new(file)) as Box, + Err(_) => Box::new(std::io::stdout()) as Box, + } + } else { + Box::new(std::io::stdout()) as Box + } + }; + + let (non_blocking_writer, _guard) = tracing_appender::non_blocking(writer); + let my_subscriber = tracing_subscriber::FmtSubscriber::builder() .with_env_filter( EnvFilter::builder() .with_default_directive(LevelFilter::INFO.into()) .from_env_lossy(), ) + .with_writer(non_blocking_writer) .finish(); + tracing::subscriber::set_global_default(my_subscriber).expect("setting tracing default failed"); let args: Args = argh::from_env();