001/**
002 * Logback: the reliable, generic, fast and flexible logging framework.
003 * Copyright (C) 1999-2015, QOS.ch. All rights reserved.
004 *
005 * This program and the accompanying materials are dual-licensed under
006 * either the terms of the Eclipse Public License v1.0 as published by
007 * the Eclipse Foundation
008 *
009 *   or (per the licensee's choosing)
010 *
011 * under the terms of the GNU Lesser General Public License version 2.1
012 * as published by the Free Software Foundation.
013 */
014package ch.qos.logback.core.encoder;
015
016import java.nio.charset.Charset;
017
018import ch.qos.logback.core.Appender;
019import ch.qos.logback.core.CoreConstants;
020import ch.qos.logback.core.Layout;
021import ch.qos.logback.core.OutputStreamAppender;
022
023public class LayoutWrappingEncoder<E> extends EncoderBase<E> {
024
025    protected Layout<E> layout;
026
027    /**
028     * The charset to use when converting a String into bytes.
029     * <p>
030     * By default this property has the value {@code null} which corresponds to
031     * the system's default charset.
032     */
033    private Charset charset;
034
035    Appender<?> parent;
036    Boolean immediateFlush = null;
037
038    public Layout<E> getLayout() {
039        return layout;
040    }
041
042    public void setLayout(Layout<E> layout) {
043        this.layout = layout;
044    }
045
046    public Charset getCharset() {
047        return charset;
048    }
049
050    /**
051     * Set the charset to use when converting the string returned by the layout
052     * into bytes.
053     * <p>
054     * By default this property has the value {@code null} which corresponds to
055     * the system's default charset.
056     *
057     * @param charset
058     */
059    public void setCharset(Charset charset) {
060        this.charset = charset;
061    }
062
063    /**
064     * Sets the immediateFlush option. The default value for immediateFlush is 'true'. If set to true,
065     * the doEncode() method will immediately flush the underlying OutputStream. Although immediate flushing
066     * is safer, it also significantly degrades logging throughput.
067     *
068     * @since 1.0.3
069     */
070    public void setImmediateFlush(boolean immediateFlush) {
071        addWarn("As of version 1.2.0 \"immediateFlush\" property should be set within the enclosing Appender.");
072        addWarn("Please move \"immediateFlush\" property into the enclosing appender.");
073        this.immediateFlush = immediateFlush;
074    }
075
076    @Override
077    public byte[] headerBytes() {
078        if (layout == null)
079            return null;
080
081        StringBuilder sb = new StringBuilder();
082        appendIfNotNull(sb, layout.getFileHeader());
083        appendIfNotNull(sb, layout.getPresentationHeader());
084        if (sb.length() > 0) {
085            // If at least one of file header or presentation header were not
086            // null, then append a line separator.
087            // This should be useful in most cases and should not hurt.
088            sb.append(CoreConstants.LINE_SEPARATOR);
089        }
090        return convertToBytes(sb.toString());
091    }
092
093    @Override
094    public byte[] footerBytes() {
095        if (layout == null)
096            return null;
097
098        StringBuilder sb = new StringBuilder();
099        appendIfNotNull(sb, layout.getPresentationFooter());
100        appendIfNotNull(sb, layout.getFileFooter());
101        return convertToBytes(sb.toString());
102    }
103
104    private byte[] convertToBytes(String s) {
105        if (charset == null) {
106            return s.getBytes();
107        } else {
108            return s.getBytes(charset);
109        }
110    }
111
112    public byte[] encode(E event) {
113        String txt = layout.doLayout(event);
114        return convertToBytes(txt);
115    }
116
117    public boolean isStarted() {
118        return false;
119    }
120
121    public void start() {
122        if (immediateFlush != null) {
123            if (parent instanceof OutputStreamAppender) {
124                addWarn("Setting the \"immediateFlush\" property of the enclosing appender to " + immediateFlush);
125                @SuppressWarnings("unchecked")
126                OutputStreamAppender<E> parentOutputStreamAppender = (OutputStreamAppender<E>) parent;
127                parentOutputStreamAppender.setImmediateFlush(immediateFlush);
128            } else {
129                addError("Could not set the \"immediateFlush\" property of the enclosing appender.");
130            }
131        }
132        started = true;
133    }
134
135    public void stop() {
136        started = false;
137    }
138
139    private void appendIfNotNull(StringBuilder sb, String s) {
140        if (s != null) {
141            sb.append(s);
142        }
143    }
144
145    /**
146     * This method allows RollingPolicy implementations to be aware of their
147     * containing appender.
148     * 
149     * @param parent
150     */
151    public void setParent(Appender<?> parent) {
152        this.parent = parent;
153    }
154}