Reading from stdout

W

White Spirit

I'm writing a unit test for a logger class that writes to stdout. The
logger class uses an OutputStreamWriter to wrap FileDescriptor.out:

mOut = new OutputStreamWriter(new
FileOutputStream(FileDescriptor.out), "UTF-8");

My unit test instantiates the logger class as a local object. When I
try to read from FileDescriptor.out in my unit test class, however,
nothing is received:

BufferedReader reader = new BufferedReader( new InputStreamReader( new
FileInputStream( FileDescriptor.out ) ) );
// do some logging
// ...
String n = reader.readLine();

Is there any way of reading what the logger is outputting?
 
D

Daniel Pitts

I'm writing a unit test for a logger class that writes to stdout. The
logger class uses an OutputStreamWriter to wrap FileDescriptor.out:

mOut = new OutputStreamWriter(new
FileOutputStream(FileDescriptor.out), "UTF-8");

My unit test instantiates the logger class as a local object. When I
try to read from FileDescriptor.out in my unit test class, however,
nothing is received:

BufferedReader reader = new BufferedReader( new InputStreamReader( new
FileInputStream( FileDescriptor.out ) ) );
// do some logging
// ...
String n = reader.readLine();

Is there any way of reading what the logger is outputting?

If you allow the Logger to accept an OutputStream, then you can pass in
a new ByteArrayOutputStream().

final ByteArrayOutputStream baos = new ByteArrayOutputStream();
final Logger logger = new Logger(baos);
doSomeLogging(logger);
String result = baos.toString(); // uses default encoding.
assertEqual(expectedResult, result);
 
R

Roedy Green

I'm writing a unit test for a logger class that writes to stdout.

Your logger could write to a file, with a .flush() and
..getFD().sync(). Then you could read from that file.
 
A

Arne Vajhøj

I'm writing a unit test for a logger class that writes to stdout. The
logger class uses an OutputStreamWriter to wrap FileDescriptor.out:

mOut = new OutputStreamWriter(new
FileOutputStream(FileDescriptor.out), "UTF-8");

My unit test instantiates the logger class as a local object. When I
try to read from FileDescriptor.out in my unit test class, however,
nothing is received:

BufferedReader reader = new BufferedReader( new InputStreamReader( new
FileInputStream( FileDescriptor.out ) ) );
// do some logging
// ...
String n = reader.readLine();

Is there any way of reading what the logger is outputting?

If that logger class had been well behaving and used System.out
as the documentation recommends, then it would be easy to do
with System.out, but this way it is more tricky because
FileDescriptor.out refers directly to some native OS ref.

The best thing would be to change that logger class to use
System.out.

If not then I think you would need to dive deep into some
JNI and native calls.

Arne
 
M

markspace

I'm writing a unit test for a logger class that writes to stdout. The
logger class uses an OutputStreamWriter to wrap FileDescriptor.out:

mOut = new OutputStreamWriter(new
FileOutputStream(FileDescriptor.out), "UTF-8");

My unit test instantiates the logger class as a local object. When I
try to read from FileDescriptor.out in my unit test class, however,
nothing is received:

BufferedReader reader = new BufferedReader( new InputStreamReader( new
FileInputStream( FileDescriptor.out ) ) );
// do some logging
// ...
String n = reader.readLine();

Is there any way of reading what the logger is outputting?


I've run into this problem before as well, and I concluded the same as
Daniel: All output should go to a plain vanilla OutputStream (or a
Writer). Then you just wrap where you really want the output to go in a
plain stream of some sort, and you're set.

Anything else, and you're pretty much fooked.

I don't have any good suggestions for you, I'm sorry. Other than
refactor the whole thing to use regular logging logging instead.

So, why aren't use using the regular logging package? Because there's
standard loggers, StreamHandler or MemoryHandler, that would do what you
want pretty easily, allowing you to inject a stream to intercept and
test logging output.
 

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,580
Members
45,055
Latest member
SlimSparkKetoACVReview

Latest Threads

Top