Skip to content

Commit bec2e9b

Browse files
committed
refactor first
1 parent 7d450db commit bec2e9b

File tree

1 file changed

+38
-27
lines changed
  • opengrok-indexer/src/main/java/org/opengrok/indexer/util

1 file changed

+38
-27
lines changed

opengrok-indexer/src/main/java/org/opengrok/indexer/util/Progress.java

Lines changed: 38 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,19 @@
3131
import java.util.logging.Level;
3232
import java.util.logging.Logger;
3333

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+
*/
3440
public class Progress implements AutoCloseable {
3541
private final Logger logger;
3642
private final Long totalCount;
3743
private final String suffix;
3844

3945
private final AtomicLong currentCount = new AtomicLong();
46+
private final Map<Level, Integer> levelCount;
4047
private Thread loggerThread = null;
4148
private volatile boolean run;
4249

@@ -47,13 +54,7 @@ public class Progress implements AutoCloseable {
4754
* @param suffix string suffix to identify the operation
4855
*/
4956
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);
5758
}
5859

5960
/**
@@ -64,10 +65,20 @@ public Progress(Logger logger, String suffix) {
6465
public Progress(Logger logger, String suffix, long totalCount) {
6566
this.logger = logger;
6667
this.suffix = suffix;
67-
this.totalCount = totalCount;
6868

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+
6980
// Assuming printProgress configuration setting cannot be changed on the fly.
70-
if (totalCount > 0 && RuntimeEnvironment.getInstance().isPrintProgress()) {
81+
if (RuntimeEnvironment.getInstance().isPrintProgress()) {
7182
spawnLogThread();
7283
}
7384
}
@@ -102,10 +113,6 @@ public void increment() {
102113
private void logLoop() {
103114
long cachedCount = 0;
104115
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);
109116

110117
while (true) {
111118
long currentCount = this.currentCount.get();
@@ -128,20 +135,7 @@ private void logLoop() {
128135
}
129136
}
130137

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);
145139
}
146140

147141
if (!run) {
@@ -165,6 +159,23 @@ private void logLoop() {
165159
}
166160
}
167161

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+
168179
@Override
169180
public void close() {
170181
if (loggerThread == null) {

0 commit comments

Comments
 (0)