Skip to content

Commit b3d8b39

Browse files
committed
Convert logging related gradle classes to java (#44771)
This commit converts the logging related classes (only used for vagrant) to java from groovy. relates #34459
1 parent 8bac13d commit b3d8b39

File tree

8 files changed

+279
-284
lines changed

8 files changed

+279
-284
lines changed

buildSrc/src/main/groovy/org/elasticsearch/gradle/LoggingOutputStream.groovy

-64
This file was deleted.

buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/BatsOverVagrantTask.groovy

+1-4
Original file line numberDiff line numberDiff line change
@@ -40,9 +40,6 @@ public class BatsOverVagrantTask extends VagrantCommandTask {
4040

4141
@Override
4242
protected OutputStream createLoggerOutputStream() {
43-
return new TapLoggerOutputStream(
44-
command: commandLine.join(' '),
45-
factory: getProgressLoggerFactory(),
46-
logger: logger)
43+
return new TapLoggerOutputStream(logger, getProgressLoggerFactory().newOperation(boxName).setDescription(boxName));
4744
}
4845
}

buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/TapLoggerOutputStream.groovy

-111
This file was deleted.

buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantCommandTask.groovy

+2-4
Original file line numberDiff line numberDiff line change
@@ -78,11 +78,9 @@ public class VagrantCommandTask extends LoggedExec {
7878
}
7979

8080
protected OutputStream createLoggerOutputStream() {
81-
return new VagrantLoggerOutputStream(
82-
command: commandLine.join(' '),
83-
factory: getProgressLoggerFactory(),
81+
return new VagrantLoggerOutputStream(getProgressLoggerFactory().newOperation(boxName + " " + command).setDescription(boxName),
8482
/* Vagrant tends to output a lot of stuff, but most of the important
8583
stuff starts with ==> $box */
86-
squashedPrefix: "==> $boxName: ")
84+
"==> $boxName: ")
8785
}
8886
}

buildSrc/src/main/groovy/org/elasticsearch/gradle/vagrant/VagrantLoggerOutputStream.groovy

-101
This file was deleted.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
/*
2+
* Licensed to Elasticsearch under one or more contributor
3+
* license agreements. See the NOTICE file distributed with
4+
* this work for additional information regarding copyright
5+
* ownership. Elasticsearch licenses this file to you under
6+
* the Apache License, Version 2.0 (the "License"); you may
7+
* not use this file except in compliance with the License.
8+
* You may obtain a copy of the License at
9+
*
10+
* http://www.apache.org/licenses/LICENSE-2.0
11+
*
12+
* Unless required by applicable law or agreed to in writing,
13+
* software distributed under the License is distributed on an
14+
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15+
* KIND, either express or implied. See the License for the
16+
* specific language governing permissions and limitations
17+
* under the License.
18+
*/
19+
20+
package org.elasticsearch.gradle;
21+
22+
import java.io.IOException;
23+
import java.io.OutputStream;
24+
import java.util.Arrays;
25+
26+
/**
27+
* Writes data passed to this stream as log messages.
28+
*
29+
* The stream will be flushed whenever a newline is detected.
30+
* Allows setting an optional prefix before each line of output.
31+
*/
32+
public abstract class LoggingOutputStream extends OutputStream {
33+
/** The starting length of the buffer */
34+
private static final int DEFAULT_BUFFER_LENGTH = 4096;
35+
36+
/** The buffer of bytes sent to the stream */
37+
private byte[] buffer = new byte[DEFAULT_BUFFER_LENGTH];
38+
39+
/** Offset of the start of unwritten data in the buffer */
40+
private int start = 0;
41+
42+
/** Offset of the end (semi-open) of unwritten data in the buffer */
43+
private int end = 0;
44+
45+
@Override
46+
public void write(final int b) throws IOException {
47+
if (b == 0) return;
48+
if (b == '\n') {
49+
// always flush with newlines instead of adding to the buffer
50+
flush();
51+
return;
52+
}
53+
54+
if (end == buffer.length) {
55+
if (start != 0) {
56+
// first try shifting the used buffer back to the beginning to make space
57+
int len = end - start;
58+
System.arraycopy(buffer, start, buffer, 0, len);
59+
start = 0;
60+
end = len;
61+
} else {
62+
// otherwise extend the buffer
63+
buffer = Arrays.copyOf(buffer, buffer.length + DEFAULT_BUFFER_LENGTH);
64+
}
65+
}
66+
67+
buffer[end++] = (byte) b;
68+
}
69+
70+
@Override
71+
public void flush() {
72+
if (end == start) return;
73+
logLine(new String(buffer, start, end - start));
74+
start = end;
75+
}
76+
77+
protected abstract void logLine(String line);
78+
}

0 commit comments

Comments
 (0)