1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17 package org.apache.logging.log4j.core.appender;
18
19 import java.io.IOException;
20 import java.io.OutputStream;
21 import java.io.Serializable;
22 import java.nio.ByteBuffer;
23 import java.util.Objects;
24 import java.util.concurrent.TimeUnit;
25
26 import org.apache.logging.log4j.core.Layout;
27 import org.apache.logging.log4j.core.LoggerContext;
28 import org.apache.logging.log4j.core.layout.ByteBufferDestination;
29 import org.apache.logging.log4j.core.util.Constants;
30
31
32
33
34
35 public class OutputStreamManager extends AbstractManager implements ByteBufferDestination {
36 protected final Layout<?> layout;
37 protected ByteBuffer byteBuffer;
38 private volatile OutputStream os;
39 private boolean skipFooter;
40
41 protected OutputStreamManager(final OutputStream os, final String streamName, final Layout<?> layout,
42 final boolean writeHeader) {
43
44 this(os, streamName, layout, writeHeader, Constants.ENCODER_BYTE_BUFFER_SIZE);
45 }
46
47 protected OutputStreamManager(final OutputStream os, final String streamName, final Layout<?> layout,
48 final boolean writeHeader, final int bufferSize) {
49
50 this(os, streamName, layout, writeHeader, ByteBuffer.wrap(new byte[bufferSize]));
51 }
52
53
54
55
56
57 @Deprecated
58 protected OutputStreamManager(final OutputStream os, final String streamName, final Layout<?> layout,
59 final boolean writeHeader, final ByteBuffer byteBuffer) {
60 super(null, streamName);
61 this.os = os;
62 this.layout = layout;
63 if (writeHeader && layout != null) {
64 final byte[] header = layout.getHeader();
65 if (header != null) {
66 try {
67 getOutputStream().write(header, 0, header.length);
68 } catch (final IOException e) {
69 logError("Unable to write header", e);
70 }
71 }
72 }
73 this.byteBuffer = Objects.requireNonNull(byteBuffer, "byteBuffer");
74 }
75
76
77
78
79 protected OutputStreamManager(final LoggerContext loggerContext, final OutputStream os, final String streamName,
80 final boolean createOnDemand, final Layout<? extends Serializable> layout, final boolean writeHeader,
81 final ByteBuffer byteBuffer) {
82 super(loggerContext, streamName);
83 if (createOnDemand && os != null) {
84 LOGGER.error(
85 "Invalid OutputStreamManager configuration for '{}': You cannot both set the OutputStream and request on-demand.",
86 streamName);
87 }
88 this.layout = layout;
89 this.byteBuffer = Objects.requireNonNull(byteBuffer, "byteBuffer");
90 this.os = os;
91 if (writeHeader && layout != null) {
92 final byte[] header = layout.getHeader();
93 if (header != null) {
94 try {
95 getOutputStream().write(header, 0, header.length);
96 } catch (final IOException e) {
97 logError("Unable to write header for " + streamName, e);
98 }
99 }
100 }
101 }
102
103
104
105
106
107
108
109
110
111
112 public static <T> OutputStreamManager getManager(final String name, final T data,
113 final ManagerFactory<? extends OutputStreamManager, T> factory) {
114 return AbstractManager.getManager(name, factory, data);
115 }
116
117 @SuppressWarnings("unused")
118 protected OutputStream createOutputStream() throws IOException {
119 throw new IllegalStateException(getClass().getCanonicalName() + " must implement createOutputStream()");
120 }
121
122
123
124
125
126 public void skipFooter(final boolean skipFooter) {
127 this.skipFooter = skipFooter;
128 }
129
130
131
132
133 @Override
134 public boolean releaseSub(final long timeout, final TimeUnit timeUnit) {
135 writeFooter();
136 return closeOutputStream();
137 }
138
139
140
141
142 protected void writeFooter() {
143 if (layout == null || skipFooter) {
144 return;
145 }
146 final byte[] footer = layout.getFooter();
147 if (footer != null) {
148 write(footer);
149 }
150 }
151
152
153
154
155
156 public boolean isOpen() {
157 return getCount() > 0;
158 }
159
160 protected OutputStream getOutputStream() throws IOException {
161 if (os == null) {
162 os = createOutputStream();
163 }
164 return os;
165 }
166
167 protected void setOutputStream(final OutputStream os) {
168 final byte[] header = layout.getHeader();
169 if (header != null) {
170 try {
171 os.write(header, 0, header.length);
172 this.os = os;
173 } catch (final IOException ioe) {
174 logError("Unable to write header", ioe);
175 }
176 } else {
177 this.os = os;
178 }
179 }
180
181
182
183
184
185
186 protected void write(final byte[] bytes) {
187 write(bytes, 0, bytes.length, false);
188 }
189
190
191
192
193
194
195
196 protected void write(final byte[] bytes, final boolean immediateFlush) {
197 write(bytes, 0, bytes.length, immediateFlush);
198 }
199
200
201
202
203
204
205
206
207
208 protected void write(final byte[] bytes, final int offset, final int length) {
209 write(bytes, offset, length, false);
210 }
211
212
213
214
215
216
217
218
219
220
221 protected synchronized void write(final byte[] bytes, final int offset, final int length, final boolean immediateFlush) {
222 if (immediateFlush && byteBuffer.position() == 0) {
223 writeToDestination(bytes, offset, length);
224 flushDestination();
225 return;
226 }
227 if (length >= byteBuffer.capacity()) {
228
229 flush();
230 writeToDestination(bytes, offset, length);
231 } else {
232 if (length > byteBuffer.remaining()) {
233 flush();
234 }
235 byteBuffer.put(bytes, offset, length);
236 }
237 if (immediateFlush) {
238 flush();
239 }
240 }
241
242
243
244
245
246
247
248
249
250 protected synchronized void writeToDestination(final byte[] bytes, final int offset, final int length) {
251 try {
252 getOutputStream().write(bytes, offset, length);
253 } catch (final IOException ex) {
254 throw new AppenderLoggingException("Error writing to stream " + getName(), ex);
255 }
256 }
257
258
259
260
261
262 protected synchronized void flushDestination() {
263 final OutputStream stream = os;
264 if (stream != null) {
265 try {
266 stream.flush();
267 } catch (final IOException ex) {
268 throw new AppenderLoggingException("Error flushing stream " + getName(), ex);
269 }
270 }
271 }
272
273
274
275
276
277
278
279
280
281 protected synchronized void flushBuffer(final ByteBuffer buf) {
282 buf.flip();
283 if (buf.limit() > 0) {
284 writeToDestination(buf.array(), 0, buf.limit());
285 }
286 buf.clear();
287 }
288
289
290
291
292 public synchronized void flush() {
293 flushBuffer(byteBuffer);
294 flushDestination();
295 }
296
297 protected synchronized boolean closeOutputStream() {
298 flush();
299 final OutputStream stream = os;
300 if (stream == null || stream == System.out || stream == System.err) {
301 return true;
302 }
303 try {
304 stream.close();
305 } catch (final IOException ex) {
306 logError("Unable to close stream", ex);
307 return false;
308 }
309 return true;
310 }
311
312
313
314
315
316
317 @Override
318 public ByteBuffer getByteBuffer() {
319 return byteBuffer;
320 }
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339 @Override
340 public ByteBuffer drain(final ByteBuffer buf) {
341 flushBuffer(buf);
342 return buf;
343 }
344 }