public final class VerboseProcess extends Object
stdout
from a running process
and logging it through SLF4J.
For example:
String name = new VerboseProcess( new ProcessBuilder("who", "am", "i") ).stdout();
The class throws an exception if the process returns a non-zero exit code.
The class is thread-safe.
Constructor and Description |
---|
VerboseProcess(Process prc)
Public ctor.
|
VerboseProcess(ProcessBuilder builder)
Public ctor (builder will be configured to redirect error input to
the
stdout and will receive an empty stdin ). |
VerboseProcess(ProcessBuilder bdr,
Level stdout,
Level stderr)
Public ctor, with a given process and logging levels for
stdout
and stderr . |
VerboseProcess(Process prc,
Level stdout,
Level stderr)
Public ctor, with a given process and logging levels for
stdout
and stderr . |
Modifier and Type | Method and Description |
---|---|
String |
stdout()
Get
stdout from the process, after its finish (the method will
wait for the process and log its output). |
String |
stdoutQuietly()
Get
stdout from the process, after its finish (the method will
wait for the process and log its output). |
public VerboseProcess(Process prc)
prc
- The process to work withpublic VerboseProcess(@NotNull(message="process builder can\'t be NULL") ProcessBuilder builder)
stdout
and will receive an empty stdin
).builder
- Process builder to work withpublic VerboseProcess(@NotNull(message="process can\'t be NULL") Process prc, @NotNull(message="stdout level can\'t be NULL") Level stdout, @NotNull(message="stderr level can\'t be NULL") Level stderr)
stdout
and stderr
.prc
- Process to execute and monitorstdout
- Log level for stdoutstderr
- Log level for stderrpublic VerboseProcess(@NotNull(message="process can\'t be NULL") ProcessBuilder bdr, @NotNull(message="stdout level can\'t be NULL") Level stdout, @NotNull(message="stderr level can\'t be NULL") Level stderr)
stdout
and stderr
.bdr
- Process builder to execute and monitorstdout
- Log level for stdoutstderr
- Log level for stderrpublic String stdout()
stdout
from the process, after its finish (the method will
wait for the process and log its output).
The method will check process exit code, and if it won't be equal
to zero a runtime exception will be thrown. A non-zero exit code
usually is an indicator of problem. If you want to ignore this code,
use stdoutQuietly()
instead.
stdout
of the processpublic String stdoutQuietly()
stdout
from the process, after its finish (the method will
wait for the process and log its output).
This method ignores exit code of the process. Even if it is
not equal to zero (which usually is an indicator of an error), the
method will quietly return its output. The method is useful when
you're running a background process. You will kill it with
Process.destroy()
, which usually will lead to a non-zero
exit code, which you want to ignore.
stdout
of the processCopyright © 2012–2014 jcabi.com. All rights reserved.