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+ use crate :: models:: { Level , Log } ;
2+ use std:: io:: { Write , stdout} ;
3+
4+ pub fn log ( ) -> impl FnMut ( & Log ) {
5+ let mut error_count = 0 ;
6+ let mut warn_count = 0 ;
7+ let mut info_count = 0 ;
8+ move |l| {
9+ stdout ( )
10+ . write ( format ! ( "{}\n " , l. to_string( ) ) . as_bytes ( ) )
11+ . unwrap ( ) ;
12+ match l. level {
13+ Level :: Error => error_count += 1 ,
14+ Level :: Warn => warn_count += 1 ,
15+ Level :: Info => info_count += 1 ,
16+ _ => { }
17+ }
18+ stdout ( )
19+ . write (
20+ format ! (
21+ "Log Tracker: {} errors, {} warnings, {} infos\n " ,
22+ error_count, warn_count, info_count
23+ )
24+ . as_bytes ( ) ,
25+ )
26+ . unwrap ( ) ;
27+ }
28+ }
Original file line number Diff line number Diff line change 1+ mod logging;
12mod models;
23mod repository;
34mod systems;
45
6+ use crate :: logging:: log;
57use crate :: models:: * ;
68use crate :: systems:: * ;
79use repository:: * ;
@@ -71,4 +73,15 @@ fn main() {
7173 total_team_score += p. score ;
7274 } ) ;
7375 println ! ( "Total score after update: {}" , total_team_score) ;
76+
77+ // FnMut döndüren log fonksiyonunun kullanımı
78+ let mut logger = log ( ) ;
79+
80+ logger ( & Log :: new ( Level :: Info , "Authentication Success" . to_string ( ) ) ) ;
81+ logger ( & Log :: new ( Level :: Error , "File Not Found" . to_string ( ) ) ) ;
82+ logger ( & Log :: new ( Level :: Error , "Login failed" . to_string ( ) ) ) ;
83+ logger ( & Log :: new (
84+ Level :: Warn ,
85+ "Response Time Decreasing" . to_string ( ) ,
86+ ) ) ;
7487}
Original file line number Diff line number Diff line change 1+ use std:: fmt:: Display ;
2+
3+ #[ derive( Debug ) ]
4+ pub struct Game {
5+ pub title : String ,
6+ pub year : u16 ,
7+ pub popularity : f32 ,
8+ }
9+
110#[ derive( Debug ) ]
211pub struct Player {
312 pub id : u32 ,
@@ -10,3 +19,38 @@ pub struct Player {
1019pub struct GameWorld {
1120 pub players : Vec < Player > ,
1221}
22+
23+ #[ derive( Debug ) ]
24+ pub struct Log {
25+ pub level : Level ,
26+ pub explanation : String ,
27+ }
28+
29+ impl Log {
30+ pub fn new ( level : Level , explanation : String ) -> Self {
31+ Log { level, explanation }
32+ }
33+ }
34+
35+ impl Display for Log {
36+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
37+ write ! ( f, "{}:{}" , self . level, self . explanation)
38+ }
39+ }
40+
41+ #[ derive( Debug ) ]
42+ pub enum Level {
43+ Info ,
44+ Warn ,
45+ Error ,
46+ }
47+
48+ impl Display for Level {
49+ fn fmt ( & self , f : & mut std:: fmt:: Formatter < ' _ > ) -> std:: fmt:: Result {
50+ match self {
51+ Level :: Info => write ! ( f, "INFO" ) ,
52+ Level :: Warn => write ! ( f, "WARN" ) ,
53+ Level :: Error => write ! ( f, "ERROR" ) ,
54+ }
55+ }
56+ }
Original file line number Diff line number Diff line change 1- #[ derive( Debug ) ]
2- pub struct Game {
3- pub title : String ,
4- pub year : u16 ,
5- pub popularity : f32 ,
6- }
1+ use crate :: models:: Game ;
2+
73pub fn print_games ( games : & Vec < Game > ) {
84 for game in games {
95 println ! ( "{}, {}" , game. year, game. title) ;
You can’t perform that action at this time.
0 commit comments