Skip to content

Commit 819484d

Browse files
committed
support for setting writers to capture the matlab process stdout and
stderr to ease debugging in some cases
1 parent 66ae5ea commit 819484d

2 files changed

Lines changed: 60 additions & 8 deletions

File tree

src/matlabcontrol/MatlabProxyFactoryOptions.java

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
package matlabcontrol;
77

88
import java.io.File;
9+
import java.io.Writer;
910
import java.util.concurrent.atomic.AtomicLong;
1011

1112
import matlabcontrol.MatlabProxyFactory.CopyPasteCallback;
@@ -32,6 +33,8 @@ public class MatlabProxyFactoryOptions {
3233
private final String _licenseFile;
3334
private final boolean _useSingleCompThread;
3435
private final int _port;
36+
private final Writer _inputWriter;
37+
private final Writer _errorWriter;
3538

3639
private MatlabProxyFactoryOptions(Builder options) {
3740
_matlabLocation = options._matlabLocation;
@@ -46,6 +49,8 @@ private MatlabProxyFactoryOptions(Builder options) {
4649
_licenseFile = options._licenseFile;
4750
_useSingleCompThread = options._useSingleCompThread;
4851
_port = options._port;
52+
_inputWriter = options._inputWriter;
53+
_errorWriter = options._errorWriter;
4954
}
5055

5156
String getMatlabLocation() {
@@ -95,6 +100,14 @@ boolean getUseSingleComputationalThread() {
95100
int getPort() {
96101
return _port;
97102
}
103+
104+
public Writer getInputWriter() {
105+
return _inputWriter;
106+
}
107+
108+
public Writer getErrorWriter() {
109+
return _errorWriter;
110+
}
98111

99112
/**
100113
* Creates instances of {@link MatlabProxyFactoryOptions}. Any and all of these properties may be left unset, if so
@@ -127,6 +140,8 @@ public static class Builder {
127140
private volatile String _licenseFile = null;
128141
private volatile boolean _useSingleCompThread = false;
129142
private volatile int _port = 2100;
143+
private volatile Writer _inputWriter;
144+
private volatile Writer _errorWriter;
130145

131146
//Assigning to a long is not atomic, so use an AtomicLong so that a thread always sees an intended value
132147
private final AtomicLong _proxyTimeout = new AtomicLong(180000L);
@@ -386,6 +401,30 @@ public final Builder setPort(int port) {
386401

387402
return this;
388403
}
404+
405+
/**
406+
* Sets the input writer where the standard output of the created matlab process is written to.
407+
* This is only used when an actual process is spawned for the matlab proxy instance.
408+
*
409+
* @param inputWriter
410+
*/
411+
public final Builder setInputWriter(Writer inputWriter){
412+
_inputWriter = inputWriter;
413+
414+
return this;
415+
}
416+
417+
/**
418+
* Sets the error writer where the error output of the created matlab process is written to.
419+
* This is only used when an actual process is spawned for the matlab proxy instance.
420+
*
421+
* @param inputWriter
422+
*/
423+
public final Builder setErrorWriter(Writer errorWriter){
424+
_errorWriter = errorWriter;
425+
426+
return this;
427+
}
389428

390429
/**
391430
* Builds a {@code MatlabProxyFactoryOptions} instance.
@@ -396,4 +435,5 @@ public final MatlabProxyFactoryOptions build() {
396435
return new MatlabProxyFactoryOptions(this);
397436
}
398437
}
438+
399439
}

src/matlabcontrol/RemoteMatlabProxyFactory.java

Lines changed: 20 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,9 @@
77

88
import java.io.IOException;
99
import java.io.InputStream;
10+
import java.io.InputStreamReader;
11+
import java.io.Reader;
12+
import java.io.Writer;
1013
import java.rmi.AlreadyBoundException;
1114
import java.rmi.NoSuchObjectException;
1215
import java.rmi.NotBoundException;
@@ -270,8 +273,8 @@ private Process createProcess(RemoteRequestReceiver receiver) throws MatlabConne
270273

271274
//If running under UNIX and MATLAB is hidden these streams need to be read so that MATLAB does not block
272275
if (_options.getHidden() && !Configuration.isWindows()) {
273-
new ProcessStreamDrainer(process.getInputStream(), "Input").start();
274-
new ProcessStreamDrainer(process.getErrorStream(), "Error").start();
276+
new ProcessStreamDrainer(process.getInputStream(), "Input", _options.getInputWriter()).start();
277+
new ProcessStreamDrainer(process.getErrorStream(), "Error", _options.getErrorWriter()).start();
275278
}
276279

277280
return process;
@@ -309,10 +312,12 @@ private String getRunArg(RemoteRequestReceiver receiver) throws MatlabConnection
309312
* blocking.
310313
*/
311314
private static class ProcessStreamDrainer extends Thread {
312-
private final InputStream _stream;
315+
private final Reader _reader;
316+
private final Writer _writer;
313317

314-
private ProcessStreamDrainer(InputStream stream, String type) {
315-
_stream = stream;
318+
private ProcessStreamDrainer(InputStream stream, String type, Writer writer) {
319+
_reader = new InputStreamReader(stream);
320+
_writer = writer;
316321

317322
this.setDaemon(true);
318323
this.setName("ProcessStreamDrainer - " + type);
@@ -321,14 +326,21 @@ private ProcessStreamDrainer(InputStream stream, String type) {
321326
@Override
322327
public void run() {
323328
try {
324-
byte[] buffer = new byte[1024];
325-
while (_stream.read(buffer) != -1)
326-
;
329+
char[] buffer = new char[1024];
330+
if(_writer != null){
331+
while (_reader.read(buffer) != -1){
332+
_writer.write(buffer);
333+
}
334+
}else{
335+
while (_reader.read(buffer) != -1)
336+
;
337+
}
327338
} catch (IOException e) {
328339
e.printStackTrace();
329340
throw new RuntimeException(e);
330341
}
331342
}
343+
332344
}
333345

334346
/**

0 commit comments

Comments
 (0)