Skip to content

Commit eb8d432

Browse files
committed
Convert logging related gradle classes to java
This commit converts the logging related classes (only used for vagrant) to java from groovy. relates elastic#34459
1 parent 343bb08 commit eb8d432

File tree

8 files changed

+277
-284
lines changed

8 files changed

+277
-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,77 @@
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+
25+
/**
26+
* Writes data passed to this stream as log messages.
27+
*
28+
* The stream will be flushed whenever a newline is detected.
29+
* Allows setting an optional prefix before each line of output.
30+
*/
31+
public abstract class LoggingOutputStream extends OutputStream {
32+
/** The starting length of the buffer */
33+
private static final int DEFAULT_BUFFER_LENGTH = 4096;
34+
35+
/** The buffer of bytes sent to the stream */
36+
private byte[] buffer = new byte[DEFAULT_BUFFER_LENGTH];
37+
38+
/** Offset of the start of unwritten data in the buffer */
39+
private int start = 0;
40+
41+
/** Offset of the end (semi-open) of unwritten data in the buffer */
42+
private int end = 0;
43+
44+
@Override
45+
public void write(final int b) throws IOException {
46+
if (b == 0) return;
47+
if (b == '\n') {
48+
// always flush with newlines instead of adding to the buffer
49+
flush();
50+
return;
51+
}
52+
53+
if (end == buffer.length) {
54+
if (start != 0) {
55+
// first try shifting the used buffer back to the beginning to make space
56+
System.arraycopy(buffer, start, buffer, 0, end - start);
57+
} else {
58+
// otherwise extend the buffer
59+
final int newBufferLength = buffer.length + DEFAULT_BUFFER_LENGTH;
60+
final byte[] newBuffer = new byte[newBufferLength];
61+
System.arraycopy(buffer, 0, newBuffer, 0, buffer.length);
62+
buffer = newBuffer;
63+
}
64+
}
65+
66+
buffer[end++] = (byte) b;
67+
}
68+
69+
@Override
70+
public void flush() {
71+
if (end == start) return;
72+
logLine(new String(buffer, start, end - start));
73+
start = end;
74+
}
75+
76+
protected abstract void logLine(String line);
77+
}

0 commit comments

Comments
 (0)