Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/target
Cargo.lock
Cargo.lock
log.txt
23 changes: 22 additions & 1 deletion crates/chatbot/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use rand::{rngs::SmallRng, Rng, SeedableRng};
use std::{cell::RefCell, path::PathBuf, time::Duration};
use std::{cell::RefCell, io, path::PathBuf, time::Duration};

thread_local! {
static RNG: RefCell<SmallRng> = RefCell::new(SmallRng::from_entropy());
Expand Down Expand Up @@ -58,3 +58,24 @@ impl Chatbot {
]
}
}

/// Holds chat messages and writes them to disk infrequently.
#[derive(Default)]
pub struct Logger {
logs: Vec<String>,
}

impl Logger {
/// Saves the message to the logger.
pub fn append(&mut self, message: &str) {
self.logs.push(message.to_string());
}

/// Potentially writes the logs to disk, if needed.
pub async fn save(&self) -> io::Result<()> {
if self.logs.len() % 3 == 0 {
tokio::fs::write("log.txt", self.logs.join("\n")).await?;
}
Ok(())
}
}
Loading