Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Java: synchronizing standard out and standard error

Tags:

java

eclipse

I have a strange problem and it would be nice if I could solve it. For the debugging purposes (and some other things, as well) I'm writing a log of a console Java application on the standard output. Some things that are writen on standard out, and some things like errors are printed on standard error. The problem is that these two are not perfectly synchronized, so the order of printed lines is not always correct. I guess this is because many things are printed and it happens that a buffer for one output is full so the other output prints before the first one flushes it buffer.

E.g., I want to write this:

syso: aaa
syso: bbb
syso: ccc
syso: ddd
syso: eee
syserr: ---

What is sometimes printed is

aaa
bbb
ccc
---
ddd
eee

Sometimes there is not a new line in between, so it looks like

aaa
bbb
ccc---

ddd
eee

Every time I print something on an output, I flush the same output with

System.out.flush();

or

System.err.flush();

How to solve this problem? Btw, everything is printed in the Eclipse console.

like image 214
Ivan Avatar asked May 25 '11 08:05

Ivan


4 Answers

The problem is that it's the responsibility of the terminal emulator (in your case, Eclipse) to process the standard output and the standard error of your application. Without communicating with the terminal emulator, you can never be sure that out and err are displayed in the right order. Therefore, I would consider printing everything on err and redirect it to a file. You can still use out for clean user interaction.

Nevertheless, there is a (very bad, but strict) solution to your problem:

System.out.println(...);
System.out.flush();
Thread.sleep(100);

System.err.println(...);
System.err.flush();
Thread.sleep(100);

You may have to change the sleep duration depending on your configuration!

like image 163
Eser Aygün Avatar answered Oct 20 '22 18:10

Eser Aygün


java.lang.System.setErr(java.lang.System.out);

makes the application use the standard output as error stream.

like image 28
Oswald Avatar answered Oct 20 '22 17:10

Oswald


This is a long-standing Eclipse bug: https://bugs.eclipse.org/bugs/show_bug.cgi?id=32205.

like image 3
Matt Ball Avatar answered Oct 20 '22 17:10

Matt Ball


I know this post is ancient, but it's still an issue today, so here another solution that fixes the problem using @EserAygün's answer, but in a way that does not require you to find and modify every place in your project where you are writing to System.out or System.err.

Create yourself a class called EclipseTools with the following content (and the required package declaration and imports):

public class EclipseTools {

    private static OutputStream lastStream = null;
    private static boolean      isFixed    = false;

    private static class FixedStream extends OutputStream {

        private final OutputStream target;

        public FixedStream(OutputStream originalStream) {
            target = originalStream;
        }

        @Override
        public void write(int b) throws IOException {
            if (lastStream!=this) swap();
            target.write(b);
        }

        @Override
        public void write(byte[] b) throws IOException {
            if (lastStream!=this) swap();
            target.write(b);
        }

        @Override
        public void write(byte[] b, int off, int len) throws IOException {
            if (lastStream!=this) swap();
            target.write(b, off, len);
        }

        private void swap() throws IOException {
            if (lastStream!=null) {
                lastStream.flush();
                try { Thread.sleep(200); } catch (InterruptedException e) {}
            }
            lastStream = this;
        }

        @Override public void close() throws IOException { target.close(); }
        @Override public void flush() throws IOException { target.flush(); }
    }

    /**
     * Inserts a 200ms delay into the System.err or System.out OutputStreams
     * every time the output switches from one to the other. This prevents
     * the Eclipse console from showing the output of the two streams out of
     * order. This function only needs to be called once.
     */
    public static void fixConsole() {
        if (isFixed) return;
        isFixed = true;
        System.setErr(new PrintStream(new FixedStream(System.err)));
        System.setOut(new PrintStream(new FixedStream(System.out)));
    }
}

Then, just call EclipseTools.fixConsole() once in the beginning of your code. Problem solved.

Basically, this replaces the two streams System.err and System.out with a custom set of streams that simply forward their data to the original streams, but keep track of which stream was written to last. If the stream that is written to changes, for example a System.err.something(...) followed by a System.out.something(...), it flushes the output of the last stream and waits for 200ms to give the Eclipse console time to complete printing it.

Note: The 200ms are just a rough initial value. If this code reduces, but does not eliminate the problem for you, increase the delay in Thread.sleep from 200 to something higher until it works. Alternatively, if this delay works but impacts performance of your code (if you alternate streams often), you can try reducing it gradually until you start getting errors.

like image 3
Markus A. Avatar answered Oct 20 '22 17:10

Markus A.