1
0
Fork 0
mirror of https://github.com/eclipse-cdt/cdt synced 2025-04-29 19:45:01 +02:00

improve build ouptut console streaming

This commit is contained in:
David Inglis 2004-07-14 01:26:22 +00:00
parent 425adf04b8
commit 6144b2233d
6 changed files with 304 additions and 277 deletions

View file

@ -1,14 +1,11 @@
package org.eclipse.cdt.core;
/*
* (c) Copyright IBM Corp. 2000, 2001.
* All Rights Reserved.
* (c) Copyright IBM Corp. 2000, 2001. All Rights Reserved.
*/
import java.io.IOException;
import java.io.OutputStream;
import java.io.PipedInputStream;
import java.io.PipedOutputStream;
import java.util.Properties;
import org.eclipse.cdt.internal.core.ProcessClosure;
@ -17,7 +14,6 @@ import org.eclipse.cdt.utils.spawner.ProcessFactory;
import org.eclipse.core.runtime.IPath;
import org.eclipse.core.runtime.IProgressMonitor;
public class CommandLauncher {
public final static int COMMAND_CANCELED = 1;
@ -33,15 +29,14 @@ public class CommandLauncher {
private String lineSeparator;
/**
* The number of milliseconds to pause
* between polling.
* The number of milliseconds to pause between polling.
*/
protected static final long DELAY = 50L;
/**
* Creates a new launcher
* Fills in stderr and stdout output to the given streams.
* Streams can be set to <code>null</code>, if output not required
* Creates a new launcher Fills in stderr and stdout output to the given
* streams. Streams can be set to <code>null</code>, if output not
* required
*/
public CommandLauncher() {
fProcess = null;
@ -125,10 +120,10 @@ public class CommandLauncher {
}
/**
* Reads output form the process to the streams. A progress monitor is polled to
* test if the cancel button has been pressed.
* Destroys the process if the monitor becomes canceled
* override to implement a different way to read the process inputs
* Reads output form the process to the streams. A progress monitor is
* polled to test if the cancel button has been pressed. Destroys the
* process if the monitor becomes canceled override to implement a different
* way to read the process inputs
*/
public int waitAndRead(OutputStream output, OutputStream err, IProgressMonitor monitor) {
if (fShowCommand) {
@ -139,55 +134,13 @@ public class CommandLauncher {
return ILLEGAL_COMMAND;
}
PipedOutputStream errOutPipe = new PipedOutputStream();
PipedOutputStream outputPipe = new PipedOutputStream();
PipedInputStream errInPipe, inputPipe;
try {
errInPipe = new PipedInputStream(errOutPipe);
inputPipe = new PipedInputStream(outputPipe);
} catch( IOException e ) {
setErrorMessage(CCorePlugin.getResourceString("CommandLauncher.error.commandCanceled")); //$NON-NLS-1$
return COMMAND_CANCELED;
}
ProcessClosure closure= new ProcessClosure(fProcess, outputPipe, errOutPipe);
ProcessClosure closure = new ProcessClosure(fProcess, output, err);
closure.runNonBlocking();
byte buffer[] = new byte[1024];
int nbytes;
int waited = 0;
boolean flushed = false;
while (!monitor.isCanceled() && closure.isAlive()) {
nbytes = 0;
try {
if ( errInPipe.available() > 0 ) {
nbytes = errInPipe.read(buffer);
err.write(buffer, 0, nbytes);
}
if ( inputPipe.available() > 0 ) {
nbytes = inputPipe.read(buffer);
output.write(buffer, 0, nbytes);
}
} catch( IOException e) {
}
monitor.worked(0);
if (nbytes == 0) {
//DELAY * 10 is half a second with no new input, flush anything thats waiting
if( !flushed && waited > DELAY * 10 ){
try {
err.flush();
output.flush();
flushed = true;
} catch (IOException e1) {
}
waited = 0;
}
try {
waited += DELAY;
Thread.sleep(DELAY);
} catch (InterruptedException ie) {
}
} else {
flushed = false;
// ignore
}
}
@ -203,29 +156,8 @@ public class CommandLauncher {
try {
fProcess.waitFor();
} catch (InterruptedException e) {
//System.err.println("Closure exception " +e);
//e.printStackTrace();
// ignore
}
// Drain the pipes.
try {
while (errInPipe.available() > 0 || inputPipe.available() > 0) {
if ( errInPipe.available() > 0 ) {
nbytes = errInPipe.read(buffer);
err.write(buffer, 0, nbytes);
err.flush();
}
if ( inputPipe.available() > 0 ) {
nbytes = inputPipe.read(buffer);
output.write(buffer, 0, nbytes);
output.flush();
}
}
errInPipe.close();
inputPipe.close();
} catch (IOException e) {
}
return state;
}

View file

@ -6,23 +6,20 @@ package org.eclipse.cdt.internal.core;
*/
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
/**
* Bundled state of a launched process including the threads linking the process in/output
* to console documents.
* Bundled state of a launched process including the threads linking the process
* in/output to console documents.
*/
public class ProcessClosure {
/**
* Thread which continuously reads from a input stream and pushes the read data
* to an output stream which is immediately flushed afterwards.
* Thread which continuously reads from a input stream and pushes the read
* data to an output stream which is immediately flushed afterwards.
*/
protected static class ReaderThread extends Thread {
@ -42,29 +39,32 @@ public class ProcessClosure {
}
public void run() {
byte[] buffer = new byte[1024];
int len;
try {
try {
BufferedReader reader = new BufferedReader(new InputStreamReader(fInputStream));
BufferedWriter writer = new BufferedWriter(new OutputStreamWriter(fOutputStream));
String line;
while ((line = reader.readLine()) != null) {
line += lineSeparator;
char[] array = line.toCharArray();
writer.write(array, 0, array.length);
writer.flush();
fOutputStream.write(line.getBytes());
}
} catch (IOException x) {
// ignore
} finally {
try {
fInputStream.close();
// writer.flush();
fOutputStream.flush();
} catch (IOException e) {
// ignore
}
try {
fOutputStream.close();
fInputStream.close();
} catch (IOException e) {
// ignore
}
}
} finally {
complete();
}
}
@ -86,6 +86,14 @@ public class ProcessClosure {
fFinished = true;
notify();
}
public void close() {
try {
fOutputStream.close();
} catch (IOException e) {
// ignore
}
}
}
protected static int fCounter = 0;
@ -99,12 +107,15 @@ public class ProcessClosure {
protected ReaderThread fErrorReader;
/**
* Creates a process closure and connects the launched process with
* a console document.
* @param outputStream prcess stdout is written to this stream. Can be <code>null</code>, if
* not interested in reading the output
* @param errorStream prcess stderr is written to this stream. Can be <code>null</code>, if
* not interested in reading the output
* Creates a process closure and connects the launched process with a
* console document.
*
* @param outputStream
* prcess stdout is written to this stream. Can be
* <code>null</code>, if not interested in reading the output
* @param errorStream
* prcess stderr is written to this stream. Can be
* <code>null</code>, if not interested in reading the output
*/
public ProcessClosure(Process process, OutputStream outputStream, OutputStream errorStream) {
fProcess = process;
@ -156,24 +167,27 @@ public class ProcessClosure {
fErrorReader.waitFor();
}
// it seems that thread termination and stream closing is working without
fOutputReader.close();
fErrorReader.close();
// it seems that thread termination and stream closing is working
// without
// any help
fProcess = null;
fOutputReader = null;
fErrorReader = null;
}
public boolean isAlive() {
if (fProcess != null) {
if (fOutputReader.isAlive() || fErrorReader.isAlive()) {
return true;
} else {
}
fProcess = null;
fOutputReader.close();
fErrorReader.close();
fOutputReader = null;
fErrorReader = null;
}
}
return false;
}
@ -185,5 +199,15 @@ public class ProcessClosure {
fProcess.destroy();
fProcess = null;
}
if (!fOutputReader.finished()) {
fOutputReader.waitFor();
}
if (!fErrorReader.finished()) {
fErrorReader.waitFor();
}
fOutputReader.close();
fErrorReader.close();
fOutputReader = null;
fErrorReader = null;
}
}

View file

@ -79,9 +79,6 @@ public class BuildConsoleManager implements IBuildConsoleManager, IResourceChang
* front.
*/
protected void showConsole() {
CUIPlugin.getStandardDisplay().asyncExec(new Runnable() {
public void run() {
IWorkbenchWindow window = CUIPlugin.getActiveWorkbenchWindow();
if (window != null) {
IWorkbenchPage page = window.getActivePage();
@ -109,8 +106,6 @@ public class BuildConsoleManager implements IBuildConsoleManager, IResourceChang
}
}
}
});
}
boolean shouldBringToTop(IViewPart consoleView) {
boolean bringToTop = false;

View file

@ -117,21 +117,15 @@ public class BuildConsolePage extends Page implements ISelectionListener, IPrope
public void consoleChange(IBuildConsoleEvent event) {
if (event.getType() == IBuildConsoleEvent.CONSOLE_START || event.getType() == IBuildConsoleEvent.CONSOLE_CLOSE) {
if (isAvailable()) {
Display display = getControl().getDisplay();
if (event.getType() == IBuildConsoleEvent.CONSOLE_CLOSE && getProject() != event.getProject()) {
return;
}
setProject(event.getProject());
display.asyncExec(new Runnable() {
public void run() {
if (isAvailable()) {
setDocument();
getConsole().setTitle(getProject());
}
}
});
}
}
}
@ -233,8 +227,7 @@ public class BuildConsolePage extends Page implements ISelectionListener, IPrope
action.setImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
action.setDisabledImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
ISharedImages.IMG_TOOL_COPY_DISABLED));
action.setHoverImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(
ISharedImages.IMG_TOOL_COPY));
action.setHoverImageDescriptor(PlatformUI.getWorkbench().getSharedImages().getImageDescriptor(ISharedImages.IMG_TOOL_COPY));
setGlobalAction(actionBars, ActionFactory.COPY.getId(), action);
action = new TextViewerAction(getViewer(), ITextOperationTarget.SELECT_ALL);
action.configureAction(ConsoleMessages.getString("BuildConsolePage.Select_&All@Ctrl+A_12"), //$NON-NLS-1$

View file

@ -12,6 +12,7 @@ import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Vector;
import org.eclipse.cdt.core.ConsoleOutputStream;
import org.eclipse.cdt.core.resources.IConsole;
@ -30,6 +31,7 @@ import org.eclipse.jface.text.Region;
import org.eclipse.jface.util.IPropertyChangeListener;
import org.eclipse.jface.util.PropertyChangeEvent;
import org.eclipse.swt.widgets.Display;
import org.eclipse.ui.console.ConsolePlugin;
public class BuildConsolePartitioner
implements
@ -54,6 +56,55 @@ public class BuildConsolePartitioner
boolean killed;
BuildConsoleManager fManager;
/**
* A queue of stream entries written to standard out and standard err.
* Entries appended to the end of the queue and removed from the front.
* Intentionally a vector to obtain synchronization as entries are added and
* removed.
*/
private Vector fQueue = new Vector(5);
private boolean fAppending;
class StreamEntry {
/**
* Identifier of the stream written to.
*/
private BuildConsoleStream fStream;
/**
* The text written
*/
private StringBuffer fText = null;
StreamEntry(String text, BuildConsoleStream stream) {
fText = new StringBuffer(text);
fStream = stream;
}
/**
* Returns the stream identifier
*/
public BuildConsoleStream getStream() {
return fStream;
}
public void appendText(String text) {
fText.append(text);
}
public int size() {
return fText.length();
}
/**
* Returns the text written
*/
public String getText() {
return fText.toString();
}
}
public BuildConsolePartitioner(BuildConsoleManager manager) {
fManager = manager;
fMaxLines = BuildConsolePreferencePage.buildConsoleLines();
@ -71,19 +122,40 @@ public class BuildConsolePartitioner
* the stream to append to
*/
public void appendToDocument(final String text, final BuildConsoleStream stream) {
if( text.length() == 0 )
return;
public void appendToDocument(String text, BuildConsoleStream stream) {
boolean addToQueue = true;
synchronized (fQueue) {
int i = fQueue.size();
if (i > 0) {
StreamEntry entry = (StreamEntry)fQueue.get(i - 1);
// if last stream is the same and we have not exceeded our
// display write limit, append.
if (entry.getStream() == stream && entry.size() < 10000) {
entry.appendText(text);
addToQueue = false;
}
}
if (addToQueue) {
fQueue.add(new StreamEntry(text, stream));
}
}
Runnable r = new Runnable() {
public void run() {
fLastStream = stream;
StreamEntry entry;
try {
if (stream == null) {
fDocument.set(text);
entry = (StreamEntry)fQueue.remove(0);
} catch (ArrayIndexOutOfBoundsException e) {
return;
}
fLastStream = entry.getStream();
System.out.println(entry.getText().length());
try {
warnOfContentChange(fLastStream);
if (fLastStream == null) {
fDocument.set(entry.getText());
} else {
fDocument.replace(fDocument.getLength(), 0, text);
fDocument.replace(fDocument.getLength(), 0, entry.getText());
checkOverflow();
}
} catch (BadLocationException e) {
@ -91,11 +163,18 @@ public class BuildConsolePartitioner
}
};
Display display = CUIPlugin.getStandardDisplay();
if (display != null) {
if (addToQueue && display != null) {
display.asyncExec(r);
}
}
private void warnOfContentChange(BuildConsoleStream stream) {
if (stream != null) {
ConsolePlugin.getDefault().getConsoleManager().warnOfContentChange(stream.getConsole());
}
fManager.showConsole();
}
public IDocument getDocument() {
return fDocument;
}
@ -285,11 +364,20 @@ public class BuildConsolePartitioner
}
}
public void start(IProject project) {
public void start(final IProject project) {
Display display = CUIPlugin.getStandardDisplay();
if (display != null) {
display.asyncExec(new Runnable() {
public void run() {
fManager.startConsoleActivity(project);
}
});
}
if (BuildConsolePreferencePage.isClearBuildConsole()) {
appendToDocument("", null); //$NON-NLS-1$
}
fManager.startConsoleActivity(project);
}
public class BuildOutputStream extends ConsoleOutputStream {
@ -301,19 +389,14 @@ public class BuildConsolePartitioner
}
public void flush() throws IOException {
if( fBuffer.length() > 0 )
appendToDocument(readBuffer(), fStream);
fManager.showConsole();
}
public void close() throws IOException {
flush();
}
public synchronized void write(byte[] b, int off, int len) throws IOException {
super.write(b, off, len);
if( fBuffer.length() > 4096 )
flush();
public void write(byte[] b, int off, int len) throws IOException {
appendToDocument(new String(b, off, len), fStream);
}
}

View file

@ -123,8 +123,8 @@ public class BuildConsolePreferencePage extends FieldEditorPreferencePage implem
public static void initDefaults(IPreferenceStore prefs) {
prefs.setDefault(PREF_CLEAR_CONSOLE, true);
prefs.setDefault(PREF_AUTO_OPEN_CONSOLE, false);
prefs.setDefault(PREF_CONSOLE_ON_TOP, true);
prefs.setDefault(PREF_AUTO_OPEN_CONSOLE, true);
prefs.setDefault(PREF_CONSOLE_ON_TOP, false);
prefs.setDefault(PREF_BUILDCONSOLE_LINES, 500);
prefs.setDefault(PREF_BUILDCONSOLE_TAB_WIDTH, 4);
PreferenceConverter.setDefault(prefs, PREF_BUILDCONSOLE_OUTPUT_COLOR, new RGB(0, 0, 0));