001/*
002 * CDDL HEADER START
003 *
004 * The contents of this file are subject to the terms of the
005 * Common Development and Distribution License, Version 1.0 only
006 * (the "License").  You may not use this file except in compliance
007 * with the License.
008 *
009 * You can obtain a copy of the license at legal-notices/CDDLv1_0.txt
010 * or http://forgerock.org/license/CDDLv1.0.html.
011 * See the License for the specific language governing permissions
012 * and limitations under the License.
013 *
014 * When distributing Covered Code, include this CDDL HEADER in each
015 * file and include the License file at legal-notices/CDDLv1_0.txt.
016 * If applicable, add the following below this CDDL HEADER, with the
017 * fields enclosed by brackets "[]" replaced with your own identifying
018 * information:
019 *      Portions Copyright [yyyy] [name of copyright owner]
020 *
021 * CDDL HEADER END
022 *
023 *
024 *      Copyright 2006-2008 Sun Microsystems, Inc.
025 *      Portions Copyright 2015 ForgeRock AS
026 */
027package org.opends.server.loggers;
028
029import java.io.OutputStream;
030import java.io.PrintWriter;
031
032/**
033 * A TextWriter provides a character-based stream used by a
034 * Text Publishers as a target for outputting log records.
035 */
036public interface TextWriter
037{
038  /**
039   * Writes a text record to the output stream.
040   *
041   * @param record - the record to write.
042   */
043  void writeRecord(String record);
044
045  /**
046   * Flushes any buffered contents of the output stream.
047   */
048  void flush();
049
050  /**
051   * Releases any resources held by the writer.
052   */
053  void shutdown();
054
055  /**
056   * Retrieves the number of bytes written by this writer.
057   *
058   * @return the number of bytes written by this writer.
059   */
060  long getBytesWritten();
061
062  /**
063   * A TextWriter implementation which writes to standard out.
064   */
065  public static class STDOUT implements TextWriter
066  {
067    private MeteredStream stream = new MeteredStream(System.out, 0);
068    private PrintWriter writer = new PrintWriter(stream, true);
069
070    /** {@inheritDoc} */
071    public void writeRecord(String record)
072    {
073      writer.println(record);
074    }
075
076    /** {@inheritDoc} */
077    public void flush()
078    {
079      writer.flush();
080    }
081
082    /** {@inheritDoc} */
083    public void shutdown()
084    {
085      // Should never close the system out stream.
086    }
087
088    /** {@inheritDoc} */
089    public long getBytesWritten()
090    {
091      return stream.written;
092    }
093  }
094
095  /**
096   * A TextWriter implementation which writes to standard error.
097   */
098  public static class STDERR implements TextWriter
099  {
100    private MeteredStream stream = new MeteredStream(System.err, 0);
101    private PrintWriter writer = new PrintWriter(stream, true);
102
103    /** {@inheritDoc} */
104    public void writeRecord(String record)
105    {
106      writer.println(record);
107    }
108
109    /** {@inheritDoc} */
110    public void flush()
111    {
112      writer.flush();
113    }
114
115    /** {@inheritDoc} */
116    public void shutdown()
117    {
118      // Should never close the system error stream.
119    }
120
121    /** {@inheritDoc} */
122    public long getBytesWritten()
123    {
124      return stream.written;
125    }
126  }
127
128  /**
129   * A TextWriter implementation which writes to a given output stream.
130   */
131  public class STREAM implements TextWriter
132  {
133    private MeteredStream stream;
134    private PrintWriter writer;
135
136    /**
137     * Creates a new text writer that will write to the provided output stream.
138     *
139     * @param  outputStream  The output stream to which
140     */
141    public STREAM(OutputStream outputStream)
142    {
143      stream = new MeteredStream(outputStream, 0);
144      writer = new PrintWriter(stream, true);
145    }
146
147    /** {@inheritDoc} */
148    public void writeRecord(String record)
149    {
150      writer.println(record);
151    }
152
153    /** {@inheritDoc} */
154    public void flush()
155    {
156      writer.flush();
157    }
158
159    /** {@inheritDoc} */
160    public void shutdown()
161    {
162      // Should never close the system error stream.
163    }
164
165    /** {@inheritDoc} */
166    public long getBytesWritten()
167    {
168      return stream.written;
169    }
170  }
171}