31
31
import java .util .logging .Level ;
32
32
import java .util .logging .Logger ;
33
33
34
+ /**
35
+ * Progress reporting via logging. The idea is that for anything that has a set of items
36
+ * to go through, it will ping an instance of this class for each item completed.
37
+ * This class will then log based on the number of pings. The bigger the progress,
38
+ * the higher log level ({@link Level} value) will be used.
39
+ */
34
40
public class Progress implements AutoCloseable {
35
41
private final Logger logger ;
36
42
private final Long totalCount ;
37
43
private final String suffix ;
38
44
39
45
private final AtomicLong currentCount = new AtomicLong ();
46
+ private final Map <Level , Integer > levelCount ;
40
47
private Thread loggerThread = null ;
41
48
private volatile boolean run ;
42
49
@@ -47,13 +54,7 @@ public class Progress implements AutoCloseable {
47
54
* @param suffix string suffix to identify the operation
48
55
*/
49
56
public Progress (Logger logger , String suffix ) {
50
- this .logger = logger ;
51
- this .suffix = suffix ;
52
- this .totalCount = null ;
53
-
54
- if (RuntimeEnvironment .getInstance ().isPrintProgress ()) {
55
- spawnLogThread ();
56
- }
57
+ this (logger , suffix , -1 );
57
58
}
58
59
59
60
/**
@@ -64,10 +65,20 @@ public Progress(Logger logger, String suffix) {
64
65
public Progress (Logger logger , String suffix , long totalCount ) {
65
66
this .logger = logger ;
66
67
this .suffix = suffix ;
67
- this .totalCount = totalCount ;
68
68
69
+ if (totalCount < 0 ) {
70
+ this .totalCount = null ;
71
+ } else {
72
+ this .totalCount = totalCount ;
73
+ }
74
+
75
+ levelCount = Map .of (Level .INFO , 100 ,
76
+ Level .FINE , 50 ,
77
+ Level .FINER , 10 ,
78
+ Level .FINEST , 1 );
79
+
69
80
// Assuming printProgress configuration setting cannot be changed on the fly.
70
- if (totalCount > 0 && RuntimeEnvironment .getInstance ().isPrintProgress ()) {
81
+ if (RuntimeEnvironment .getInstance ().isPrintProgress ()) {
71
82
spawnLogThread ();
72
83
}
73
84
}
@@ -102,10 +113,6 @@ public void increment() {
102
113
private void logLoop () {
103
114
long cachedCount = 0 ;
104
115
Map <Level , Long > lastLoggedChunk = new HashMap <>();
105
- final Map <Level , Integer > levelCount = Map .of (Level .INFO , 100 ,
106
- Level .FINE , 50 ,
107
- Level .FINER , 10 ,
108
- Level .FINEST , 1 );
109
116
110
117
while (true ) {
111
118
long currentCount = this .currentCount .get ();
@@ -128,20 +135,7 @@ private void logLoop() {
128
135
}
129
136
}
130
137
131
- if (logger .isLoggable (currentLevel )) {
132
- lastLoggedChunk .put (currentLevel , currentCount / levelCount .get (currentLevel ));
133
- StringBuilder stringBuilder = new StringBuilder ();
134
- stringBuilder .append ("Progress: " );
135
- stringBuilder .append (currentCount );
136
- stringBuilder .append (" " );
137
- if (totalCount != null ) {
138
- stringBuilder .append ("(" );
139
- stringBuilder .append (String .format ("%.2f" , currentCount * 100.0f / totalCount ));
140
- stringBuilder .append ("%) " );
141
- }
142
- stringBuilder .append (suffix );
143
- logger .log (currentLevel , stringBuilder .toString ());
144
- }
138
+ logIt (lastLoggedChunk , currentCount , currentLevel );
145
139
}
146
140
147
141
if (!run ) {
@@ -165,6 +159,23 @@ private void logLoop() {
165
159
}
166
160
}
167
161
162
+ private void logIt (Map <Level , Long > lastLoggedChunk , long currentCount , Level currentLevel ) {
163
+ if (logger .isLoggable (currentLevel )) {
164
+ lastLoggedChunk .put (currentLevel , currentCount / levelCount .get (currentLevel ));
165
+ StringBuilder stringBuilder = new StringBuilder ();
166
+ stringBuilder .append ("Progress: " );
167
+ stringBuilder .append (currentCount );
168
+ stringBuilder .append (" " );
169
+ if (totalCount != null ) {
170
+ stringBuilder .append ("(" );
171
+ stringBuilder .append (String .format ("%.2f" , currentCount * 100.0f / totalCount ));
172
+ stringBuilder .append ("%) " );
173
+ }
174
+ stringBuilder .append (suffix );
175
+ logger .log (currentLevel , stringBuilder .toString ());
176
+ }
177
+ }
178
+
168
179
@ Override
169
180
public void close () {
170
181
if (loggerThread == null ) {
0 commit comments