Getting full process output in listener
Answered
Hello,
In my IntelliJ plugin I want to grab the output done by processes running in IntelliJ. I add a ProcessListener to the ProcessHandler and in onTextAvailable I get the output. But for output that consists of multiple lines, on TextAvailable is called for every single line, so I do not see where the output starts and ends. I would like to get the whole output at once that was put out using e.g. LOGGER.debug.
I see that as the ProcessHandler is a KillableColoredProcessHandler, the option splitToLines is used. But I see no possibility to change this at runtime.
Is there a way to get the full output at once?
Please sign in to leave a comment.
Hi,
There is no API for getting the full process output as processes may be executed for a very long time and contain huge amount of text.
It is not recommended to log the full process output. If you need it for development purposes, you can accumulate output in a StringBuilder in ProcessListener and log it when the process is terminated.
Hello Karol,
by whole output, I do not mean the whole output of a process at once, but the whole output of a single output of the process.
For example, if a process calls Console.writeln("Some line\nAnother line") and afterwards Console.writeln("Second line\nAnother second line"), ProcessListener::onTextAvailable is called four times with “Some line”, “Another line”, “Second line” and “Another second line”. That way, I am not able to determine what belongs to the first and what belongs to the second output.
Is there a way to do this?
Hi,
Sorry for misunderstanding. I get it now.
If you have control over process handler implementation, and your handler extends BaseOSProcessHandler, you can override:
to return output reader extending BaseOutputReader with the options returning true from splitToLines().
See https://github.com/JetBrains/intellij-community/blob/master/platform/util/src/com/intellij/execution/process/BaseOSProcessHandler.java#L117
If you don't control handler implementation, I don't see a possibility to set it at runtime.
Hello Karol,
I was afraid of this…
The processes are default Spring configurations run inIntelliJ. Maybe I could change the handler that will be started when Run XXX button is pressed? I see that at the moment it is KillableColoredProcessHandler.
Unfortunately, there is no EP for changing the process handler.
I see. Thank you very much for your kind help.