Burger Wilhelm | 18 Feb 13:46 2013
Picon

Using System.out and System.err from ImageJ plugins (Java)

Hello all,

I would like to share a piece of Java code for ImageJ that I wrote out of need and perhaps others might find
useful too:

This small library facilitates the redirection of output to System.out and System.err to the ImageJ
Log-console. Typically in ImageJ output sent to the standard system streams by calls such as
System.out.println(...) are suppressed. Redirecting the standard output streams makes this output
appear in ImageJ's log console, analogous to using the IJ.log() method. This is particularly useful for
debugging imported code and for writing classes that are independent of ImageJ.

More details and the complete code with examples can be found here:
http://staff.fh-hagenberg.at/burger/imagej/index.html

Comments and suggestions for improvements are welcome.

Best regards,
Wilhelm

www.imagingbook.com
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

Johannes Schindelin | 18 Feb 16:18 2013
Picon
Picon

Re: Using System.out and System.err from ImageJ plugins (Java)

Hi Wilhelm,

On Mon, 18 Feb 2013, Burger Wilhelm wrote:

> I would like to share a piece of Java code for ImageJ that I wrote out
> of need and perhaps others might find useful too:
> 
> This small library facilitates the redirection of output to System.out
> and System.err to the ImageJ Log-console. Typically in ImageJ output
> sent to the standard system streams by calls such as
> System.out.println(...) are suppressed. Redirecting the standard output
> streams makes this output appear in ImageJ's log console, analogous to
> using the IJ.log() method. This is particularly useful for debugging
> imported code and for writing classes that are independent of ImageJ.

Note that Fiji writes stdout and stderr to the ImageJ Log window in
addition to the console (if there is one) if you switch on debug mode via
Edit>Options>Misc...

Ciao,
Johannes

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

Burger Wilhelm | 18 Feb 16:42 2013
Picon

Re: Using System.out and System.err from ImageJ plugins (Java)

Good to know! -- suppose this would have saved me some troubles ...

Thanks much,
Wilhelm

________________________________________
From: Johannes Schindelin [Johannes.Schindelin@...]
Sent: Monday, February 18, 2013 16:18
To: Burger Wilhelm
Cc: IMAGEJ@...
Subject: Re: Using System.out and System.err from ImageJ plugins (Java)

Hi Wilhelm,

On Mon, 18 Feb 2013, Burger Wilhelm wrote:

> I would like to share a piece of Java code for ImageJ that I wrote out
> of need and perhaps others might find useful too:
>
> This small library facilitates the redirection of output to System.out
> and System.err to the ImageJ Log-console. Typically in ImageJ output
> sent to the standard system streams by calls such as
> System.out.println(...) are suppressed. Redirecting the standard output
> streams makes this output appear in ImageJ's log console, analogous to
> using the IJ.log() method. This is particularly useful for debugging
> imported code and for writing classes that are independent of ImageJ.

Note that Fiji writes stdout and stderr to the ImageJ Log window in
addition to the console (if there is one) if you switch on debug mode via
Edit>Options>Misc...
(Continue reading)

Picon

Re: Using System.out and System.err from ImageJ plugins (Java)

On Feb 18, 2013, at 7:46 AM, Burger Wilhelm wrote:

> Hello all,
> 
> I would like to share a piece of Java code for ImageJ that I wrote out of need and perhaps others might find
useful too:
> 
> This small library facilitates the redirection of output to System.out and System.err to the ImageJ
Log-console. Typically in ImageJ output sent to the standard system streams by calls such as
System.out.println(...) are suppressed. Redirecting the standard output streams makes this output
appear in ImageJ's log console, analogous to using the IJ.log() method. This is particularly useful for
debugging imported code and for writing classes that are independent of ImageJ.
> 
> More details and the complete code with examples can be found here:
> http://staff.fh-hagenberg.at/burger/imagej/index.html
> 
> Comments and suggestions for improvements are welcome.

This library is built into the ImageJ 1.47m daily build as the ij.io.LogStream class. You can redirect
System.out and System.err messages to the Log window by enabling debug mode in Edit>Options>Misc.

-wayne
--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

Johannes Schindelin | 27 Feb 21:29 2013
Picon
Picon

Re: Using System.out and System.err from ImageJ plugins (Java)

Hi,

On Wed, 27 Feb 2013, Rasband, Wayne (NIH/NIMH) [E] wrote:

