This class provides the functionality to divert output sent to the System.out and System.err streams to ImageJ's log console. The purpose is to allow use of existing Java classes or writing new generic Java classes that only output to System.out and are thus less dependent on ImageJ. See the ImageJ
| 16 | * See Also: Redirect_System_Streams (http://staff.fh-hagenberg.at/burger/imagej/) |
| 17 | */ |
| 18 | public class LogStream extends PrintStream { |
| 19 | |
| 20 | private static String outPrefix = "out> "; // prefix string for System.out |
| 21 | private static String errPrefix = "err >"; // prefix string for System.err |
| 22 | |
| 23 | private static PrintStream originalSystemOut = null; |
| 24 | private static PrintStream originalSystemErr = null; |
| 25 | private static PrintStream temporarySystemOut = null; |
| 26 | private static PrintStream temporarySystemErr = null; |
| 27 | |
| 28 | /** |
| 29 | * Redirects all output sent to <code>System.out</code> and <code>System.err</code> to ImageJ's log console |
| 30 | * using the default prefixes. |
| 31 | */ |
| 32 | public static void redirectSystem(boolean redirect) { |
| 33 | if (redirect) |
| 34 | redirectSystem(); |
| 35 | else |
| 36 | revertSystem(); |
| 37 | } |
| 38 | |
| 39 | /** |
| 40 | * Redirects all output sent to <code>System.out</code> and <code>System.err</code> to ImageJ's log console |
| 41 | * using the default prefixes. |
| 42 | * Alternatively use |
| 43 | * {@link #redirectSystemOut(String)} and {@link #redirectSystemErr(String)} |
| 44 | * to redirect the streams separately and to specify individual prefixes. |
| 45 | */ |
| 46 | public static void redirectSystem() { |
| 47 | redirectSystemOut(outPrefix); |
| 48 | redirectSystemErr(errPrefix); |
| 49 | } |
| 50 | |
| 51 | /** |
| 52 | * Redirects all output sent to <code>System.out</code> to ImageJ's log console. |
| 53 | * @param prefix The prefix string inserted at the start of each output line. |
| 54 | * Pass <code>null</code> to use the default prefix or an empty string to |
| 55 | * remove the prefix. |
| 56 | */ |
| 57 | public static void redirectSystemOut(String prefix) { |
| 58 | if (originalSystemOut == null) { // has no effect if System.out is already replaced |
| 59 | originalSystemOut = System.out; // remember the original System.out stream |
| 60 | temporarySystemOut = new LogStream(prefix); |
| 61 | System.setOut(temporarySystemOut); |
| 62 | } |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Redirects all output sent to <code>System.err</code> to ImageJ's log console. |
| 67 | * @param prefix The prefix string inserted at the start of each output line. |
| 68 | * Pass <code>null</code> to use the default prefix or an empty string to |
| 69 | * remove the prefix. |
| 70 | */ |
| 71 | public static void redirectSystemErr(String prefix) { |
| 72 | if (originalSystemErr == null) { // has no effect if System.out is already replaced |
| 73 | originalSystemErr = System.err; // remember the original System.out stream |
| 74 | temporarySystemErr = new LogStream(prefix); |
| 75 | System.setErr(temporarySystemErr); |
nothing calls this directly
no test coverage detected