@@ -24,13 +24,12 @@ use std::sync::Mutex;
24
24
///
25
25
/// See `slog` at https://github.com/slog-rs/slog
26
26
/// See `log` at https://github.com/rust-lang/log
27
- ///
27
+ ///
28
28
29
- fn create_file < S : Storage > ( storage : & S , dp_path : & str , timestamp : i64 ) -> Result < S :: F > {
30
- let new_path = generate_filename ( dp_path, FileType :: OldInfoLog , timestamp as u64 ) ;
31
- storage. rename ( dp_path, new_path. as_str ( ) ) ?;
32
- storage. create ( dp_path)
33
-
29
+ fn create_file < S : Storage > ( storage : & S , dp_path : & str , timestamp : i64 ) -> Result < S :: F > {
30
+ let new_path = generate_filename ( dp_path, FileType :: OldInfoLog , timestamp as u64 ) ;
31
+ storage. rename ( dp_path, new_path. as_str ( ) ) ?;
32
+ storage. create ( dp_path)
34
33
}
35
34
36
35
pub struct Logger {
@@ -45,10 +44,10 @@ impl Logger {
45
44
/// If `inner` is `None`
46
45
/// - In dev mode, use a std output
47
46
/// - In release mode, use a storage specific file with name `LOG`
48
- pub fn new < S : Storage + Clone + ' static > (
47
+ pub fn new < S : Storage + Clone + ' static > (
49
48
inner : Option < slog:: Logger > ,
50
49
level : LevelFilter ,
51
- storage : S ,
50
+ storage : S ,
52
51
db_path : String ,
53
52
) -> Self {
54
53
let inner = match inner {
@@ -62,12 +61,13 @@ impl Logger {
62
61
} else {
63
62
// Use a file `Log` to record all logs
64
63
// TODO: add file rotation
65
- let file = create_file ( & storage, db_path. as_str ( ) , Local :: now ( ) . timestamp ( ) ) . unwrap ( ) ;
66
- let file_fn = move |path : String | { create_file ( & storage,
67
- path. as_str ( ) , Local :: now ( ) . timestamp ( ) ) } ;
68
- let drain =FileBasedDrain :: new ( file, db_path. clone ( ) ,
69
- file_fn)
70
- . add_rotator ( RotatedFileBySize :: new ( 0 ) ) ;
64
+ let file =
65
+ create_file ( & storage, db_path. as_str ( ) , Local :: now ( ) . timestamp ( ) ) . unwrap ( ) ;
66
+ let file_fn = move |path : String | {
67
+ create_file ( & storage, path. as_str ( ) , Local :: now ( ) . timestamp ( ) )
68
+ } ;
69
+ let drain = FileBasedDrain :: new ( file, db_path, file_fn)
70
+ . add_rotator ( RotatedFileBySize :: new ( 0 ) ) ;
71
71
let drain = slog_async:: Async :: new ( drain) . build ( ) . fuse ( ) ;
72
72
slog:: Logger :: root ( drain, o ! ( ) )
73
73
}
@@ -128,36 +128,37 @@ fn log_to_slog_level(level: log::Level) -> Level {
128
128
}
129
129
}
130
130
131
- struct FileBasedDrain < F : File > {
131
+ struct FileBasedDrain < F : File > {
132
132
inner : Mutex < F > ,
133
133
rotators : Vec < Box < dyn Rotator > > ,
134
- dp_path : String ,
135
- new_file : Box < dyn Send + Fn ( String ) -> Result < F > >
134
+ dp_path : String ,
135
+ new_file : Box < dyn Send + Fn ( String ) -> Result < F > > ,
136
136
}
137
137
138
- impl < F : File > FileBasedDrain < F > {
139
- fn new < H > ( f : F , path : String , new_file : H ) -> Self
140
- where H : ' static +Send +Fn ( String ) ->Result < F >
141
- {
138
+ impl < F : File > FileBasedDrain < F > {
139
+ fn new < H > ( f : F , path : String , new_file : H ) -> Self
140
+ where
141
+ H : ' static + Send + Fn ( String ) -> Result < F > ,
142
+ {
142
143
FileBasedDrain {
143
- dp_path : path. clone ( ) ,
144
+ dp_path : path,
144
145
inner : Mutex :: new ( f) ,
145
146
rotators : vec ! [ ] ,
146
- new_file : Box :: new ( new_file)
147
+ new_file : Box :: new ( new_file) ,
147
148
}
148
149
}
149
150
150
- fn add_rotator < R : ' static + Rotator > ( mut self , rotator : R ) -> Self {
151
+ fn add_rotator < R : ' static + Rotator > ( mut self , rotator : R ) -> Self {
151
152
if rotator. is_enabled ( ) {
152
153
self . rotators . push ( Box :: new ( rotator) ) ;
153
154
}
154
- for rotator in ( & self ) . rotators . iter ( ) {
155
+ for rotator in self . rotators . iter ( ) {
155
156
rotator. prepare ( & * self . inner . lock ( ) . unwrap ( ) ) . unwrap ( ) ;
156
157
}
157
158
self
158
159
}
159
160
160
- fn flush ( & self ) ->Result < ( ) > {
161
+ fn flush ( & self ) -> Result < ( ) > {
161
162
self . inner . lock ( ) . unwrap ( ) . flush ( ) ?;
162
163
let new_file = ( self . new_file ) ( self . dp_path . clone ( ) ) . unwrap ( ) ;
163
164
@@ -166,12 +167,11 @@ impl<F: File> FileBasedDrain<F> {
166
167
for rotator in self . rotators . iter ( ) {
167
168
rotator. on_rotate ( ) ?;
168
169
}
169
- return Ok ( ( ) ) ;
170
-
170
+ Ok ( ( ) )
171
171
}
172
172
}
173
173
174
- impl < F : File > Drain for FileBasedDrain < F > {
174
+ impl < F : File > Drain for FileBasedDrain < F > {
175
175
type Ok = ( ) ;
176
176
type Err = slog:: Never ;
177
177
@@ -186,13 +186,13 @@ impl<F:File> Drain for FileBasedDrain<F> {
186
186
record. msg( ) ,
187
187
values
188
188
) ;
189
- for rotator in self . rotators . iter ( ) {
190
- if rotator. should_rotate ( ) {
191
- self . flush ( ) . unwrap ( ) ;
192
- return Ok ( ( ) ) ;
193
- }
194
- }
195
-
189
+ for rotator in self . rotators . iter ( ) {
190
+ if rotator. should_rotate ( ) {
191
+ self . flush ( ) . unwrap ( ) ;
192
+ return Ok ( ( ) ) ;
193
+ }
194
+ }
195
+
196
196
for rotator in self . rotators . iter ( ) {
197
197
rotator. on_write ( by. as_bytes ( ) ) . unwrap ( ) ;
198
198
}
@@ -203,8 +203,6 @@ impl<F:File> Drain for FileBasedDrain<F> {
203
203
}
204
204
}
205
205
206
-
207
-
208
206
trait Rotator : Send {
209
207
/// Check if the option is enabled in configuration.
210
208
/// Return true if the `rotator` is valid.
@@ -239,10 +237,9 @@ impl Rotator for RotatedFileBySize {
239
237
fn is_enabled ( & self ) -> bool {
240
238
self . rotation_size != 0
241
239
}
242
- fn prepare ( & self , file : & dyn File ) -> Result < ( ) > {
240
+ fn prepare ( & self , file : & dyn File ) -> Result < ( ) > {
243
241
* self . file_size . lock ( ) . unwrap ( ) = file. len ( ) . unwrap ( ) ;
244
242
Ok ( ( ) )
245
-
246
243
}
247
244
248
245
fn should_rotate ( & self ) -> bool {
@@ -266,11 +263,11 @@ mod tests {
266
263
use crate :: storage:: mem:: MemStorage ;
267
264
268
265
use std:: thread;
269
- use std:: time:: Duration ;
270
-
266
+ use std:: time:: Duration ;
267
+
271
268
#[ test]
272
269
fn test_default_logger ( ) {
273
- let s =MemStorage :: default ( ) ;
270
+ let s = MemStorage :: default ( ) ;
274
271
// let s = &'static s;
275
272
let db_path = "test" ;
276
273
let logger = Logger :: new ( None , LevelFilter :: Debug , s, db_path. to_string ( ) ) ;
0 commit comments