@@ -21,7 +21,7 @@ pub struct Log {
21
21
id : Id ,
22
22
max : Option < usize > ,
23
23
unit : Option < Unit > ,
24
- step : usize ,
24
+ step : StepShared ,
25
25
current_level : usize ,
26
26
max_level : usize ,
27
27
trigger : Arc < AtomicBool > ,
@@ -50,13 +50,29 @@ impl Log {
50
50
current_level : 0 ,
51
51
max_level : max_level. unwrap_or ( usize:: MAX ) ,
52
52
max : None ,
53
- step : 0 ,
53
+ step : Default :: default ( ) ,
54
54
unit : None ,
55
55
trigger,
56
56
}
57
57
}
58
58
}
59
59
60
+ impl Log {
61
+ fn maybe_log ( & self ) {
62
+ if self . current_level > self . max_level {
63
+ return ;
64
+ }
65
+ let step = self . step ( ) ;
66
+ if self . trigger . swap ( false , Ordering :: Relaxed ) {
67
+ match ( self . max , & self . unit ) {
68
+ ( max, Some ( unit) ) => log:: info!( "{} → {}" , self . name, unit. display( step, max, None ) ) ,
69
+ ( Some ( max) , None ) => log:: info!( "{} → {} / {}" , self . name, step, max) ,
70
+ ( None , None ) => log:: info!( "{} → {}" , self . name, step) ,
71
+ }
72
+ }
73
+ }
74
+ }
75
+
60
76
impl Progress for Log {
61
77
type SubProgress = Log ;
62
78
@@ -70,37 +86,28 @@ impl Progress for Log {
70
86
id,
71
87
current_level : self . current_level + 1 ,
72
88
max_level : self . max_level ,
73
- step : 0 ,
89
+ step : Default :: default ( ) ,
74
90
max : None ,
75
91
unit : None ,
76
92
trigger : Arc :: clone ( & self . trigger ) ,
77
93
}
78
94
}
79
95
80
- fn init ( & mut self , max : Option < usize > , unit : Option < Unit > ) {
96
+ fn init ( & mut self , max : Option < Step > , unit : Option < Unit > ) {
81
97
self . max = max;
82
98
self . unit = unit;
83
99
}
84
100
85
- fn set ( & mut self , step : usize ) {
86
- self . step = step;
87
- if self . current_level > self . max_level {
88
- return ;
89
- }
90
- if self . trigger . swap ( false , Ordering :: Relaxed ) {
91
- match ( self . max , & self . unit ) {
92
- ( max, Some ( unit) ) => log:: info!( "{} → {}" , self . name, unit. display( step, max, None ) ) ,
93
- ( Some ( max) , None ) => log:: info!( "{} → {} / {}" , self . name, step, max) ,
94
- ( None , None ) => log:: info!( "{} → {}" , self . name, step) ,
95
- }
96
- }
101
+ fn set ( & mut self , step : Step ) {
102
+ self . step . store ( step, Ordering :: SeqCst ) ;
103
+ self . maybe_log ( )
97
104
}
98
105
99
106
fn unit ( & self ) -> Option < Unit > {
100
107
self . unit . clone ( )
101
108
}
102
109
103
- fn max ( & self ) -> Option < usize > {
110
+ fn max ( & self ) -> Option < Step > {
104
111
self . max
105
112
}
106
113
@@ -111,11 +118,12 @@ impl Progress for Log {
111
118
}
112
119
113
120
fn step ( & self ) -> usize {
114
- self . step
121
+ self . step . load ( Ordering :: Relaxed )
115
122
}
116
123
117
- fn inc_by ( & mut self , step : usize ) {
118
- self . set ( self . step + step)
124
+ fn inc_by ( & mut self , step : Step ) {
125
+ self . step . fetch_add ( step, Ordering :: SeqCst ) ;
126
+ self . maybe_log ( )
119
127
}
120
128
121
129
fn set_name ( & mut self , name : impl Into < String > ) {
@@ -146,6 +154,6 @@ impl Progress for Log {
146
154
}
147
155
148
156
fn counter ( & self ) -> Option < StepShared > {
149
- None
157
+ Some ( self . step . clone ( ) )
150
158
}
151
159
}
0 commit comments