Skip to content

Commit b6a0795

Browse files
authored
feat(log): Dump log at end (#139)
2 parents aad4054 + 094584d commit b6a0795

File tree

3 files changed

+34
-171
lines changed

3 files changed

+34
-171
lines changed

Cargo.lock

+13-160
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/weidu.rs

+13-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::{
55
sync::{
66
atomic::{AtomicUsize, Ordering},
77
mpsc::{self, Receiver, TryRecvError},
8-
Arc,
8+
Arc, RwLock,
99
},
1010
thread,
1111
};
@@ -15,6 +15,8 @@ use crate::{
1515
weidu_parser::parse_raw_output,
1616
};
1717

18+
const TICK: u64 = 1000;
19+
1820
pub(crate) fn get_user_input() -> String {
1921
let stdin = io::stdin();
2022
let mut input = String::new();
@@ -70,15 +72,17 @@ pub(crate) fn handle_io(
7072
parser_config: Arc<ParserConfig>,
7173
timeout: usize,
7274
) -> InstallationResult {
75+
let log = Arc::new(RwLock::new(String::new()));
7376
let mut weidu_stdin = child.stdin.take().unwrap();
74-
let wait_counter = Arc::new(AtomicUsize::new(0));
77+
let wait_count = Arc::new(AtomicUsize::new(0));
7578
let raw_output_receiver = create_output_reader(child.stdout.take().unwrap());
7679
let (sender, parsed_output_receiver) = mpsc::channel::<State>();
7780
parse_raw_output(
7881
sender,
7982
raw_output_receiver,
8083
parser_config,
81-
wait_counter.clone(),
84+
wait_count.clone(),
85+
log.clone(),
8286
timeout,
8387
);
8488

@@ -93,6 +97,7 @@ pub(crate) fn handle_io(
9397
}
9498
State::CompletedWithErrors { error_details } => {
9599
log::error!("Weidu process seem to have completed with errors");
100+
log::error!("Dumping log: {}", log.read().unwrap());
96101
weidu_stdin
97102
.write_all("\n".as_bytes())
98103
.expect("Failed to send final ENTER to weidu process");
@@ -103,10 +108,12 @@ pub(crate) fn handle_io(
103108
"Weidu process seem to have been running for {} seconds, exiting",
104109
timeout
105110
);
111+
log::error!("Dumping log: {}", log.read().unwrap());
106112
return InstallationResult::Fail("Timed out".to_string());
107113
}
108114
State::CompletedWithWarnings => {
109115
log::warn!("Weidu process seem to have completed with warnings");
116+
log::warn!("Dumping log: {}", log.read().unwrap());
110117
weidu_stdin
111118
.write_all("\n".as_bytes())
112119
.expect("Failed to send final ENTER to weidu process");
@@ -127,14 +134,11 @@ pub(crate) fn handle_io(
127134
}
128135
}
129136
Err(TryRecvError::Empty) => {
130-
log::info!(
131-
"{}\r",
132-
".".repeat(wait_counter.load(Ordering::Relaxed) % 10)
133-
);
137+
log::info!("{}", ".".repeat(wait_count.load(Ordering::Relaxed) % 10));
134138
std::io::stdout().flush().expect("Failed to flush stdout");
135139

136-
wait_counter.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
137-
sleep(1000);
140+
wait_count.fetch_add(1, std::sync::atomic::Ordering::Relaxed);
141+
sleep(TICK);
138142

139143
std::io::stdout().flush().expect("Failed to flush stdout");
140144
}

src/weidu_parser.rs

+8-2
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ use std::{
22
sync::{
33
atomic::{AtomicUsize, Ordering},
44
mpsc::{Receiver, Sender, TryRecvError},
5-
Arc,
5+
Arc, RwLock,
66
},
77
thread,
88
};
@@ -21,6 +21,7 @@ pub(crate) fn parse_raw_output(
2121
receiver: Receiver<String>,
2222
parser_config: Arc<ParserConfig>,
2323
wait_count: Arc<AtomicUsize>,
24+
log: Arc<RwLock<String>>,
2425
timeout: usize,
2526
) {
2627
let mut current_state = ParserState::LookingForInterestingOutput;
@@ -32,6 +33,9 @@ pub(crate) fn parse_raw_output(
3233
match receiver.try_recv() {
3334
Ok(string) => match current_state {
3435
ParserState::CollectingQuestion | ParserState::WaitingForMoreQuestionContent => {
36+
if let Ok(mut writer) = log.write() {
37+
writer.push_str(&string);
38+
}
3539
if parser_config.useful_status_words.contains(&string) {
3640
log::debug!(
3741
"Weidu seems to know an answer for the last question, ignoring it"
@@ -45,7 +49,9 @@ pub(crate) fn parse_raw_output(
4549
}
4650
}
4751
ParserState::LookingForInterestingOutput => {
48-
log::trace!("{}", string);
52+
if let Ok(mut writer) = log.write() {
53+
writer.push_str(&string);
54+
}
4955
if let Some(weidu_finished_state) =
5056
parser_config.detect_weidu_finished_state(&string)
5157
{

0 commit comments

Comments
 (0)