Redirect cout and cerr to System.out and System.err

W

Will Lichtenberger

I have a java application that calls, via JNI, many VC++ dlls. The
java application ports System.out and System.err (among other things)
to a file. I would like to capture the cout from the dlls as well.

When running the application from a command line or a debugger, the
couts do show up in the console. It seems if the console is getting
the output from all, then I should be able to capture the couts as
well.

I've considered redirecting the cout to the same file that the
System.out and System.err are written too, but I feel that would cause
extra problems with the JVM and dll sharing the same file.

Anyone have any ideas? Thanks

Note: This is my second attempt to post this, so my apologies if it
appears twice.
 
A

Arne Vajhøj

Will said:
I have a java application that calls, via JNI, many VC++ dlls. The
java application ports System.out and System.err (among other things)
to a file. I would like to capture the cout from the dlls as well.

When running the application from a command line or a debugger, the
couts do show up in the console. It seems if the console is getting
the output from all, then I should be able to capture the couts as
well.

I've considered redirecting the cout to the same file that the
System.out and System.err are written too, but I feel that would cause
extra problems with the JVM and dll sharing the same file.

I was looking into this problem a couple of years.

The solution I came up with was to create some JNI code
with two methods:
- one that redirect stdout to a pipe
- one that get the output stored there

Then the fow is:
- call the function that redirect
- make multiple other JNI calls
- fetch the output and write it where I want it

A bit cumbersome, but I was not able to come up with a
better solution.

:)

You should be able to see the code at:
http://www.vajhoej.dk/arne/eksperten/jniredirect/

Arne
 
G

Gordon Beaton

I was looking into this problem a couple of years.

The solution I came up with was to create some JNI code
with two methods:
- one that redirect stdout to a pipe
- one that get the output stored there

Then the fow is:
- call the function that redirect
- make multiple other JNI calls
- fetch the output and write it where I want it

A bit cumbersome, but I was not able to come up with a
better solution.

From native code you can get the file descriptors that correspond to
System.in and System.out using a technique similar to this one:
http://groups.google.com/group/comp.lang.java.programmer/msg/2d7bf4a019c66825

Once you've got that, you can either use fdopen() to get a new stream
and always specify the target stream in your code, or (on Unix anyway)
use dup2() to make the stdin/stdout descriptors be duplicates of
System.in/System.out.

/gordon

--
 
Joined
Nov 5, 2008
Messages
1
Reaction score
0
how to do what you tell ?

please, can you show us the sample source code from the C and Java ?

how do you convert the java object java.io.FileDescriptor static out to your argument in the C function dup2 ? or do you get the handle ?

thanks
dener
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top