@@ -6,10 +6,6 @@ use std::io::Write;
66use termcolor:: { Color , ColorChoice , ColorSpec , StandardStream , WriteColor } ;
77use tokio:: sync:: { broadcast, mpsc, oneshot} ;
88
9- // --- Public API ---
10-
11- /// A single log entry, containing all relevant information.
12- /// It must be `Clone` to be sent over a broadcast channel.
139#[ derive( Clone , Debug ) ]
1410pub struct LogEntry {
1511 pub seq : u64 ,
@@ -32,22 +28,17 @@ impl fmt::Display for LogEntry {
3228 }
3329}
3430
35- /// A clonable handle that allows creating new log readers.
36- /// This is the primary object you'll interact with after initialization.
3731#[ derive( Clone ) ]
3832pub struct LogHandle {
3933 history_requester : mpsc:: Sender < HistoryRequest > ,
4034 broadcast_sender : broadcast:: Sender < LogEntry > ,
4135}
4236
43- /// A reader that provides access to the log history and the live stream.
4437pub struct LogReader {
4538 history_snapshot : VecDeque < LogEntry > ,
4639 receiver : broadcast:: Receiver < LogEntry > ,
4740}
4841
49- /// A builder for creating and initializing the logger.
50- /// This is the main entry point for setting up the logging system.
5142pub struct Builder {
5243 filter : LevelFilter ,
5344 max_history : usize ,
@@ -59,7 +50,7 @@ pub struct Builder {
5950impl Default for Builder {
6051 fn default ( ) -> Self {
6152 Self {
62- filter : LevelFilter :: Info , // Default log level
53+ filter : LevelFilter :: Info ,
6354 max_history : 1000 ,
6455 broadcast_capacity : 1024 ,
6556 mpsc_capacity : 4096 ,
@@ -89,7 +80,6 @@ impl Builder {
8980 }
9081
9182 pub fn init ( self ) -> Result < LogHandle , SetLoggerError > {
92- // FIX: The channel now sends our new, simple `LogMessage` struct.
9383 let ( log_tx, log_rx) = mpsc:: channel :: < LogMessage > ( self . mpsc_capacity ) ;
9484 let ( history_tx, history_rx) = mpsc:: channel ( 32 ) ;
9585 let ( broadcast_tx, _) = broadcast:: channel ( self . broadcast_capacity ) ;
@@ -172,9 +162,7 @@ struct LogMessage {
172162 message : String ,
173163}
174164
175- /// The frontend that implements the `log::Log` trait.
176165struct FeosLogger {
177- // FIX: The sender now sends the safe `LogMessage` struct.
178166 sender : mpsc:: Sender < LogMessage > ,
179167 filter : LevelFilter ,
180168}
@@ -189,8 +177,6 @@ impl Log for FeosLogger {
189177 return ;
190178 }
191179
192- // FIX: Create the safe `LogMessage` here, on the calling thread.
193- // `format!` turns the `Arguments` into a `String`, which is `Send`.
194180 let msg = LogMessage {
195181 level : record. level ( ) ,
196182 target : record. target ( ) . to_string ( ) ,
@@ -205,9 +191,7 @@ impl Log for FeosLogger {
205191 fn flush ( & self ) { }
206192}
207193
208- /// The central actor task that owns and manages all logger state.
209194struct LoggerActor {
210- // FIX: The receiver now gets the safe `LogMessage` struct.
211195 log_receiver : mpsc:: Receiver < LogMessage > ,
212196 history_requester : mpsc:: Receiver < HistoryRequest > ,
213197 broadcast_sender : broadcast:: Sender < LogEntry > ,
@@ -222,11 +206,9 @@ impl LoggerActor {
222206 async fn run ( mut self ) {
223207 loop {
224208 tokio:: select! {
225- // FIX: Receive the `LogMessage` instead of a `Record`.
226209 Some ( msg) = self . log_receiver. recv( ) => {
227210 self . seq_counter += 1 ;
228211
229- // FIX: Construct the final `LogEntry` from the `LogMessage`.
230212 let entry = LogEntry {
231213 seq: self . seq_counter,
232214 timestamp: Utc :: now( ) ,
@@ -236,8 +218,6 @@ impl LoggerActor {
236218 } ;
237219
238220 if self . log_to_stdout {
239- // We ignore the result of the write operation. In a more
240- // critical application, you might handle I/O errors here.
241221 let _ = self . write_log_entry_to_stdout( & entry) ;
242222 }
243223
@@ -260,7 +240,6 @@ impl LoggerActor {
260240
261241 fn write_log_entry_to_stdout ( & mut self , entry : & LogEntry ) -> std:: io:: Result < ( ) > {
262242 let mut level_spec = ColorSpec :: new ( ) ;
263- // Set color and boldness based on log level
264243 match entry. level {
265244 Level :: Error => level_spec. set_fg ( Some ( Color :: Red ) ) . set_bold ( true ) ,
266245 Level :: Warn => level_spec. set_fg ( Some ( Color :: Yellow ) ) . set_bold ( true ) ,
@@ -269,18 +248,15 @@ impl LoggerActor {
269248 Level :: Trace => level_spec. set_fg ( Some ( Color :: Magenta ) ) . set_bold ( true ) ,
270249 } ;
271250
272- // Write the timestamp (no color)
273251 write ! (
274252 & mut self . stdout_writer,
275253 "[{} " ,
276254 entry. timestamp. format( "%Y-%m-%dT%H:%M:%SZ" )
277255 ) ?;
278256
279- // Set the color for the level and write it
280257 self . stdout_writer . set_color ( & level_spec) ?;
281258 write ! ( & mut self . stdout_writer, "{:<5}" , entry. level. to_string( ) ) ?;
282259
283- // Reset color for the rest of the line
284260 self . stdout_writer . reset ( ) ?;
285261 writeln ! (
286262 & mut self . stdout_writer,
0 commit comments