Skip to content

Let the ConsoleService track stdout and stderr messages - #152

Merged
ctrueden merged 15 commits into
masterfrom
stdout-stderr
Mar 21, 2015
Merged

ctrueden merged 15 commits into
masterfrom
stdout-stderr

Conversation

@ctrueden

Copy link
Copy Markdown
Member

This branch adds API so that interested parties can register as listeners for stdout and stderr. But not just any old stdout and stderr messages—only those emitted by threads associated with the relevant SciJava application context. "Pretty snazzy," I hear you say. Well you're darn tootin' it is!

This will come in handy shortly.
These don't do anything in an interface declaration.
This method tells you whether the given thread was spawned
by the same SciJava Context as the ThreadService.
@ctrueden

Copy link
Copy Markdown
Member Author

@hinerm This branch is not yet ready for merge. It still needs unit tests. But I would greatly appreciate feedback on the design!

@ctrueden
ctrueden force-pushed the stdout-stderr branch 7 times, most recently from 07920d0 to cf55394 Compare March 20, 2015 22:14
ctrueden and others added 11 commits March 20, 2015 22:55
At a given time, each Thread can only have one parent Thread, even
if there are multiple application Contexts in play each with its
own ThreadService.

Without this change, the new getThreadContext method will sometimes fail
to return ThreadContext.OTHER as expected, because thread parents were
only tracked by the spawning ThreadService, and hence unavailable to the
other ThreadService doing the interrogation.
This will be extremely useful for intercepting stdout and stderr output
without repeatedly calling System.setOut/setErr and battling over who
gets to be "the guy." Everyone can be the guy -- they just have to add
themselves to the MultiOutputStream.
This goes hand in hand with the previous commit: by overriding stdout
and stderr with MultiPrintStream instances (each backed by a
MultiOutputStream), any interested OutputStreams can add themselves
to the stdout/stderr pipeline without needing to call System.setOut/Err.

This avoids stomping on other interested OutputStreams, and avoids
creating arbitrarily long PrintStream chains which cannot later be
disentangled (since PrintStream has no API for accessing or resetting
its parent after construction).
This event, though it is a SciJavaEvent, is _not_ intended for
publication via the EventService, but rather via the ConsoleService.
Now, interested parties can add themselves as OutputListeners, and they
will receive notification when output occurs to stdout and stderr.

One cool thing is that the notifications are limited to output produced
by threads _of this SciJava Context_. This is achieved via the new
ThreadService#getThreadContext(Thread) method, which checks the calling
thread to see whether its Context matches this one.

Actually, this commit does not _quite_ finish adding working support for
this feature. One more piece is needed -- see the next commit.
This change completes the loop, such that stdout and stderr messages
are now reported back to all ConsoleService listeners as desired.

When output occurs, something needs to tell the ConsoleService, so that
it can tell all its listeners what happened. That's where the
OutputStreamReporter comes in: it is added to the MultiOutputStream,
which is wrapped by a MultiPrintStream, which overrides the default
System.out or System.err, such that when output occurs, the
OutputStreamReporter finds out and calls ConsoleService#notifyListeners.

This change also cleans up after itself when the context is disposed,
removing the OutputStreamReporters as appropriate.
When streams are added or removed, we do not want that to interfere with
concurrent stream operations (i.e., writing, closing and flushing).
But we also do not particularly want to incur the heavy performance
penalty of synchronizing all the stream operations, either. So let's
keep a copy of the streams, which is regenerated whenever stream is
added or removed from the mix, and use that copy when iterating over the
streams during the stream operations.

Similarly, when listeners are added or removed from the ConsoleService,
we use the same trick to avoid the need to synchronize listener
notification every time an output event occurs.

Signed-off-by: Curtis Rueden <ctrueden@wisc.edu>
This code tests that the OutputListeners receive the proper
notifications for both stdout and stderr when output is emitted
in various ways. It also verifies that output is properly
classified when multiple simultaneous Contexts are in play.
@ctrueden

Copy link
Copy Markdown
Member Author

Hooray, everything is working now!

ctrueden added a commit that referenced this pull request Mar 21, 2015
Let the ConsoleService track stdout and stderr messages
@ctrueden
ctrueden merged commit 28f717b into master Mar 21, 2015
@ctrueden
ctrueden deleted the stdout-stderr branch March 21, 2015 04:11
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants