1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
use serde_json::json;
use tokio::fs::File;
use tokio::io::BufWriter;
use tokio::prelude::*;
use tokio::sync::mpsc;
use crate::goose::GooseDebug;
use crate::GooseConfiguration;
pub async fn logger_main(
configuration: GooseConfiguration,
mut log_receiver: mpsc::UnboundedReceiver<Option<GooseDebug>>,
) {
let mut debug_log_file = None;
if !configuration.debug_log_file.is_empty() {
debug_log_file = match File::create(&configuration.debug_log_file).await {
Ok(f) => {
info!(
"writing errors to debug_log_file: {}",
&configuration.debug_log_file
);
Some(BufWriter::new(f))
}
Err(e) => {
panic!(
"failed to create debug_log_file ({}): {}",
configuration.debug_log_file, e
);
}
}
}
while let Some(message) = log_receiver.recv().await {
if let Some(goose_debug) = message {
if let Some(file) = debug_log_file.as_mut() {
let formatted_log = match configuration.debug_log_format.as_str() {
"json" => json!(goose_debug).to_string(),
"raw" => format!("{:?}", goose_debug).to_string(),
_ => unreachable!(),
};
match file.write(format!("{}\n", formatted_log).as_ref()).await {
Ok(_) => (),
Err(e) => {
warn!(
"failed to write to {}: {}",
&configuration.debug_log_file, e
);
}
}
};
} else {
break;
}
}
if let Some(file) = debug_log_file.as_mut() {
info!("flushing debug_log_file: {}", &configuration.debug_log_file);
let _ = file.flush().await;
};
}