@@ -75,6 +75,11 @@ Executor setCommandLine(String... cmdline) {
75
75
return setProcessBuilder (new ProcessBuilder (cmdline ));
76
76
}
77
77
78
+ Executor setQuiet (boolean v ) {
79
+ quietCommand = v ;
80
+ return this ;
81
+ }
82
+
78
83
List <String > getOutput () {
79
84
return output ;
80
85
}
@@ -84,7 +89,7 @@ Executor executeExpectSuccess() throws IOException {
84
89
if (0 != ret ) {
85
90
throw new IOException (
86
91
String .format ("Command %s exited with %d code" ,
87
- createLogMessage (pb ), ret ));
92
+ createLogMessage (pb , false ), ret ));
88
93
}
89
94
return this ;
90
95
}
@@ -108,7 +113,7 @@ int execute() throws IOException {
108
113
pb .redirectOutput (ProcessBuilder .Redirect .DISCARD );
109
114
}
110
115
111
- Log .verbose (String .format ("Running %s" , createLogMessage (pb )));
116
+ Log .verbose (String .format ("Running %s" , createLogMessage (pb , true )));
112
117
Process p = pb .start ();
113
118
114
119
int code = 0 ;
@@ -126,47 +131,35 @@ int execute() throws IOException {
126
131
Supplier <Stream <String >> outputStream ;
127
132
128
133
if (writeOutputToFile ) {
129
- savedOutput = Files .readAllLines (outputFile );
134
+ output = savedOutput = Files .readAllLines (outputFile );
130
135
Files .delete (outputFile );
131
136
outputStream = () -> {
132
137
if (savedOutput != null ) {
133
138
return savedOutput .stream ();
134
139
}
135
140
return null ;
136
141
};
137
-
138
- if (Log .isVerbose ()) {
139
- outputStream .get ().forEach (Log ::verbose );
140
- }
141
-
142
142
if (outputConsumer != null ) {
143
143
outputConsumer .accept (outputStream .get ());
144
144
}
145
145
} else {
146
146
try (var br = new BufferedReader (new InputStreamReader (
147
147
p .getInputStream ()))) {
148
- // Need to save output if explicitely requested (saveOutput=true) or
149
- // if will be used used by multiple consumers
150
- if (( outputConsumer != null && Log . isVerbose ()) || saveOutput ) {
148
+
149
+ if (( outputConsumer != null || Log . isVerbose ())
150
+ || saveOutput ) {
151
151
savedOutput = br .lines ().collect (Collectors .toList ());
152
- if (saveOutput ) {
153
- output = savedOutput ;
154
- }
155
152
} else {
156
153
savedOutput = null ;
157
154
}
155
+ output = savedOutput ;
158
156
159
157
outputStream = () -> {
160
158
if (savedOutput != null ) {
161
159
return savedOutput .stream ();
162
160
}
163
161
return br .lines ();
164
162
};
165
-
166
- if (Log .isVerbose ()) {
167
- outputStream .get ().forEach (Log ::verbose );
168
- }
169
-
170
163
if (outputConsumer != null ) {
171
164
outputConsumer .accept (outputStream .get ());
172
165
}
@@ -188,6 +181,9 @@ int execute() throws IOException {
188
181
if (!writeOutputToFile ) {
189
182
code = p .waitFor ();
190
183
}
184
+ if (!quietCommand ) {
185
+ Log .verbose (pb .command (), getOutput (), code );
186
+ }
191
187
return code ;
192
188
} catch (InterruptedException ex ) {
193
189
Log .verbose (ex );
@@ -203,7 +199,7 @@ private int waitForProcess(Process p) throws InterruptedException {
203
199
return p .exitValue ();
204
200
} else {
205
201
Log .verbose (String .format ("Command %s timeout after %d seconds" ,
206
- createLogMessage (pb ), timeout ));
202
+ createLogMessage (pb , false ), timeout ));
207
203
p .destroy ();
208
204
return -1 ;
209
205
}
@@ -218,9 +214,9 @@ static Executor of(ProcessBuilder pb) {
218
214
return new Executor ().setProcessBuilder (pb );
219
215
}
220
216
221
- private static String createLogMessage (ProcessBuilder pb ) {
217
+ private static String createLogMessage (ProcessBuilder pb , boolean quiet ) {
222
218
StringBuilder sb = new StringBuilder ();
223
- sb .append (String . format ( "%s" , pb .command ()));
219
+ sb .append (( quiet ) ? pb .command (). get ( 0 ) : pb . command ( ));
224
220
if (pb .directory () != null ) {
225
221
sb .append (String .format ("in %s" , pb .directory ().getAbsolutePath ()));
226
222
}
@@ -232,6 +228,7 @@ private static String createLogMessage(ProcessBuilder pb) {
232
228
private ProcessBuilder pb ;
233
229
private boolean saveOutput ;
234
230
private boolean writeOutputToFile ;
231
+ private boolean quietCommand ;
235
232
private long timeout = INFINITE_TIMEOUT ;
236
233
private List <String > output ;
237
234
private Consumer <Stream <String >> outputConsumer ;
0 commit comments