| 35 | import java.io.Closeable; |
| 36 | |
| 37 | public class LogFileWriter extends SynchronizedJob implements Closeable, LogWriter { |
| 38 | |
| 39 | private static final int DEFAULT_BUFFER_SIZE = 1024 * 1024; |
| 40 | private final int level; |
| 41 | private final RingQueue<LogRecordUtf8Sink> ring; |
| 42 | private final SCSequence subSeq; |
| 43 | private long _wptr; |
| 44 | private long buf; |
| 45 | private int bufSize; |
| 46 | private String bufferSize; |
| 47 | private long fd = -1; |
| 48 | private long lim; |
| 49 | private String location; |
| 50 | private QueueConsumer<LogRecordUtf8Sink> myConsumer = this::copyToBuffer; |
| 51 | // can be set via reflection |
| 52 | @SuppressWarnings("unused") |
| 53 | private String truncate; |
| 54 | |
| 55 | public LogFileWriter(RingQueue<LogRecordUtf8Sink> ring, SCSequence subSeq, int level) { |
| 56 | this.ring = ring; |
| 57 | this.subSeq = subSeq; |
| 58 | this.level = level; |
| 59 | } |
| 60 | |
| 61 | @Override |
| 62 | public void bindProperties(LogFactory factory) { |
| 63 | if (this.bufferSize != null) { |
| 64 | try { |
| 65 | bufSize = Numbers.parseIntSize(this.bufferSize); |
| 66 | } catch (NumericException e) { |
| 67 | throw new LogError("Invalid value for bufferSize"); |
| 68 | } |
| 69 | } else { |
| 70 | bufSize = DEFAULT_BUFFER_SIZE; |
| 71 | } |
| 72 | this.buf = _wptr = Unsafe.malloc(bufSize, MemoryTag.NATIVE_LOGGER); |
| 73 | this.lim = buf + bufSize; |
| 74 | try (Path path = new Path()) { |
| 75 | path.of(location); |
| 76 | if (truncate != null && Chars.equalsLowerCaseAscii(truncate, "true")) { |
| 77 | this.fd = Files.openRW(path.$()); |
| 78 | Files.truncate(fd, 0); |
| 79 | } else { |
| 80 | this.fd = Files.openAppend(path.$()); |
| 81 | } |
| 82 | } |
| 83 | if (this.fd == -1) { |
| 84 | throw new LogError("Cannot open file for append: " + location + " [errno=" + Os.errno() + ']'); |
| 85 | } |
| 86 | } |
| 87 | |
| 88 | @Override |
| 89 | public void close() { |
| 90 | if (buf != 0) { |
| 91 | if (_wptr > buf) { |
| 92 | flush(); |
| 93 | } |
| 94 | Unsafe.free(buf, bufSize, MemoryTag.NATIVE_LOGGER); |
nothing calls this directly
no outgoing calls
no test coverage detected
searching dependent graphs…