Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Logger data structure #38

Open
wants to merge 2 commits into
base: 07-pin-b
Choose a base branch
from
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