Skip to content

Commit

Permalink
#2: Added log redirection, even though it won't work
Browse files Browse the repository at this point in the history
  • Loading branch information
lptr committed Apr 17, 2014
1 parent ca9bcab commit d78768d
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 1 deletion.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.prezi.gradle.pride

import com.prezi.gradle.pride.internal.LoggerOutputStream
import org.gradle.tooling.GradleConnector
import org.gradle.tooling.model.gradle.GradleBuild
import org.slf4j.Logger
Expand Down Expand Up @@ -58,7 +59,12 @@ class PrideInitializer {
def relativePath = prideDirectory.toURI().relativize(moduleDirectory.toURI()).toString()

// Load the model for the build
GradleBuild build = connection.getModel(GradleBuild)
def builder = connection.model(GradleBuild)
// Redirect output to loggers
// Won't work until http://issues.gradle.org/browse/GRADLE-2687
builder.standardError = new LoggerOutputStream({ log.error("{}", it) })
builder.standardOutput = new LoggerOutputStream({ log.info("{}", it) })
GradleBuild build = builder.get()

// Merge settings
settingsFile << "\n// Settings from project in directory /${relativePath}\n\n"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package com.prezi.gradle.pride.internal

/**
* Based on Ant's LineOrientedOutputStream.
*/
public class LoggerOutputStream extends OutputStream {

private static final int CR = 0x0d
private static final int LF = 0x0a

private final def buffer = new ByteArrayOutputStream(256)
private boolean skip = false

private final Closure logLine

public LoggerOutputStream(Closure logLine) {
this.logLine = logLine
}

protected void processLine() {
try {
logLine(new String(buffer.toByteArray()))
} finally {
buffer.reset();
}
}

public void close() {
if (buffer.size() > 0) {
processLine()
}
super.close()
}

public final void write(int cc) {
final byte c = (byte) cc
if ((c == LF) || (c == CR)) {
if (!skip) {
processLine()
}
} else {
buffer.write(cc)
}
skip = (c == CR)
}

public final void write(byte[] b, int off, int len) throws IOException {
// find the line breaks and pass other chars through in blocks
int offset = off
int blockStartOffset = offset
int remaining = len
while (remaining > 0) {
while (remaining > 0 && b[offset] != LF && b[offset] != CR) {
offset++
remaining--
}
// either end of buffer or a line separator char
int blockLength = offset - blockStartOffset
if (blockLength > 0) {
buffer.write(b, blockStartOffset, blockLength)
}
while (remaining > 0 && (b[offset] == LF || b[offset] == CR)) {
write(b[offset])
offset++
remaining--
}
blockStartOffset = offset
}
}
}

0 comments on commit d78768d

Please sign in to comment.