Skip to content

Commit a8d4a7c

Browse files
committed
Add Logger data structure
1 parent f92772f commit a8d4a7c

2 files changed

Lines changed: 24 additions & 2 deletions

File tree

.gitignore

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
1-
/target
1+
/target
2+
log.txt

crates/chatbot/src/lib.rs

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use rand::{rngs::SmallRng, Rng, SeedableRng};
2-
use std::{cell::RefCell, path::PathBuf, time::Duration};
2+
use std::{cell::RefCell, io, path::PathBuf, time::Duration};
33

44
thread_local! {
55
static RNG: RefCell<SmallRng> = RefCell::new(SmallRng::from_entropy());
@@ -58,3 +58,24 @@ impl Chatbot {
5858
]
5959
}
6060
}
61+
62+
/// Holds chat messages and writes them to disk infrequently.
63+
#[derive(Default)]
64+
pub struct Logger {
65+
logs: Vec<String>,
66+
}
67+
68+
impl Logger {
69+
/// Saves the message to the logger.
70+
pub fn append(&mut self, message: &str) {
71+
self.logs.push(message.to_string());
72+
}
73+
74+
/// Potentially writes the logs to disk, if needed.
75+
pub async fn save(&self) -> io::Result<()> {
76+
if self.logs.len() % 3 == 0 {
77+
tokio::fs::write("log.txt", self.logs.join("\n")).await?;
78+
}
79+
Ok(())
80+
}
81+
}

0 commit comments

Comments
 (0)