From 04c136467587d54cc328a12b415ee6f895987339 Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Thu, 22 Aug 2024 17:35:54 -0700 Subject: [PATCH 1/2] Add Logger data structure --- .gitignore | 2 +- crates/chatbot/src/lib.rs | 23 ++++++++++++++++++++++- 2 files changed, 23 insertions(+), 2 deletions(-) diff --git a/.gitignore b/.gitignore index 869df07..3d91d49 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,2 @@ /target -Cargo.lock \ No newline at end of file +log.txt \ No newline at end of file diff --git a/crates/chatbot/src/lib.rs b/crates/chatbot/src/lib.rs index 13c321f..c9bd6c1 100644 --- a/crates/chatbot/src/lib.rs +++ b/crates/chatbot/src/lib.rs @@ -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 = RefCell::new(SmallRng::from_entropy()); @@ -58,3 +58,24 @@ impl Chatbot { ] } } + +/// Holds chat messages and writes them to disk infrequently. +#[derive(Default)] +pub struct Logger { + logs: Vec, +} + +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(()) + } +} From 47547c2992a3e84f41c15dfd2347c73cc9e18962 Mon Sep 17 00:00:00 2001 From: Will Crichton Date: Mon, 26 Aug 2024 14:22:12 -0700 Subject: [PATCH 2/2] Restore Cargo.lock to gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 3d91d49..04f3846 100644 --- a/.gitignore +++ b/.gitignore @@ -1,2 +1,3 @@ /target +Cargo.lock log.txt \ No newline at end of file