1
1
#![ allow( dead_code) ]
2
2
3
3
use std:: {
4
- fs:: { File , self } ,
5
- io:: { Write , BufWriter , Read , SeekFrom , Seek } ,
6
- path:: PathBuf , time:: Instant ,
4
+ fs:: { self , File } ,
5
+ io:: { BufWriter , Read , Seek , SeekFrom , Write } ,
6
+ path:: PathBuf ,
7
+ time:: Instant ,
7
8
} ;
8
9
9
10
use anyhow:: Context ;
@@ -14,15 +15,15 @@ use super::SimpleLog;
14
15
pub struct FileLog {
15
16
path : PathBuf ,
16
17
file : Option < BufWriter < File > > ,
17
- last_written : Option < Instant >
18
+ last_written : Option < Instant > ,
18
19
}
19
20
20
21
impl FileLog {
21
22
pub fn new ( path : impl Into < PathBuf > ) -> Self {
22
23
Self {
23
24
path : path. into ( ) ,
24
25
file : None ,
25
- last_written : None
26
+ last_written : None ,
26
27
}
27
28
}
28
29
@@ -34,26 +35,37 @@ impl FileLog {
34
35
if let Some ( dir) = self . path . parent ( ) {
35
36
fs:: create_dir_all ( dir) . context ( "Failed to create parent directories for log file." ) ?;
36
37
}
37
- let mut file = File :: options ( ) . create ( true ) . append ( true ) . open ( & self . path ) . context ( "Failed to open or create log file." ) ?;
38
-
38
+ let mut file = File :: options ( )
39
+ . create ( true )
40
+ . append ( true )
41
+ . open ( & self . path )
42
+ . context ( "Failed to open or create log file." ) ?;
43
+
39
44
dbg ! ( ) ;
40
45
if dbg ! ( dbg!( file. metadata( ) . unwrap( ) . len( ) ) > 100 * 1024 ) {
41
- file = File :: options ( ) . write ( true ) . read ( true ) . open ( & self . path ) . context ( "Failed to open or create log file." ) ?;
46
+ file = File :: options ( )
47
+ . write ( true )
48
+ . read ( true )
49
+ . open ( & self . path )
50
+ . context ( "Failed to open or create log file." ) ?;
42
51
let mut contents = String :: new ( ) ;
43
- file. read_to_string ( & mut contents) . context ( "Failed to read log file." ) ?;
52
+ file. read_to_string ( & mut contents)
53
+ . context ( "Failed to read log file." ) ?;
44
54
45
55
let mut truncated_contents = String :: new ( ) ;
46
56
for ( index, _) in contents. match_indices ( '\n' ) {
47
- let succeeding = & contents[ ( index+ 1 ) ..] ;
57
+ let succeeding = & contents[ ( index + 1 ) ..] ;
48
58
if succeeding. len ( ) > 10 * 1024 {
49
59
continue ;
50
60
}
51
61
truncated_contents. push_str ( succeeding) ;
52
62
break ;
53
63
}
54
- file. seek ( SeekFrom :: Start ( 0 ) ) . context ( "Failed to seek in log file." ) ?;
64
+ file. seek ( SeekFrom :: Start ( 0 ) )
65
+ . context ( "Failed to seek in log file." ) ?;
55
66
file. set_len ( 0 ) . context ( "Failed to clear log file." ) ?;
56
- file. write_all ( truncated_contents. as_bytes ( ) ) . context ( "Failed to write log file." ) ?;
67
+ file. write_all ( truncated_contents. as_bytes ( ) )
68
+ . context ( "Failed to write log file." ) ?;
57
69
}
58
70
let writer = BufWriter :: new ( file) ;
59
71
Ok ( self . file . insert ( writer) )
@@ -62,7 +74,10 @@ impl FileLog {
62
74
63
75
impl SimpleLog for FileLog {
64
76
fn log ( & mut self , message : & str ) {
65
- let file = self . open_file ( ) . context ( "Failed to prepare log file." ) . unwrap ( ) ;
77
+ let file = self
78
+ . open_file ( )
79
+ . context ( "Failed to prepare log file." )
80
+ . unwrap ( ) ;
66
81
writeln ! ( file, "{}" , message) . unwrap ( ) ;
67
82
file. flush ( ) . unwrap ( ) ;
68
83
}
0 commit comments