Skip to content

Commit

Permalink
+ buffering standard out file streams
Browse files Browse the repository at this point in the history
  • Loading branch information
q3769 committed Jun 10, 2023
1 parent 1a15873 commit 69715e7
Showing 1 changed file with 56 additions and 25 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,17 @@

package elf4j.engine.service.writer;

import elf4j.engine.service.LogServiceManager;
import elf4j.engine.service.Stoppable;
import elf4j.engine.service.configuration.LogServiceConfiguration;
import elf4j.util.IeLogger;
import lombok.NonNull;
import lombok.ToString;

import java.io.*;
import java.util.Properties;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
*
Expand All @@ -39,7 +44,7 @@
public class FileStreamStandardOutput implements StandardOutput {
private static final OutStreamType DEFAULT_OUT_STREAM_TYPE = OutStreamType.STDOUT;
@NonNull private final OutStreamType outStreamType;
private final StandardFileOutputStreams standardFileOutputStreams = new StandardFileOutputStreams();
private final StandardOutputStreams standardOutputStreams = new StandardOutputStreams();

/**
* @param outStreamType
Expand All @@ -65,9 +70,9 @@ private FileStreamStandardOutput(@NonNull OutStreamType outStreamType) {
@Override
public void write(byte[] bytes) {
if (this.outStreamType == OutStreamType.STDERR) {
standardFileOutputStreams.writeErr(bytes);
standardOutputStreams.err(bytes);
} else {
standardFileOutputStreams.writeOut(bytes);
standardOutputStreams.out(bytes);
}
}

Expand All @@ -76,38 +81,64 @@ enum OutStreamType {
STDERR
}

static class StandardFileOutputStreams {
final OutputStream stdoutFos = new FileOutputStream(FileDescriptor.out);
final OutputStream stderrFos = new FileOutputStream(FileDescriptor.err);
static class StandardOutputStreams implements Stoppable.Output {
final OutputStream stdout = new BufferedOutputStream(new FileOutputStream(FileDescriptor.out));
final OutputStream stderr = new BufferedOutputStream(new FileOutputStream(FileDescriptor.err));

StandardFileOutputStreams() {
final Lock lock = new ReentrantLock();
boolean stopped;

StandardOutputStreams() {
LogServiceManager.INSTANCE.registerStop(this);
}

@Override
public void stop() {
IeLogger.INFO.log("Stopping {}", this);
try (AutoCloseable out = stdout; AutoCloseable err = stderr) {
this.stopped = true;
} catch (Exception e) {
IeLogger.WARN.log(e, "Error closing {} or {}", stdout, stderr);
throw new IllegalStateException(e);
}
}

void writeErr(byte[] bytes) {
doErr(bytes);
@Override
public boolean isStopped() {
return this.stopped;
}

void writeOut(byte[] bytes) {
doOut(bytes);
void err(byte[] bytes) {
lock.lock();
try {
writeErr(bytes);
} finally {
lock.unlock();
}
}

void out(byte[] bytes) {
lock.lock();
try {
writeOut(bytes);
} finally {
lock.unlock();
}
}

private void doErr(byte[] bytes) {
synchronized (System.err) {
try {
stderrFos.write(bytes);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
private void writeErr(byte[] bytes) {
try {
stderr.write(bytes);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}

private void doOut(byte[] bytes) {
synchronized (System.out) {
try {
stdoutFos.write(bytes);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
private void writeOut(byte[] bytes) {
try {
stdout.write(bytes);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
Expand Down

0 comments on commit 69715e7

Please sign in to comment.