001/*
002 * Licensed to the Apache Software Foundation (ASF) under one or more
003 * contributor license agreements. See the NOTICE file distributed with
004 * this work for additional information regarding copyright ownership.
005 * The ASF licenses this file to You under the Apache license, Version 2.0
006 * (the "License"); you may not use this file except in compliance with
007 * the License. You may obtain a copy of the License at
008 *
009 *      http://www.apache.org/licenses/LICENSE-2.0
010 *
011 * Unless required by applicable law or agreed to in writing, software
012 * distributed under the License is distributed on an "AS IS" BASIS,
013 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014 * See the license for the specific language governing permissions and
015 * limitations under the license.
016 */
017package org.apache.logging.log4j.core.appender;
018
019import java.io.IOException;
020import java.io.OutputStream;
021
022import org.apache.logging.log4j.core.Layout;
023
024/**
025 * Manage an OutputStream so that it can be shared by multiple Appenders and will
026 * allow appenders to reconfigure without requiring a new stream.
027 */
028public class OutputStreamManager extends AbstractManager {
029
030    private volatile OutputStream os;
031
032    private final byte[] footer;
033    private final byte[] header;
034
035    protected OutputStreamManager(final OutputStream os, final String streamName, final Layout<?> layout) {
036        super(streamName);
037        this.os = os;
038        if (layout != null) {
039            this.footer = layout.getFooter();
040            this.header = layout.getHeader();
041            if (this.header != null) {
042                try {
043                    this.os.write(header, 0, header.length);
044                } catch (final IOException ioe) {
045                    LOGGER.error("Unable to write header", ioe);
046                }
047            }
048        } else {
049            this.footer = null;
050            this.header = null;
051        }
052    }
053
054    /**
055     * Create a Manager.
056     *
057     * @param name The name of the stream to manage.
058     * @param data The data to pass to the Manager.
059     * @param factory The factory to use to create the Manager.
060     * @param <T> The type of the OutputStreamManager.
061     * @return An OutputStreamManager.
062     */
063    public static <T> OutputStreamManager getManager(final String name, final T data,
064                                                 final ManagerFactory<? extends OutputStreamManager, T> factory) {
065        return AbstractManager.getManager(name, factory, data);
066    }
067
068    /**
069     * Default hook to write footer during close.
070     */
071    @Override
072    public void releaseSub() {
073        if (footer != null) {
074            write(footer);
075        }
076        close();
077    }
078
079    /**
080     * Returns the status of the stream.
081     * @return true if the stream is open, false if it is not.
082     */
083    public boolean isOpen() {
084        return getCount() > 0;
085    }
086
087    protected OutputStream getOutputStream() {
088        return os;
089    }
090
091    protected void setOutputStream(final OutputStream os) {
092        if (header != null) {
093            try {
094                os.write(header, 0, header.length);
095                this.os = os; // only update field if os.write() succeeded
096            } catch (final IOException ioe) {
097                LOGGER.error("Unable to write header", ioe);
098            }
099        } else {
100            this.os = os;
101        }
102    }
103
104    /**
105     * Some output streams synchronize writes while others do not. Synchronizing here insures that
106     * log events won't be intertwined.
107     * @param bytes The serialized Log event.
108     * @param offset The offset into the byte array.
109     * @param length The number of bytes to write.
110     * @throws AppenderLoggingException if an error occurs.
111     */
112    protected synchronized void write(final byte[] bytes, final int offset, final int length)  {
113        //System.out.println("write " + count);
114        try {
115            os.write(bytes, offset, length);
116        } catch (final IOException ex) {
117            final String msg = "Error writing to stream " + getName();
118            throw new AppenderLoggingException(msg, ex);
119        }
120    }
121
122    /**
123     * Some output streams synchronize writes while others do not. Synchronizing here insures that
124     * log events won't be intertwined.
125     * @param bytes The serialized Log event.
126     * @throws AppenderLoggingException if an error occurs.
127     */
128    protected void write(final byte[] bytes)  {
129        write(bytes, 0, bytes.length);
130    }
131
132    protected synchronized void close() {
133        final OutputStream stream = os; // access volatile field only once per method
134        if (stream == System.out || stream == System.err) {
135            return;
136        }
137        try {
138            stream.close();
139        } catch (final IOException ex) {
140            LOGGER.error("Unable to close stream " + getName() + ". " + ex);
141        }
142    }
143
144    /**
145     * Flush any buffers.
146     */
147    public synchronized void flush() {
148        try {
149            os.flush();
150        } catch (final IOException ex) {
151            final String msg = "Error flushing stream " + getName();
152            throw new AppenderLoggingException(msg, ex);
153        }
154    }
155}