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-2016 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 /** Flushes any buffered contents of the output stream. */ 036 void flush(); 037 038 /** Releases any resources held by the writer. */ 039 void shutdown(); 040 041 /** 042 * Retrieves the number of bytes written by this writer. 043 * 044 * @return the number of bytes written by this writer. 045 */ 046 long getBytesWritten(); 047 048 /** A TextWriter implementation which writes to standard out. */ 049 public static class STDOUT implements TextWriter 050 { 051 private MeteredStream stream = new MeteredStream(System.out, 0); 052 private PrintWriter writer = new PrintWriter(stream, true); 053 054 @Override 055 public void writeRecord(String record) 056 { 057 writer.println(record); 058 } 059 060 @Override 061 public void flush() 062 { 063 writer.flush(); 064 } 065 066 @Override 067 public void shutdown() 068 { 069 // Should never close the system out stream. 070 } 071 072 @Override 073 public long getBytesWritten() 074 { 075 return stream.written; 076 } 077 } 078 079 /** A TextWriter implementation which writes to standard error. */ 080 public static class STDERR implements TextWriter 081 { 082 private MeteredStream stream = new MeteredStream(System.err, 0); 083 private PrintWriter writer = new PrintWriter(stream, true); 084 085 @Override 086 public void writeRecord(String record) 087 { 088 writer.println(record); 089 } 090 091 @Override 092 public void flush() 093 { 094 writer.flush(); 095 } 096 097 @Override 098 public void shutdown() 099 { 100 // Should never close the system error stream. 101 } 102 103 @Override 104 public long getBytesWritten() 105 { 106 return stream.written; 107 } 108 } 109 110 /** A TextWriter implementation which writes to a given output stream. */ 111 public class STREAM implements TextWriter 112 { 113 private MeteredStream stream; 114 private PrintWriter writer; 115 116 /** 117 * Creates a new text writer that will write to the provided output stream. 118 * 119 * @param outputStream The output stream to which 120 */ 121 public STREAM(OutputStream outputStream) 122 { 123 stream = new MeteredStream(outputStream, 0); 124 writer = new PrintWriter(stream, true); 125 } 126 127 @Override 128 public void writeRecord(String record) 129 { 130 writer.println(record); 131 } 132 133 @Override 134 public void flush() 135 { 136 writer.flush(); 137 } 138 139 @Override 140 public void shutdown() 141 { 142 // Should never close the system error stream. 143 } 144 145 @Override 146 public long getBytesWritten() 147 { 148 return stream.written; 149 } 150 } 151}