> On Feb 18, 2013, at 7:46 AM, Burger Wilhelm wrote:
> 
> > I would like to share a piece of Java code for ImageJ that I wrote out
> > of need and perhaps others might find useful too:
> > 
> > This small library facilitates the redirection of output to System.out
> > and System.err to the ImageJ Log-console. Typically in ImageJ output
> > sent to the standard system streams by calls such as
> > System.out.println(...) are suppressed. Redirecting the standard
> > output streams makes this output appear in ImageJ's log console,
> > analogous to using the IJ.log() method. This is particularly useful
> > for debugging imported code and for writing classes that are
> > independent of ImageJ.
> > 
> > More details and the complete code with examples can be found here:
> > http://staff.fh-hagenberg.at/burger/imagej/index.html
> > 
> > Comments and suggestions for improvements are welcome.
> 
> This library is built into the ImageJ 1.47m daily build as the
> ij.io.LogStream class. You can redirect System.out and System.err
> messages to the Log window by enabling debug mode in Edit>Options>Misc.

I am glad you liked my approach, but I would have preferred it if my code
had just been reused... it was unnecessary to reinvent the wheel.

(Continue reading)

Johannes Schindelin | 27 Feb 21:45 2013
Picon
Picon

Re: Using System.out and System.err from ImageJ plugins (Java)

Dear Wayne,

On Wed, 27 Feb 2013, Johannes Schindelin wrote:

> On Wed, 27 Feb 2013, Rasband, Wayne (NIH/NIMH) [E] wrote:
> 
> > You can redirect System.out and System.err messages to the Log window
> > by enabling debug mode in Edit>Options>Misc.
> 
> [...] this interferes with Fiji's stdout/stderr handling. I am fixing it
> as we speak.

I just tested this Javascript after downloading and starting
http://imagej.net/ij.jar:

	importClass(Packages.java.lang.System);
	System.err.println("Hello");

It did not interfere with Fiji's redirection -- unexpectedly.

I tried to find out by studying the source code, but the newest commit did
not show how the redirection works:

http://fiji.sc/cgi-bin/gitweb.cgi?p=imagej.git;a=commitdiff;h=83f7efc605f666626f5f78d10bbe5c7d52b75e61

Indeed, building from the current 'master' of your repository does not
redirect stdout/stderr when the debug mode is enabled.

Ciao,
Johannes
(Continue reading)

Johannes Schindelin | 27 Feb 22:24 2013
Picon
Picon

Re: Using System.out and System.err from ImageJ plugins (Java)

Dear Wayne,

On Wed, 27 Feb 2013, Johannes Schindelin wrote:

> On Wed, 27 Feb 2013, Johannes Schindelin wrote:
> 
> > On Wed, 27 Feb 2013, Rasband, Wayne (NIH/NIMH) [E] wrote:
> > 
> > > You can redirect System.out and System.err messages to the Log
> > > window by enabling debug mode in Edit>Options>Misc.
> > 
> > [...] this interferes with Fiji's stdout/stderr handling. I am fixing
> > it as we speak.
> 
> I just tested this Javascript after downloading and starting
> http://imagej.net/ij.jar:
> 
> 	importClass(Packages.java.lang.System);
> 	System.err.println("Hello");
> 
> It did not interfere with Fiji's redirection -- unexpectedly.

I found out why it did not work in my test: I activated the debug mode
using the -debug command-line option. And while it seems that the very
same code was added in three places (ij.macro.Functions#setOption(),
ij.plugin.Options#miscOptions() and ij.text.TextWindow#close(boolean)), it
was not added at the two other places in ij.jar where debugMode can be
activated: ij.macro.Functions#restoreSettings() and
ij.ImageJ#main(String[]).

(Continue reading)

Johannes Schindelin | 27 Feb 22:29 2013
Picon
Picon

Re: Using System.out and System.err from ImageJ plugins (Java)

Dear Wayne,

On Wed, 27 Feb 2013, Johannes Schindelin wrote:

> [...] causing an infinite loop. You can easily reproduce that issue with
> this command-line:
> 
> 	java -jar ij.jar -eval \
> 		'setOption("debugmode", 1); eval("script", "importClass(Packages.java.lang.System);
System.err.println(\"Hello\");");' \
> 		-batch

An easier command-line that also demonstrates that this is not some looney
idea of mine but a real-world scenario:

	java -jar ij.jar -eval \
		'setOption("debugmode", 1); print("Hello, Wayne");' -batch

I also failed to mention that this just exits without an error message
(but exit code 1) because the exception that is thrown when stack space is
up is swallowed by the fact that Macro_Runner#runMacro(String, String)
cannot print to stderr due to the very infinte loop.

Ciao,
Johannes

--
ImageJ mailing list: http://imagej.nih.gov/ij/list.html

(Continue reading)


Gmane