001: /*
002: * Licensed to the Apache Software Foundation (ASF) under one or more
003: * contributor license agreements. See the NOTICE file distributed with
004: * this work for additional information regarding copyright ownership.
005: * The ASF licenses this file to You under the Apache License, Version 2.0
006: * (the "License"); you may not use this file except in compliance with
007: * the License. You may obtain a copy of the License at
008: *
009: * http://www.apache.org/licenses/LICENSE-2.0
010: *
011: * Unless required by applicable law or agreed to in writing, software
012: * distributed under the License is distributed on an "AS IS" BASIS,
013: * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
014: *
015: * See the License for the specific language governing permissions and
016: * limitations under the License.
017: */
018:
019: /**
020: * @author Vitaly A. Provodin
021: * @version $Revision: 1.6 $
022: */
023:
024: /**
025: * Created on 28.03.2005
026: */package org.apache.harmony.jpda.tests.share;
027:
028: import java.io.PrintStream;
029:
030: import org.apache.harmony.jpda.tests.framework.LogWriter;
031:
032: /**
033: * This class provides logging messages to underlying output stream. There are
034: * can be several JPDALogWriter objects writing to the same underlying stream
035: * with different prefixes.
036: */
037: public class JPDALogWriter extends LogWriter {
038:
039: protected String printPrefix;
040:
041: protected String errorMessage;
042:
043: protected LogStream logStream;
044:
045: public boolean enablePrint = true;
046:
047: /**
048: * Constructs an instance of the class for given output stream.
049: *
050: * @param outputStream
051: * stream for output
052: * @param prefix
053: * prefix for messages or null
054: * @param enablePrint
055: * flag for enabling print to log
056: */
057: public JPDALogWriter(PrintStream outputStream, String prefix,
058: boolean enablePrint) {
059: super (prefix);
060: this .enablePrint = enablePrint;
061: logStream = new LogStream(outputStream);
062: }
063:
064: /**
065: * Constructs an instance of the class for given output stream.
066: *
067: * @param outputStream
068: * stream for output
069: * @param prefix
070: * prefix for messages or null
071: */
072: public JPDALogWriter(PrintStream outputStream, String prefix) {
073: this (outputStream, prefix, true);
074: }
075:
076: /**
077: * Constructs an instance of the class to log to the same output stream.
078: *
079: * @param logWriter
080: * log writer containing stream for output
081: * @param prefix
082: * prefix for messages or null
083: */
084: public JPDALogWriter(JPDALogWriter logWriter, String prefix) {
085: super (prefix);
086: logStream = logWriter.getLogStream();
087: }
088:
089: /**
090: * Sets prefix for messages.
091: *
092: * @param prefix
093: * to be set
094: */
095: public void setPrefix(String prefix) {
096: super .setPrefix(prefix);
097: if (prefix == null || prefix.length() <= 0) {
098: printPrefix = "";
099: } else {
100: printPrefix = prefix + "> ";
101: }
102: }
103:
104: /**
105: * Prints error message.
106: *
107: * @param message
108: * error message to be printed
109: */
110: public void printError(String message) {
111: if (null == errorMessage) {
112: errorMessage = message;
113: }
114: logStream.println(getErrorPrefix() + message);
115: }
116:
117: /**
118: * Prints exception information with explaining message.
119: *
120: * @param message
121: * explaining message to be printed
122: * @param throwable
123: * exception to be printed
124: */
125: public void printError(String message, Throwable throwable) {
126: if (null == errorMessage) {
127: errorMessage = message;
128: }
129: logStream
130: .printStackTrace(getErrorPrefix() + message, throwable);
131: }
132:
133: /**
134: * Prints exception information w/o explaining message.
135: *
136: * @param throwable
137: * exception to be printed
138: */
139: public void printError(Throwable throwable) {
140: logStream.printStackTrace(null, throwable);
141: }
142:
143: /**
144: * Prints message to the output stream w/o line feed.
145: *
146: * @param message
147: * to be printed
148: */
149: public void print(String message) {
150: if (enablePrint) {
151: logStream.print(printPrefix + message);
152: }
153: }
154:
155: /**
156: * Prints message to the output stream with line feed.
157: *
158: * @param message
159: * to be printed
160: */
161: public void println(String message) {
162: if (enablePrint) {
163: logStream.println(printPrefix + message);
164: }
165: }
166:
167: /**
168: * Returns saved error message.
169: *
170: * @return message string
171: */
172: public String getErrorMessage() {
173: return errorMessage;
174: }
175:
176: // /////////////////////////////////////////////////////////////////////////////
177:
178: /**
179: * Get prefix for error messages.
180: */
181: protected String getErrorPrefix() {
182: return "# ERROR: " + printPrefix;
183: }
184:
185: /**
186: * Get underlying LogStream object.
187: */
188: protected LogStream getLogStream() {
189: return logStream;
190: }
191:
192: /**
193: * Underlying stream with synchronous access.
194: */
195: protected static class LogStream {
196: protected PrintStream outputStream;
197:
198: /**
199: * A constructor.
200: *
201: * @param outputStream
202: */
203: public LogStream(PrintStream outputStream) {
204: setOutputStream(outputStream);
205: }
206:
207: /**
208: * @return The associated output stream.
209: */
210: public synchronized PrintStream getOutputStream() {
211: return outputStream;
212: }
213:
214: /**
215: * Sets new output stream.
216: *
217: * @param outputStream
218: * new output stream
219: */
220: public synchronized void setOutputStream(
221: PrintStream outputStream) {
222: this .outputStream = outputStream;
223: }
224:
225: /**
226: * Prints the given message with the newline to the output stream.
227: *
228: * @param message
229: * log message
230: */
231: public synchronized void println(String message) {
232: outputStream.println(message);
233: }
234:
235: /**
236: * Prints the given message to the output stream.
237: *
238: * @param message
239: * log message
240: */
241: public synchronized void print(String message) {
242: outputStream.print(message);
243: outputStream.flush();
244: }
245:
246: /**
247: * Prints the given message and the call stack of the exception to the
248: * output stream.
249: *
250: * @param message
251: * log message
252: * @param throwable
253: * exception
254: */
255: public synchronized void printStackTrace(String message,
256: Throwable throwable) {
257: if (message != null) {
258: println(message);
259: }
260: throwable.printStackTrace(outputStream);
261: }
262: }
263: }
|