@@ -12,7 +12,7 @@ use std::{
12
12
time:: Instant ,
13
13
} ;
14
14
15
- /// Allows logging to a memory buffer with limited size.
15
+ // Allows logging to a memory buffer with limited size.
16
16
pub struct BufferWriter {
17
17
state : Arc < Mutex < State > > ,
18
18
}
@@ -24,29 +24,6 @@ struct State {
24
24
format : FormatFunction ,
25
25
}
26
26
27
- /// Allows getting the current content of the memory buffer.
28
- #[ derive( Clone ) ]
29
- pub struct Snapshot {
30
- /// The latest snapshot of the memory buffer.
31
- pub text : String ,
32
- last_update : Instant ,
33
- }
34
- impl Snapshot {
35
- /// Constructor.
36
- #[ must_use]
37
- pub fn new ( ) -> Self {
38
- Self {
39
- text : String :: new ( ) ,
40
- last_update : Instant :: now ( ) ,
41
- }
42
- }
43
- }
44
- impl Default for Snapshot {
45
- fn default ( ) -> Self {
46
- Self :: new ( )
47
- }
48
- }
49
-
50
27
impl BufferWriter {
51
28
/// Create a new instance.
52
29
pub fn new ( max_size : usize , format : FormatFunction ) -> Self {
@@ -61,7 +38,7 @@ impl BufferWriter {
61
38
}
62
39
}
63
40
64
- fn lock_inner ( & self ) -> Result < std:: sync:: MutexGuard < ' _ , State > , std:: io:: Error > {
41
+ fn lock_state ( & self ) -> Result < std:: sync:: MutexGuard < ' _ , State > , std:: io:: Error > {
65
42
self . state
66
43
. lock ( )
67
44
. map_err ( |e| std:: io:: Error :: other ( e. to_string ( ) ) )
@@ -77,41 +54,46 @@ impl BufferWriter {
77
54
///
78
55
/// `FlexiLoggerError::Poison` if some mutex is poisoned.
79
56
pub fn update_snapshot ( & self , snapshot : & mut Snapshot ) -> Result < bool , FlexiLoggerError > {
80
- let inner = self . lock_inner ( ) ?;
81
- if snapshot. last_update == inner . last_update {
57
+ let state = self . lock_state ( ) ?;
58
+ if snapshot. last_update == state . last_update {
82
59
Ok ( false )
83
60
} else {
84
61
snapshot. text . clear ( ) ;
85
- for bufline in & inner . buffer {
62
+ for bufline in & state . buffer {
86
63
snapshot. text . push_str ( bufline) ;
87
64
snapshot. text . push ( '\n' ) ;
88
65
}
89
- snapshot. last_update = inner . last_update ;
66
+ snapshot. last_update = state . last_update ;
90
67
Ok ( true )
91
68
}
92
69
}
93
70
}
94
71
impl LogWriter for BufferWriter {
95
72
fn write ( & self , now : & mut DeferredNow , record : & Record ) -> std:: io:: Result < ( ) > {
96
- let mut inner = self . lock_inner ( ) ?;
73
+ let mut state = self . lock_state ( ) ?;
97
74
98
75
let mut logline = Vec :: < u8 > :: with_capacity ( 80 ) ;
99
- ( inner . format ) ( & mut logline, now, record) . inspect_err ( |e| {
76
+ ( state . format ) ( & mut logline, now, record) . inspect_err ( |e| {
100
77
eprint_err ( ErrorCode :: Format , "formatting failed" , & e) ;
101
78
} ) ?;
102
79
103
80
if !logline. is_empty ( ) {
104
- while inner. size + logline. len ( ) > inner. max_size {
105
- if let Some ( line) = inner. buffer . pop_front ( ) {
106
- inner. size -= line. len ( ) ;
81
+ if logline. len ( ) > state. max_size {
82
+ state. buffer . clear ( ) ;
83
+ state. size = 0 ;
84
+ } else {
85
+ while state. size + logline. len ( ) > state. max_size {
86
+ if let Some ( line) = state. buffer . pop_front ( ) {
87
+ state. size -= line. len ( ) ;
88
+ }
107
89
}
108
90
}
109
91
110
- ( inner )
92
+ ( state )
111
93
. buffer
112
94
. push_back ( String :: from_utf8_lossy ( & logline) . to_string ( ) ) ;
113
- inner . size += logline. len ( ) ;
114
- inner . last_update = Instant :: now ( ) ;
95
+ state . size += logline. len ( ) ;
96
+ state . last_update = Instant :: now ( ) ;
115
97
}
116
98
Ok ( ( ) )
117
99
}
@@ -121,3 +103,26 @@ impl LogWriter for BufferWriter {
121
103
Ok ( ( ) )
122
104
}
123
105
}
106
+
107
+ /// Allows getting the current content of the memory buffer.
108
+ #[ derive( Clone ) ]
109
+ pub struct Snapshot {
110
+ /// The latest snapshot of the memory buffer.
111
+ pub text : String ,
112
+ last_update : Instant ,
113
+ }
114
+ impl Snapshot {
115
+ /// Constructor.
116
+ #[ must_use]
117
+ pub fn new ( ) -> Self {
118
+ Self {
119
+ text : String :: new ( ) ,
120
+ last_update : Instant :: now ( ) ,
121
+ }
122
+ }
123
+ }
124
+ impl Default for Snapshot {
125
+ fn default ( ) -> Self {
126
+ Self :: new ( )
127
+ }
128
+ }
0 commit comments