Skip to content

Commit

Permalink
fix mc logs tx not being dropped correctly
Browse files Browse the repository at this point in the history
  • Loading branch information
blarfoon committed Jan 12, 2025
1 parent c4217ff commit 90f06c3
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 24 deletions.
10 changes: 5 additions & 5 deletions crates/carbon_app/src/managers/instance/log.rs
Original file line number Diff line number Diff line change
Expand Up @@ -302,7 +302,7 @@ impl ManagerRef<'_, InstanceManager> {
};

let mut stdout_processor =
LogProcessor::new(LogEntrySourceKind::StdOut, tx).await;
LogProcessor::new(LogEntrySourceKind::StdOut, &tx).await;

let mut buf = Vec::new();
let _ = file.read_to_end(&mut buf).await;
Expand All @@ -323,14 +323,14 @@ pub fn format_message_as_log4j_event(message: &str) -> String {
format!("<log4j:Event logger=\"GDLAUNCHER\" timestamp=\"{}\" level=\"INFO\" thread=\"N/A\">\n\t<log4j:Message><![CDATA[{}]]></log4j:Message>\n</log4j:Event>\n", Utc::now().timestamp_millis(), message)
}

pub struct LogProcessor {
pub struct LogProcessor<'a> {
pub parser: LogParser,
pub kind: LogEntrySourceKind,
pub log: watch::Sender<GameLog>,
pub log: &'a watch::Sender<GameLog>,
}

impl LogProcessor {
pub async fn new(kind: LogEntrySourceKind, log: watch::Sender<GameLog>) -> Self {
impl<'a> LogProcessor<'a> {
pub async fn new(kind: LogEntrySourceKind, log: &'a watch::Sender<GameLog>) -> Self {
Self {
parser: LogParser::new(),
kind,
Expand Down
6 changes: 2 additions & 4 deletions crates/carbon_app/src/managers/instance/run/java.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,9 +76,8 @@ pub async fn check_and_install(

let msg = format!("Suggested Java Profile: {java_profile:?}");

log.send_modify(|log| log.add_entry(LogEntry::system_message(msg.clone())));

if let Some(ref mut file) = file {
log.send_modify(|log| log.add_entry(LogEntry::system_message(msg.clone())));
file.write_all(format_message_as_log4j_event(&msg).as_bytes())
.await?;
}
Expand Down Expand Up @@ -274,9 +273,8 @@ pub async fn check_and_install(

let msg = format!("Using Java: {java:#?}");

log.send_modify(|log| log.add_entry(LogEntry::system_message(msg.clone())));

if let Some(file) = file {
log.send_modify(|log| log.add_entry(LogEntry::system_message(msg.clone())));
file.write_all(format_message_as_log4j_event(&msg).as_bytes())
.await?;
}
Expand Down
19 changes: 10 additions & 9 deletions crates/carbon_app/src/managers/instance/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -311,10 +311,10 @@ impl ManagerRef<'_, InstanceManager> {
})
);

log.send_modify(|log| {
log.add_entry(LogEntry::system_message(msg.clone()));
});
if let Some(file) = file.as_mut() {
log.send_modify(|log| {
log.add_entry(LogEntry::system_message(msg.clone()));
});
file.write_all(format_message_as_log4j_event(&msg).as_bytes())
.await?;
}
Expand Down Expand Up @@ -549,7 +549,7 @@ impl ManagerRef<'_, InstanceManager> {
tracing::info!("Instance killed");
drop(child.kill().await);
},
_ = read_logs(log.clone(), stdout, stderr, file.as_mut()) => {
_ = read_logs(&log, stdout, stderr, file.as_mut()) => {
tracing::info!("Instance read logs");
},
_ = update_playtime => {
Expand All @@ -574,10 +574,11 @@ impl ManagerRef<'_, InstanceManager> {
if let Ok(exitcode) = child.wait().await {
let msg = format!("{exitcode}");

log.send_modify(|log| log.add_entry(LogEntry::system_message(msg.clone())));

if let Some(file) = file.as_mut() {
// TODO: not sure how to handle an error in here
log.send_modify(|log| {
log.add_entry(LogEntry::system_message(msg.clone()))
});
let _ = file
.write_all(format_message_as_log4j_event(&msg).as_bytes())
.await;
Expand Down Expand Up @@ -843,7 +844,7 @@ impl From<&LaunchState> for domain::LaunchState {
}

async fn read_logs(
log: watch::Sender<GameLog>,
log: &watch::Sender<GameLog>,
stdout: impl AsyncReadExt + Unpin + Send + 'static,
stderr: impl AsyncReadExt + Unpin + Send + 'static,
file: Option<&mut File>,
Expand Down Expand Up @@ -888,12 +889,12 @@ async fn read_pipe(
}

async fn process_logs(
log: watch::Sender<GameLog>,
log: &watch::Sender<GameLog>,
mut stdout_rx: mpsc::Receiver<Vec<u8>>,
mut stderr_rx: mpsc::Receiver<Vec<u8>>,
mut file: Option<&mut File>,
) {
let mut stdout_processor = LogProcessor::new(LogEntrySourceKind::StdOut, log.clone()).await;
let mut stdout_processor = LogProcessor::new(LogEntrySourceKind::StdOut, log).await;

let mut stderr_processor = LogProcessor::new(LogEntrySourceKind::StdErr, log).await;

Expand Down
9 changes: 3 additions & 6 deletions crates/carbon_app/src/managers/minecraft/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,9 +118,8 @@ impl ManagerRef<'_, MinecraftManager> {

let msg = format!("LWJGL Meta {} - {}", lwjgl.uid, lwjgl.version);

log.send_modify(|log| log.add_entry(LogEntry::system_message(msg.clone())));

if let Some(file) = file.as_mut() {
log.send_modify(|log| log.add_entry(LogEntry::system_message(msg.clone())));
file.write_all(format_message_as_log4j_event(&msg).as_bytes())
.await?;
}
Expand Down Expand Up @@ -154,9 +153,8 @@ impl ManagerRef<'_, MinecraftManager> {
.join("\n\t-> ")
);

log.send_modify(|log| log.add_entry(LogEntry::system_message(msg.clone())));

if let Some(file) = file.as_mut() {
log.send_modify(|log| log.add_entry(LogEntry::system_message(msg.clone())));
file.write_all(format_message_as_log4j_event(&msg).as_bytes())
.await?;
}
Expand All @@ -176,9 +174,8 @@ impl ManagerRef<'_, MinecraftManager> {
client_main_jar.path.to_string_lossy().to_string()
);

log.send_modify(|log| log.add_entry(LogEntry::system_message(msg.clone())));

if let Some(file) = file.as_mut() {
log.send_modify(|log| log.add_entry(LogEntry::system_message(msg.clone())));
file.write_all(format_message_as_log4j_event(&msg).as_bytes())
.await?;
}
Expand Down

0 comments on commit 90f06c3

Please sign in to comment.