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