From 0c34ca2b31451c03ce2c2ab703d0f6b7723124ac Mon Sep 17 00:00:00 2001 From: Nikita Podvirnyi Date: Fri, 19 Jul 2024 14:31:21 +0200 Subject: [PATCH] feat: add hard limit to the game log file size --- src/games/genshin/game.rs | 52 +++++++++++++++++++++++-------------- src/games/honkai/game.rs | 52 +++++++++++++++++++++++-------------- src/games/pgr/game.rs | 52 +++++++++++++++++++++++-------------- src/games/star_rail/game.rs | 52 +++++++++++++++++++++++-------------- src/games/wuwa/game.rs | 52 +++++++++++++++++++++++-------------- src/games/zzz/game.rs | 52 +++++++++++++++++++++++-------------- 6 files changed, 198 insertions(+), 114 deletions(-) diff --git a/src/games/genshin/game.rs b/src/games/genshin/game.rs index c23c062..ca60b87 100644 --- a/src/games/genshin/game.rs +++ b/src/games/genshin/game.rs @@ -311,36 +311,50 @@ pub fn run() -> anyhow::Result<()> { // Create new game.log file to log all the game output let mut game_output = std::fs::File::create(consts::launcher_dir()?.join("game.log"))?; + // Limit max amount of log data in a file + // This is needed to stop wine from flushing + // tons of debug info there + const LOG_FILE_LIMIT: usize = 8 * 1024 * 1024; // 8 MiB + + let mut written = 0; + // Log process output while it's running while child.try_wait()?.is_none() { std::thread::sleep(std::time::Duration::from_secs(3)); - // Redirect stdout to the game.log file - if let Some(stdout) = &mut child.stdout { - let mut buf = Vec::new(); + // Check if we've written less than a limit amount of data + if written < LOG_FILE_LIMIT { + // Redirect stdout to the game.log file + if let Some(stdout) = &mut child.stdout { + let mut buf = Vec::new(); - stdout.read_to_end(&mut buf)?; + stdout.read_to_end(&mut buf)?; - if !buf.is_empty() { - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b" [stdout] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b" [stdout] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + + written += line.len() + 14; + } } } - } - // Redirect stdout to the game.log file - if let Some(stderr) = &mut child.stderr { - let mut buf = Vec::new(); + // Redirect stdout to the game.log file + if let Some(stderr) = &mut child.stderr { + let mut buf = Vec::new(); - stderr.read_to_end(&mut buf)?; + stderr.read_to_end(&mut buf)?; - if !buf.is_empty() { - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b"[!] [stderr] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b"[!] [stderr] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + + written += line.len() + 14; + } } } } diff --git a/src/games/honkai/game.rs b/src/games/honkai/game.rs index 32dfd8a..50e4103 100644 --- a/src/games/honkai/game.rs +++ b/src/games/honkai/game.rs @@ -264,36 +264,50 @@ pub fn run() -> anyhow::Result<()> { // Create new game.log file to log all the game output let mut game_output = std::fs::File::create(consts::launcher_dir()?.join("game.log"))?; + // Limit max amount of log data in a file + // This is needed to stop wine from flushing + // tons of debug info there + const LOG_FILE_LIMIT: usize = 8 * 1024 * 1024; // 8 MiB + + let mut written = 0; + // Log process output while it's running while child.try_wait()?.is_none() { std::thread::sleep(std::time::Duration::from_secs(3)); - // Redirect stdout to the game.log file - if let Some(stdout) = &mut child.stdout { - let mut buf = Vec::new(); + // Check if we've written less than a limit amount of data + if written < LOG_FILE_LIMIT { + // Redirect stdout to the game.log file + if let Some(stdout) = &mut child.stdout { + let mut buf = Vec::new(); - stdout.read_to_end(&mut buf)?; + stdout.read_to_end(&mut buf)?; - if !buf.is_empty() { - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b" [stdout] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b" [stdout] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + + written += line.len() + 14; + } } } - } - // Redirect stdout to the game.log file - if let Some(stderr) = &mut child.stderr { - let mut buf = Vec::new(); + // Redirect stdout to the game.log file + if let Some(stderr) = &mut child.stderr { + let mut buf = Vec::new(); - stderr.read_to_end(&mut buf)?; + stderr.read_to_end(&mut buf)?; - if !buf.is_empty() { - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b"[!] [stderr] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b"[!] [stderr] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + + written += line.len() + 14; + } } } } diff --git a/src/games/pgr/game.rs b/src/games/pgr/game.rs index a58d14c..38eac84 100644 --- a/src/games/pgr/game.rs +++ b/src/games/pgr/game.rs @@ -254,36 +254,50 @@ pub fn run() -> anyhow::Result<()> { // Create new game.log file to log all the game output let mut game_output = std::fs::File::create(consts::launcher_dir()?.join("game.log"))?; + // Limit max amount of log data in a file + // This is needed to stop wine from flushing + // tons of debug info there + const LOG_FILE_LIMIT: usize = 8 * 1024 * 1024; // 8 MiB + + let mut written = 0; + // Log process output while it's running while child.try_wait()?.is_none() { std::thread::sleep(std::time::Duration::from_secs(3)); - // Redirect stdout to the game.log file - if let Some(stdout) = &mut child.stdout { - let mut buf = Vec::new(); + // Check if we've written less than a limit amount of data + if written < LOG_FILE_LIMIT { + // Redirect stdout to the game.log file + if let Some(stdout) = &mut child.stdout { + let mut buf = Vec::new(); - stdout.read_to_end(&mut buf)?; + stdout.read_to_end(&mut buf)?; - if !buf.is_empty() { - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b" [stdout] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b" [stdout] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + + written += line.len() + 14; + } } } - } - // Redirect stdout to the game.log file - if let Some(stderr) = &mut child.stderr { - let mut buf = Vec::new(); + // Redirect stdout to the game.log file + if let Some(stderr) = &mut child.stderr { + let mut buf = Vec::new(); - stderr.read_to_end(&mut buf)?; + stderr.read_to_end(&mut buf)?; - if !buf.is_empty() { - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b"[!] [stderr] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b"[!] [stderr] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + + written += line.len() + 14; + } } } } diff --git a/src/games/star_rail/game.rs b/src/games/star_rail/game.rs index f9d303c..943d0d9 100644 --- a/src/games/star_rail/game.rs +++ b/src/games/star_rail/game.rs @@ -265,36 +265,50 @@ pub fn run() -> anyhow::Result<()> { // Create new game.log file to log all the game output let mut game_output = std::fs::File::create(consts::launcher_dir()?.join("game.log"))?; + // Limit max amount of log data in a file + // This is needed to stop wine from flushing + // tons of debug info there + const LOG_FILE_LIMIT: usize = 8 * 1024 * 1024; // 8 MiB + + let mut written = 0; + // Log process output while it's running while child.try_wait()?.is_none() { std::thread::sleep(std::time::Duration::from_secs(3)); - // Redirect stdout to the game.log file - if let Some(stdout) = &mut child.stdout { - let mut buf = Vec::new(); + // Check if we've written less than a limit amount of data + if written < LOG_FILE_LIMIT { + // Redirect stdout to the game.log file + if let Some(stdout) = &mut child.stdout { + let mut buf = Vec::new(); - stdout.read_to_end(&mut buf)?; + stdout.read_to_end(&mut buf)?; - if !buf.is_empty() { - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b" [stdout] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b" [stdout] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + + written += line.len() + 14; + } } } - } - // Redirect stdout to the game.log file - if let Some(stderr) = &mut child.stderr { - let mut buf = Vec::new(); + // Redirect stdout to the game.log file + if let Some(stderr) = &mut child.stderr { + let mut buf = Vec::new(); - stderr.read_to_end(&mut buf)?; + stderr.read_to_end(&mut buf)?; - if !buf.is_empty() { - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b"[!] [stderr] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b"[!] [stderr] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + + written += line.len() + 14; + } } } } diff --git a/src/games/wuwa/game.rs b/src/games/wuwa/game.rs index 4e74b52..5be8dfc 100644 --- a/src/games/wuwa/game.rs +++ b/src/games/wuwa/game.rs @@ -245,36 +245,50 @@ pub fn run() -> anyhow::Result<()> { // Create new game.log file to log all the game output let mut game_output = std::fs::File::create(consts::launcher_dir()?.join("game.log"))?; + // Limit max amount of log data in a file + // This is needed to stop wine from flushing + // tons of debug info there + const LOG_FILE_LIMIT: usize = 8 * 1024 * 1024; // 8 MiB + + let mut written = 0; + // Log process output while it's running while child.try_wait()?.is_none() { std::thread::sleep(std::time::Duration::from_secs(3)); - // Redirect stdout to the game.log file - if let Some(stdout) = &mut child.stdout { - let mut buf = Vec::new(); + // Check if we've written less than a limit amount of data + if written < LOG_FILE_LIMIT { + // Redirect stdout to the game.log file + if let Some(stdout) = &mut child.stdout { + let mut buf = Vec::new(); - stdout.read_to_end(&mut buf)?; + stdout.read_to_end(&mut buf)?; - if !buf.is_empty() { - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b" [stdout] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b" [stdout] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + + written += line.len() + 14; + } } } - } - // Redirect stdout to the game.log file - if let Some(stderr) = &mut child.stderr { - let mut buf = Vec::new(); + // Redirect stdout to the game.log file + if let Some(stderr) = &mut child.stderr { + let mut buf = Vec::new(); - stderr.read_to_end(&mut buf)?; + stderr.read_to_end(&mut buf)?; - if !buf.is_empty() { - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b"[!] [stderr] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b"[!] [stderr] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + + written += line.len() + 14; + } } } } diff --git a/src/games/zzz/game.rs b/src/games/zzz/game.rs index 9f52262..f4e1038 100644 --- a/src/games/zzz/game.rs +++ b/src/games/zzz/game.rs @@ -263,36 +263,50 @@ pub fn run() -> anyhow::Result<()> { // Create new game.log file to log all the game output let mut game_output = std::fs::File::create(consts::launcher_dir()?.join("game.log"))?; + // Limit max amount of log data in a file + // This is needed to stop wine from flushing + // tons of debug info there + const LOG_FILE_LIMIT: usize = 8 * 1024 * 1024; // 8 MiB + + let mut written = 0; + // Log process output while it's running while child.try_wait()?.is_none() { std::thread::sleep(std::time::Duration::from_secs(3)); - // Redirect stdout to the game.log file - if let Some(stdout) = &mut child.stdout { - let mut buf = Vec::new(); + // Check if we've written less than a limit amount of data + if written < LOG_FILE_LIMIT { + // Redirect stdout to the game.log file + if let Some(stdout) = &mut child.stdout { + let mut buf = Vec::new(); - stdout.read_to_end(&mut buf)?; + stdout.read_to_end(&mut buf)?; - if !buf.is_empty() { - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b" [stdout] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b" [stdout] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + + written += line.len() + 14; + } } } - } - // Redirect stdout to the game.log file - if let Some(stderr) = &mut child.stderr { - let mut buf = Vec::new(); + // Redirect stdout to the game.log file + if let Some(stderr) = &mut child.stderr { + let mut buf = Vec::new(); - stderr.read_to_end(&mut buf)?; + stderr.read_to_end(&mut buf)?; - if !buf.is_empty() { - for line in buf.split(|c| c == &b'\n') { - game_output.write_all(b"[!] [stderr] ")?; - game_output.write_all(line)?; - game_output.write_all(b"\n")?; + if !buf.is_empty() { + for line in buf.split(|c| c == &b'\n') { + game_output.write_all(b"[!] [stderr] ")?; + game_output.write_all(line)?; + game_output.write_all(b"\n")?; + + written += line.len() + 14; + } } } }