18
18
*/
19
19
20
20
/*
21
- * Copyright (c) 2007, 2021 , Oracle and/or its affiliates. All rights reserved.
21
+ * Copyright (c) 2007, 2023 , Oracle and/or its affiliates. All rights reserved.
22
22
* Portions Copyright (c) 2017, 2020, Chris Fraire <[email protected] >.
23
23
*/
24
24
package org .opengrok .indexer .util ;
33
33
34
34
public class Progress implements AutoCloseable {
35
35
private final Logger logger ;
36
- private final long totalCount ;
36
+ private final Long totalCount ;
37
37
private final String suffix ;
38
38
39
39
private final AtomicLong currentCount = new AtomicLong ();
@@ -42,6 +42,20 @@ public class Progress implements AutoCloseable {
42
42
43
43
private final Object sync = new Object ();
44
44
45
+ /**
46
+ * @param logger logger instance
47
+ * @param suffix string suffix to identify the operation
48
+ */
49
+ 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
+ }
58
+
45
59
/**
46
60
* @param logger logger instance
47
61
* @param suffix string suffix to identify the operation
@@ -54,14 +68,18 @@ public Progress(Logger logger, String suffix, long totalCount) {
54
68
55
69
// Assuming printProgress configuration setting cannot be changed on the fly.
56
70
if (totalCount > 0 && RuntimeEnvironment .getInstance ().isPrintProgress ()) {
57
- // spawn a logger thread.
58
- run = true ;
59
- loggerThread = new Thread (this ::logLoop ,
60
- "progress-thread-" + suffix .replaceAll (" " , "_" ));
61
- loggerThread .start ();
71
+ spawnLogThread ();
62
72
}
63
73
}
64
74
75
+ private void spawnLogThread () {
76
+ // spawn a logger thread.
77
+ run = true ;
78
+ loggerThread = new Thread (this ::logLoop ,
79
+ "progress-thread-" + suffix .replaceAll (" " , "_" ));
80
+ loggerThread .start ();
81
+ }
82
+
65
83
// for testing
66
84
Thread getLoggerThread () {
67
85
return loggerThread ;
@@ -84,7 +102,7 @@ public void increment() {
84
102
private void logLoop () {
85
103
long cachedCount = 0 ;
86
104
Map <Level , Long > lastLoggedChunk = new HashMap <>();
87
- Map <Level , Integer > levelCount = Map .of (Level .INFO , 100 ,
105
+ final Map <Level , Integer > levelCount = Map .of (Level .INFO , 100 ,
88
106
Level .FINE , 50 ,
89
107
Level .FINER , 10 ,
90
108
Level .FINEST , 1 );
@@ -95,7 +113,7 @@ private void logLoop() {
95
113
96
114
// Do not log if there was no progress.
97
115
if (cachedCount < currentCount ) {
98
- if (currentCount <= 1 || currentCount == totalCount ) {
116
+ if (currentCount <= 1 || ( totalCount != null && currentCount == totalCount ) ) {
99
117
currentLevel = Level .INFO ;
100
118
} else {
101
119
if (lastLoggedChunk .getOrDefault (Level .INFO , -1L ) <
@@ -112,8 +130,17 @@ private void logLoop() {
112
130
113
131
if (logger .isLoggable (currentLevel )) {
114
132
lastLoggedChunk .put (currentLevel , currentCount / levelCount .get (currentLevel ));
115
- logger .log (currentLevel , "Progress: {0} ({1}%) for {2}" ,
116
- new Object []{currentCount , currentCount * 100.0f / totalCount , suffix });
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 ());
117
144
}
118
145
}
119
146
0 commit comments