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 010 * trunk/opends/resource/legal-notices/OpenDS.LICENSE 011 * or https://OpenDS.dev.java.net/OpenDS.LICENSE. 012 * See the License for the specific language governing permissions 013 * and limitations under the License. 014 * 015 * When distributing Covered Code, include this CDDL HEADER in each 016 * file and include the License file at 017 * trunk/opends/resource/legal-notices/OpenDS.LICENSE. If applicable, 018 * add the following below this CDDL HEADER, with the fields enclosed 019 * by brackets "[]" replaced with your own identifying information: 020 * Portions Copyright [yyyy] [name of copyright owner] 021 * 022 * CDDL HEADER END 023 * 024 * 025 * Copyright 2006-2008 Sun Microsystems, Inc. 026 */ 027 package org.opends.server.loggers; 028 029 import java.io.OutputStream; 030 import 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 */ 036 public interface TextWriter 037 { 038 /** 039 * Writes a text record to the output stream. 040 * 041 * @param record - the record to write. 042 */ 043 public void writeRecord(String record); 044 045 /** 046 * Flushes any buffered contents of the output stream. 047 */ 048 public void flush(); 049 050 /** 051 * Releases any resources held by the writer. 052 */ 053 public 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 public long getBytesWritten(); 061 062 /** 063 * A TextWriter implementationwhich 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 /** 071 * {@inheritDoc} 072 */ 073 public void writeRecord(String record) 074 { 075 writer.println(record); 076 } 077 078 /** 079 * {@inheritDoc} 080 */ 081 public void flush() 082 { 083 writer.flush(); 084 } 085 086 /** 087 * {@inheritDoc} 088 */ 089 public void shutdown() 090 { 091 // Should never close the system out stream. 092 } 093 094 /** 095 * {@inheritDoc} 096 */ 097 public long getBytesWritten() 098 { 099 return stream.written; 100 } 101 } 102 103 /** 104 * A TextWriter implementation which writes to standard error. 105 */ 106 public static class STDERR implements TextWriter 107 { 108 private MeteredStream stream = new MeteredStream(System.err, 0); 109 private PrintWriter writer = new PrintWriter(stream, true); 110 111 /** 112 * {@inheritDoc} 113 */ 114 public void writeRecord(String record) 115 { 116 writer.println(record); 117 } 118 119 /** 120 * {@inheritDoc} 121 */ 122 public void flush() 123 { 124 writer.flush(); 125 } 126 127 /** 128 * {@inheritDoc} 129 */ 130 public void shutdown() 131 { 132 // Should never close the system error stream. 133 } 134 135 /** 136 * {@inheritDoc} 137 */ 138 public long getBytesWritten() 139 { 140 return stream.written; 141 } 142 } 143 144 /** 145 * A TextWriter implementation which writes to a given output stream. 146 */ 147 public class STREAM implements TextWriter 148 { 149 private MeteredStream stream; 150 private PrintWriter writer; 151 152 /** 153 * Creates a new text writer that will write to the provided output stream. 154 * 155 * @param outputStream The output stream to which 156 */ 157 public STREAM(OutputStream outputStream) 158 { 159 stream = new MeteredStream(outputStream, 0); 160 writer = new PrintWriter(stream, true); 161 } 162 163 /** 164 * {@inheritDoc} 165 */ 166 public void writeRecord(String record) 167 { 168 writer.println(record); 169 } 170 171 /** 172 * {@inheritDoc} 173 */ 174 public void flush() 175 { 176 writer.flush(); 177 } 178 179 /** 180 * {@inheritDoc} 181 */ 182 public void shutdown() 183 { 184 // Should never close the system error stream. 185 } 186 187 /** 188 * {@inheritDoc} 189 */ 190 public long getBytesWritten() 191 { 192 return stream.written; 193 } 194 } 195 }