Skip to main content
aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAndrew Ferrazzutti2014-06-05 20:46:44 +0000
committerAlexander Kurtakov2014-07-28 08:22:51 +0000
commit73412315189a0fc463062f338aa705a79fd00cea (patch)
treea98b4f9e5b9b480d5e5ae1da435a45455da67df7 /systemtap
parent754beff64a40d6ae465cbaee19f7107a77bcc2b7 (diff)
downloadorg.eclipse.linuxtools-73412315189a0fc463062f338aa705a79fd00cea.tar.gz
org.eclipse.linuxtools-73412315189a0fc463062f338aa705a79fd00cea.tar.xz
org.eclipse.linuxtools-73412315189a0fc463062f338aa705a79fd00cea.zip
Systemtap: Improve "Save Log" action.
Improve the UI of the "Save Log" button on script consoles to better indicate when and to where script output is being logged, and to allow logs to be saved while scripts are running. Also, to make these improvements work, fix a bug that prevents stap script Commands from being disposed when a running script is re-run. Change-Id: I38d1d154dba0dcbae5187aa9af8fc31c1672ee5c Signed-off-by: Andrew Ferrazzutti <aferrazz@redhat.com> Reviewed-on: https://git.eclipse.org/r/29767 Tested-by: Hudson CI Reviewed-by: Alexander Kurtakov <akurtako@redhat.com> Tested-by: Alexander Kurtakov <akurtako@redhat.com>
Diffstat (limited to 'systemtap')
-rw-r--r--systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/internal/systemtap/structures/localization.properties1
-rw-r--r--systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/LoggingStreamDaemon.java129
-rw-r--r--systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/runnable/Command.java11
-rw-r--r--systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/META-INF/MANIFEST.MF2
-rw-r--r--systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/pom.xml2
-rw-r--r--systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/internal/systemtap/ui/consolelog/actions/ConsoleAction.java15
-rw-r--r--systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/internal/systemtap/ui/consolelog/actions/SaveLogAction.java69
-rw-r--r--systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/systemtap/ui/consolelog/internal/localization.properties6
-rw-r--r--systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/systemtap/ui/consolelog/structures/ScriptConsole.java141
9 files changed, 255 insertions, 121 deletions
diff --git a/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/internal/systemtap/structures/localization.properties b/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/internal/systemtap/structures/localization.properties
index 9fbf722d93..a441fb8f12 100644
--- a/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/internal/systemtap/structures/localization.properties
+++ b/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/internal/systemtap/structures/localization.properties
@@ -10,3 +10,4 @@ PasswordDialog.Password=Password
PasswordDialog.SUDOPassword=SUDO Password:
PasswordDialog.SavePassword=Save password?
ReadingKernelSourceTree=Reading Kernel Source Tree
+LoggingStreamDaemon.ResumedLog=---PARTIAL SKIP--- \ No newline at end of file
diff --git a/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/LoggingStreamDaemon.java b/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/LoggingStreamDaemon.java
index 6865824537..01da522a7d 100644
--- a/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/LoggingStreamDaemon.java
+++ b/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/LoggingStreamDaemon.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2006 IBM Corporation.
+ * Copyright (c) 2006 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
*
* Contributors:
* IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
+ * Red Hat - ongoing maintenance
*******************************************************************************/
package org.eclipse.linuxtools.systemtap.structures;
@@ -15,38 +16,53 @@ import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
+import java.util.HashSet;
+import java.util.Set;
+import org.eclipse.linuxtools.internal.systemtap.structures.Localization;
import org.eclipse.linuxtools.systemtap.structures.listeners.IGobblerListener;
-
-
+/**
+ * A utility for saving script output to a log file as it runs.
+ */
public class LoggingStreamDaemon implements IGobblerListener {
+ private static final int BUFFER_SIZE = 1024;
+ private static final Set<LoggingStreamDaemon> allLogs = new HashSet<>();
+
+ protected StringBuilder output;
+ protected File outputFile;
+ protected FileWriter writer;
+ private boolean saveLog = false;
/**
- * Sets up an output to log to.
+ * Sets up a new logger. Log contents will be saved to a temporary file
+ * until {@link #saveLog} is used to save the log to a specified file.
*/
public LoggingStreamDaemon() {
output = new StringBuilder();
try {
outputFile = File.createTempFile(this.toString(), ".tmp"); //$NON-NLS-1$
writer = new FileWriter(outputFile, true);
- } catch(IOException ioe) {
+ } catch (IOException ioe) {
outputFile = null;
+ writer = null;
}
- saveLog = false;
}
/**
* Pushes output to log.
*/
private void pushData() {
- if(null != writer) {
- try {
- writer.write(output.toString());
- output.delete(0, output.length());
- writer.flush();
- } catch(IOException ioe) {}
- }
+ try {
+ // Recreate the log if it was deleted
+ if (!outputFile.exists()) {
+ saveLog(new File(outputFile.toString()));
+ output.insert(0, Localization.getString("LoggingStreamDaemon.ResumedLog") + '\n'); //$NON-NLS-1$
+ }
+ writer.write(output.toString());
+ output.setLength(0);
+ writer.flush();
+ } catch(IOException ioe) {}
}
/**
@@ -54,45 +70,63 @@ public class LoggingStreamDaemon implements IGobblerListener {
*/
@Override
public void handleDataEvent(String line) {
- output.append(line);
- this.pushData();
+ if (isReady()) {
+ output.append(line);
+ pushData();
+ }
}
/**
* Reads in and returns the output produced.
- *
* @return The logged data.
*/
public String getOutput() {
- if(null == outputFile)
+ if (!isReady()) {
return null;
- if(output.length() > 0) pushData();
+ }
+ if (output.length() > 0) {
+ pushData();
+ }
try (FileReader reader = new FileReader(outputFile)) {
char[] buffer = new char[BUFFER_SIZE];
int count;
StringBuilder builder = new StringBuilder();
- while(-1 != (count = reader.read(buffer))) {
+ while (-1 != (count = reader.read(buffer))) {
builder.append(buffer, 0, count);
}
return builder.toString();
- } catch(IOException ioe) {}
- return null;
+ } catch (IOException ioe) {
+ return null;
+ }
}
/**
- * Saves the logfile.
- *
- * @param file The file to save the log data to.
- *
- * @return True if the save was successful.
+ * Sets the logging stream to be continuously saved to a file.
+ * @param file The file to save the log data to. Must not be <code>null</code>.
+ * @return <code>true</code> if the save was successful, <code>false</code> otherwise.
*/
public boolean saveLog(File file) {
+ if (!isReady()) {
+ return false;
+ }
+ // If saving to the same file that's already being saved to, do nothing.
+ if (file.equals(outputFile)) {
+ return true;
+ }
+ // If saving to a file used by another active log,
+ // quit to avoid write conflicts.
+ for (LoggingStreamDaemon log : allLogs) {
+ if (!log.equals(this) && file.equals(log.outputFile)) {
+ return false;
+ }
+ }
+
try {
- if(!file.exists()) {
+ if (!file.exists()) {
file.getParentFile().mkdirs();
file.createNewFile();
}
- FileWriter w = new FileWriter(file, true);
+ FileWriter w = new FileWriter(file, false);
try (FileReader r = new FileReader(outputFile)) {
char[] buffer = new char[BUFFER_SIZE];
int count;
@@ -103,31 +137,40 @@ public class LoggingStreamDaemon implements IGobblerListener {
w.flush();
writer.close();
writer = w;
- outputFile.delete();
- outputFile = file;
- saveLog = true;
- } catch(IOException ioe) {
+ } catch (IOException ioe) {
return false;
}
+ outputFile.delete();
+ outputFile = file;
+ saveLog = true;
+ allLogs.add(this);
return true;
}
public void dispose() {
- if(null != outputFile && !saveLog)
- outputFile.delete();
- outputFile = null;
- if(null != writer) {
+ if (outputFile != null) {
+ if (!saveLog) {
+ outputFile.delete();
+ }
+ outputFile = null;
+ }
+
+ if (writer != null) {
try {
writer.close();
} catch(IOException ioe) {}
+ writer = null;
}
- writer = null;
- }
- protected StringBuilder output;
- protected File outputFile;
- protected FileWriter writer;
- private boolean saveLog;
+ if (output != null) {
+ output.setLength(0);
+ output = null;
+ }
- private static final int BUFFER_SIZE = 1024;
+ allLogs.remove(this);
+ }
+
+ private boolean isReady() {
+ return writer != null && outputFile != null;
+ }
}
diff --git a/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/runnable/Command.java b/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/runnable/Command.java
index 3798e584a3..993442f0ac 100644
--- a/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/runnable/Command.java
+++ b/systemtap/org.eclipse.linuxtools.systemtap.structures/src/org/eclipse/linuxtools/systemtap/structures/runnable/Command.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2006 IBM Corporation.
+ * Copyright (c) 2006 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
*
* Contributors:
* IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
+ * Red Hat - ongoing maintenance
*******************************************************************************/
package org.eclipse.linuxtools.systemtap.structures.runnable;
@@ -144,7 +145,7 @@ public class Command implements Runnable {
errorGobbler = new StreamGobbler(process.getErrorStream());
inputGobbler = new StreamGobbler(process.getInputStream());
- this.transferListeners();
+ transferListeners();
return Status.OK_STATUS;
} catch (IOException e) {
return new Status(IStatus.ERROR, StructuresPlugin.PLUGIN_ID, e.getMessage(), e);
@@ -169,7 +170,7 @@ public class Command implements Runnable {
/**
* This method handles checking the status of the running <code>Process</code>. It
* is called when the new Thread is created, and thus should never be called by
- * any implementing program. To run call the <code>start</code> method.
+ * any implementing program. To run call the {@link #start} method.
*/
@Override
public void run() {
@@ -295,7 +296,7 @@ public class Command implements Runnable {
}
/**
- * Saves the input stream data to a permanent file. Any new data on the
+ * Saves the input stream data to a permanent file. Any new data on the
* stream will automatically be saved to the file.
* @param file The file to save the InputStream to.
*/
@@ -307,7 +308,7 @@ public class Command implements Runnable {
* Disposes of all internal components of this class. Nothing in the class should be
* referenced after this is called.
*/
- public void dispose() {
+ public synchronized void dispose() {
if (!disposed) {
stop();
disposed = true;
diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/META-INF/MANIFEST.MF b/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/META-INF/MANIFEST.MF
index f1a4ab743c..b1cd58fbbc 100644
--- a/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/META-INF/MANIFEST.MF
+++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/META-INF/MANIFEST.MF
@@ -2,7 +2,7 @@ Manifest-Version: 1.0
Bundle-ManifestVersion: 2
Bundle-Name: %bundleName
Bundle-SymbolicName: org.eclipse.linuxtools.systemtap.ui.consolelog;singleton:=true
-Bundle-Version: 3.0.0.qualifier
+Bundle-Version: 3.1.0.qualifier
Bundle-Activator: org.eclipse.linuxtools.systemtap.ui.consolelog.internal.ConsoleLogPlugin
Bundle-Vendor: %bundleProvider
Bundle-Localization: plugin
diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/pom.xml b/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/pom.xml
index 6ad6156704..deb5d90fea 100644
--- a/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/pom.xml
+++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/pom.xml
@@ -17,7 +17,7 @@
</parent>
<artifactId>org.eclipse.linuxtools.systemtap.ui.consolelog</artifactId>
- <version>3.0.0-SNAPSHOT</version>
+ <version>3.1.0-SNAPSHOT</version>
<packaging>eclipse-plugin</packaging>
<name>Linux Tools SystemTap ConsoleLog Plug-in</name>
diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/internal/systemtap/ui/consolelog/actions/ConsoleAction.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/internal/systemtap/ui/consolelog/actions/ConsoleAction.java
index c26fc3af6f..529ca62f2f 100644
--- a/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/internal/systemtap/ui/consolelog/actions/ConsoleAction.java
+++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/internal/systemtap/ui/consolelog/actions/ConsoleAction.java
@@ -1,4 +1,5 @@
/*******************************************************************************
+ * Copyright (c) 2006 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -14,6 +15,7 @@ package org.eclipse.linuxtools.internal.systemtap.ui.consolelog.actions;
import java.net.URL;
import org.eclipse.jface.action.Action;
+import org.eclipse.jface.action.IAction;
import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.linuxtools.systemtap.ui.consolelog.structures.ScriptConsole;
@@ -29,8 +31,17 @@ public abstract class ConsoleAction extends Action {
protected ConsoleAction(ScriptConsole fConsole,
URL imagePath,
String text,
- String toolTip){
- this.console = fConsole;
+ String toolTip) {
+ this(fConsole, imagePath, text, toolTip, IAction.AS_PUSH_BUTTON);
+ }
+
+ protected ConsoleAction(ScriptConsole fConsole,
+ URL imagePath,
+ String text,
+ String toolTip,
+ int style) {
+ super(null, style);
+ console = fConsole;
setImageDescriptor(ImageDescriptor.createFromURL(imagePath));
setToolTipText(text);
diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/internal/systemtap/ui/consolelog/actions/SaveLogAction.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/internal/systemtap/ui/consolelog/actions/SaveLogAction.java
index d4bd8c2664..034e03ba93 100644
--- a/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/internal/systemtap/ui/consolelog/actions/SaveLogAction.java
+++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/internal/systemtap/ui/consolelog/actions/SaveLogAction.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2006 IBM Corporation.
+ * Copyright (c) 2006 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,15 +7,20 @@
*
* Contributors:
* IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
+ * Red Hat - ongoing maintenance
*******************************************************************************/
package org.eclipse.linuxtools.internal.systemtap.ui.consolelog.actions;
import java.io.File;
+import java.text.MessageFormat;
+import org.eclipse.core.runtime.Path;
+import org.eclipse.jface.action.IAction;
import org.eclipse.linuxtools.systemtap.ui.consolelog.internal.ConsoleLogPlugin;
import org.eclipse.linuxtools.systemtap.ui.consolelog.internal.Localization;
import org.eclipse.linuxtools.systemtap.ui.consolelog.structures.ScriptConsole;
+import org.eclipse.linuxtools.systemtap.ui.consolelog.structures.ScriptConsole.ScriptConsoleObserver;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.FileDialog;
import org.eclipse.ui.PlatformUI;
@@ -24,7 +29,9 @@ import org.eclipse.ui.PlatformUI;
* This class is used to allow the user to save the log generated from an active console
* @author Ryan Morse
*/
-public class SaveLogAction extends ConsoleAction {
+public class SaveLogAction extends ConsoleAction implements ScriptConsoleObserver {
+
+ private String logFileName = null;
/**
* @since 2.0
@@ -33,7 +40,9 @@ public class SaveLogAction extends ConsoleAction {
super(fConsole,
ConsoleLogPlugin.getDefault().getBundle().getEntry("icons/actions/save_log.gif"), //$NON-NLS-1$
Localization.getString("action.saveLog.name"), //$NON-NLS-1$
- Localization.getString("action.saveLog.desc")); //$NON-NLS-1$
+ Localization.getString("action.saveLog.desc"), //$NON-NLS-1$
+ IAction.AS_CHECK_BOX);
+ console.addScriptConsoleObserver(this);
}
/**
@@ -42,12 +51,13 @@ public class SaveLogAction extends ConsoleAction {
*/
@Override
public void run() {
- if(null != console) {
- File file = getFile();
- if(null != file){
- console.saveStream(file);
+ File file = getFile();
+ if (file != null) {
+ if (console.saveStreamAndReturnResult(file)) {
+ logFileName = file.toString();
}
}
+ updateChecked();
}
/**
@@ -55,16 +65,47 @@ public class SaveLogAction extends ConsoleAction {
* @return File representing the desired destination for the log.
*/
private File getFile() {
- String path = null;
- FileDialog dialog= new FileDialog(PlatformUI.getWorkbench().getActiveWorkbenchWindow().getShell(), SWT.SAVE);
- dialog.setText(Localization.getString("SaveLogAction.OutputFile")); //$NON-NLS-1$
+ FileDialog dialog = new FileDialog(PlatformUI
+ .getWorkbench().getActiveWorkbenchWindow()
+ .getShell(), SWT.SAVE);
- path = dialog.open();
+ if (logFileName != null) {
+ Path logPath = new Path(logFileName);
+ dialog.setFilterPath(logPath.removeLastSegments(1).toOSString());
+ dialog.setFileName(logPath.lastSegment());
+ }
+ dialog.setText(Localization.getString(
+ !isLogging() ? "SaveLogAction.OutputFile" //$NON-NLS-1$
+ : "SaveLogAction.OutputFileLocation")); //$NON-NLS-1$
+ dialog.setOverwrite(true);
+ String path = dialog.open();
+ return path != null ? new File(path) : null;
+ }
- if(null == path){
- return null;
+ public void updateChecked() {
+ if (isLogging()) {
+ setToolTipText(MessageFormat.format(
+ Localization.getString("action.saveLog.name2"), //$NON-NLS-1$
+ logFileName));
+ setChecked(true);
+ } else {
+ setToolTipText(Localization.getString("action.saveLog.name")); //$NON-NLS-1$
+ setChecked(false);
}
+ }
- return new File(path);
+ private boolean isLogging() {
+ return logFileName != null;
}
+
+ @Override
+ public void runningStateChanged(boolean started, boolean stopped) {
+ // Uncheck the button whenever a script restarts, for it will be associated with
+ // a new command and therefore a new logger.
+ if (started && !stopped) {
+ logFileName = null;
+ updateChecked();
+ }
+ }
+
}
diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/systemtap/ui/consolelog/internal/localization.properties b/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/systemtap/ui/consolelog/internal/localization.properties
index daff96aa3f..87cc35738d 100644
--- a/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/systemtap/ui/consolelog/internal/localization.properties
+++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/systemtap/ui/consolelog/internal/localization.properties
@@ -1,4 +1,5 @@
-SaveLogAction.OutputFile=Output File
+SaveLogAction.OutputFile=Create Log File
+SaveLogAction.OutputFileLocation=Change Location of Log File
ErrorStreamDaemon.Password=Password:
@@ -8,7 +9,7 @@ ScriptConsole.ErrorRunningStapTitle=Error Running SystemTap Script
ScriptConsole.ErrorRunningStapMessage=The SystemTap script could not be run. See the error details below for more information.
ScriptConsole.ErrorKillingStap=Error Killing SystemTap Process
ScriptConsole.Problem=Problem
-ScriptConsole.ErrorSavingLog=Error saving the log file.
+ScriptConsole.ErrorSavingLog=Error saving the log file. Make sure that the path chosen for this log is valid and not being used by any running logs.
ScriptConsole.Terminated=<terminated>
ErrorView.Type=Type
@@ -22,6 +23,7 @@ action.closeConsole.name=Close Console
action.closeConsole.desc=Close this console.
action.saveLog.name=Save &Log
+action.saveLog.name2=Change Log Location\n(current: {0})
action.saveLog.desc=Save console log to file
ModifyParsingAction_name=Modify &Parsing Expression
diff --git a/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/systemtap/ui/consolelog/structures/ScriptConsole.java b/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/systemtap/ui/consolelog/structures/ScriptConsole.java
index 10b6b34e5d..c131506096 100644
--- a/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/systemtap/ui/consolelog/structures/ScriptConsole.java
+++ b/systemtap/org.eclipse.linuxtools.systemtap.ui.consolelog/src/org/eclipse/linuxtools/systemtap/ui/consolelog/structures/ScriptConsole.java
@@ -1,5 +1,5 @@
/*******************************************************************************
- * Copyright (c) 2006 IBM Corporation.
+ * Copyright (c) 2006 IBM Corporation and others.
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
@@ -7,6 +7,7 @@
*
* Contributors:
* IBM Corporation - Jeff Briggs, Henry Hughes, Ryan Morse
+ * Red Hat - ongoing maintenance
*******************************************************************************/
package org.eclipse.linuxtools.systemtap.ui.consolelog.structures;
@@ -43,19 +44,17 @@ import org.eclipse.ui.console.IOConsole;
* @author Ryan Morse
*/
public class ScriptConsole extends IOConsole {
- private static final long RETRY_STOP_TIME = 500;
- private static final long JOIN_WAIT_TIME = 500;
/**
* The command that will run in this console.
*/
- private Command cmd;
+ private Command cmd = null;
/**
* A protocol for sending "stop" signals to cmd when it is forcably
* stopped by a user action.
*/
- private Runnable stopCommand;
+ private StopCommand stopCommand;
/**
* A thread in which to asynchronously run stopCommand.
@@ -100,8 +99,8 @@ public class ScriptConsole extends IOConsole {
*/
public static boolean instanceIsRunning(String name) {
IConsole ic[] = ConsolePlugin.getDefault().getConsoleManager().getConsoles();
- if (null != ic) {
- for (IConsole consoleIterator: ic) {
+ if (ic != null) {
+ for (IConsole consoleIterator : ic) {
if (consoleIterator instanceof ScriptConsole) {
ScriptConsole activeConsole = (ScriptConsole) consoleIterator;
if (activeConsole.getName().endsWith(name) && activeConsole.isRunning()) {
@@ -129,13 +128,13 @@ public class ScriptConsole extends IOConsole {
//Prevent running the same script twice
if (ic != null) {
ScriptConsole activeConsole;
- for (IConsole consoleIterator: ic) {
+ for (IConsole consoleIterator : ic) {
if (consoleIterator instanceof ScriptConsole) {
activeConsole = (ScriptConsole) consoleIterator;
if (activeConsole.getName().endsWith(name)) {
//Stop any script currently running, and terminate stream listeners.
if (activeConsole.isRunning()) {
- activeConsole.stop();
+ activeConsole.stopAndDispose();
if (activeConsole.errorDaemon != null) {
activeConsole.cmd.removeErrorStreamListener(activeConsole.errorDaemon);
}
@@ -146,7 +145,7 @@ public class ScriptConsole extends IOConsole {
if (activeConsole.onCmdStopThread != null && activeConsole.onCmdStopThread.isAlive()) {
activeConsole.onCmdStopThread.interrupt();
try {
- activeConsole.onCmdStopThread.join(JOIN_WAIT_TIME);
+ activeConsole.onCmdStopThread.join();
} catch (InterruptedException e) {}
}
//Remove output from last run
@@ -207,7 +206,47 @@ public class ScriptConsole extends IOConsole {
ScriptConsole(String name, ImageDescriptor imageDescriptor) {
super(name, imageDescriptor);
- cmd = null;
+ }
+
+ private abstract class StopCommand implements Runnable {
+ private static final long RETRY_STOP_TIME = 500;
+
+ protected final Command stopcmd;
+ protected final String stopString;
+ private boolean disposeOnStop = false;
+
+ private StopCommand(Command stopcmd, String stopString) {
+ this.stopcmd = stopcmd;
+ this.stopString = stopString;
+ }
+
+ protected abstract void stop() throws IOException, CoreException;
+
+ void makeDisposeOnStop() {
+ disposeOnStop = true;
+ }
+
+ @Override
+ public void run() {
+ try {
+ synchronized (stopcmd) {
+ while (stopcmd.isRunning()) {
+ stop();
+ stopcmd.wait(RETRY_STOP_TIME);
+ }
+ if (disposeOnStop) {
+ stopcmd.dispose();
+ }
+ }
+ } catch (IOException | CoreException e) {
+ ExceptionErrorDialog.openError(Localization.
+ getString("ScriptConsole.ErrorKillingStap"), //$NON-NLS-1$
+ e.getMessage(), e);
+ } catch (InterruptedException e) {
+ // Wait was interrupted. Exit.
+ }
+ }
+
}
/**
@@ -250,27 +289,15 @@ public class ScriptConsole extends IOConsole {
}
cmd = new ScpExec(command, remoteOptions, envVars);
- stopCommand = new Runnable() {
- private final Command stopcmd = cmd;
- private final String stopString = getStopString();
+ stopCommand = new StopCommand(cmd, getStopString()) {
+ ScpExec stop = new ScpExec(new String[]{stopString}, remoteOptions, null);
@Override
- public void run() {
- ScpExec stop = new ScpExec(new String[]{stopString}, remoteOptions, null);
- try {
- synchronized (stopcmd) {
- while (stopcmd.isRunning()) {
- stop.start();
- stopcmd.wait(RETRY_STOP_TIME);
- }
- }
- } catch (CoreException e) {
- ExceptionErrorDialog.openError(Localization.getString("ScriptConsole.ErrorKillingStap"), e.getMessage(), e); //$NON-NLS-1$
- } catch (InterruptedException e) {
- // Wait was interrupted. Exit.
- }
+ protected void stop() throws CoreException {
+ stop.start();
}
};
+
this.run(cmd, errorParser);
}
@@ -291,26 +318,13 @@ public class ScriptConsole extends IOConsole {
cmd = new Command(command, envVars, project);
final IProject proj = project;
- stopCommand = new Runnable() {
- private final Command stopcmd = cmd;
- String stopString = getStopString();
-
+ stopCommand = new StopCommand(cmd, getStopString()) {
@Override
- public void run() {
- try {
- synchronized (stopcmd) {
- while (stopcmd.isRunning()) {
- RuntimeProcessFactory.getFactory().exec(stopString, null, proj);
- stopcmd.wait(RETRY_STOP_TIME);
- }
- }
- } catch (IOException e) {
- ExceptionErrorDialog.openError(Localization.getString("ScriptConsole.ErrorKillingStap"), e.getMessage(), e); //$NON-NLS-1$
- } catch (InterruptedException e) {
- //Wait was interrupted. Exit.
- }
+ protected void stop() throws IOException {
+ RuntimeProcessFactory.getFactory().exec(stopString, null, proj);
}
};
+
this.run(cmd, errorParser);
}
@@ -433,7 +447,7 @@ public class ScriptConsole extends IOConsole {
*/
public boolean isDisposed() {
// If there is no command it can be considered disposed
- if (null == cmd) {
+ if (cmd == null) {
return true;
}
return cmd.isDisposed();
@@ -441,18 +455,30 @@ public class ScriptConsole extends IOConsole {
/**
* Method to allow the user to save the Commands output to a file for use latter.
+ * Does not return a value indicating success of the operation; for that, use
+ * {@link #saveStreamAndReturnResult(File)} instead.
* @param file The new file to save the output to.
*/
public void saveStream(File file) {
- if (isRunning()) {
- if (!cmd.saveLog(file)) {
- MessageDialog.openWarning(
- PlatformUI.getWorkbench()
- .getActiveWorkbenchWindow().getShell(),
- Localization.getString("ScriptConsole.Problem"), Localization.getString("ScriptConsole.ErrorSavingLog")); //$NON-NLS-1$//$NON-NLS-2$
+ saveStreamAndReturnResult(file);
+ }
- }
+ /**
+ * Method to allow the user to save the Commands output to a file for use later.
+ * @param file The new file to save the output to.
+ * @return <code>true</code> if the save result was successful, <code>false</code> otherwise.
+ * Note that a failed save attempt will not interfere with an already-running log.
+ * @since 3.1
+ */
+ public boolean saveStreamAndReturnResult(File file) {
+ if (!cmd.saveLog(file)) {
+ MessageDialog.openWarning(
+ PlatformUI.getWorkbench()
+ .getActiveWorkbenchWindow().getShell(),
+ Localization.getString("ScriptConsole.Problem"), Localization.getString("ScriptConsole.ErrorSavingLog")); //$NON-NLS-1$//$NON-NLS-2$
+ return false;
}
+ return true;
}
/**
@@ -475,6 +501,14 @@ public class ScriptConsole extends IOConsole {
}
/**
+ * Stops and disposes the running command.
+ */
+ private synchronized void stopAndDispose() {
+ stopCommand.makeDisposeOnStop();
+ stop();
+ }
+
+ /**
* Stops the running command and the associated listeners.
*/
public synchronized void stop() {
@@ -503,6 +537,7 @@ public class ScriptConsole extends IOConsole {
private String getStopString() {
return "pkill -SIGINT -f stapio.*"+ getModuleName(); //$NON-NLS-1$
}
+
/**
* Disposes of all internal references in the class. No method should be called after this.
*/

Back to the top