Repository navigation
Let the ConsoleService track stdout and stderr messages - #152
Merged
Merged
Conversation
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.
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
force-pushed
the
stdout-stderr
branch
7 times, most recently
from
March 20, 2015 22:14
07920d0 to
cf55394
Compare
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.
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
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
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!