File tree Expand file tree Collapse file tree
Expand file tree Collapse file tree Original file line number Diff line number Diff line change 1- /target
1+ /target
2+ log.txt
Original file line number Diff line number Diff line change 11use 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
44thread_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+ }
You can’t perform that action at this time.
0 commit comments