1 /* 2 * Licensed to the Apache Software Foundation (ASF) under one or more 3 * contributor license agreements. See the NOTICE file distributed with 4 * this work for additional information regarding copyright ownership. 5 * The ASF licenses this file to You under the Apache License, Version 2.0 6 * (the "License"); you may not use this file except in compliance with 7 * the License. You may obtain a copy of the License at 8 * 9 * http://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * 15 * See the License for the specific language governing permissions and 16 * limitations under the License. 17 */ 18 19 /** 20 * @author Vitaly A. Provodin 21 */ 22 23 /** 24 * Created on 28.03.2005 25 */ 26 package org.apache.harmony.jpda.tests.share; 27 28 import java.io.PrintStream; 29 30 import org.apache.harmony.jpda.tests.framework.LogWriter; 31 32 /** 33 * This class provides logging messages to underlying output stream. There are 34 * can be several JPDALogWriter objects writing to the same underlying stream 35 * with different prefixes. 36 */ 37 public class JPDALogWriter extends LogWriter { 38 39 protected String printPrefix; 40 41 protected String errorMessage; 42 43 protected LogStream logStream; 44 45 public boolean enablePrint = true; 46 47 /** 48 * Constructs an instance of the class for given output stream. 49 * 50 * @param outputStream 51 * stream for output 52 * @param prefix 53 * prefix for messages or null 54 * @param enablePrint 55 * flag for enabling print to log 56 */ 57 public JPDALogWriter(PrintStream outputStream, String prefix, 58 boolean enablePrint) { 59 super(prefix); 60 this.enablePrint = enablePrint; 61 logStream = new LogStream(outputStream); 62 } 63 64 /** 65 * Constructs an instance of the class for given output stream. 66 * 67 * @param outputStream 68 * stream for output 69 * @param prefix 70 * prefix for messages or null 71 */ 72 public JPDALogWriter(PrintStream outputStream, String prefix) { 73 this(outputStream, prefix, true); 74 } 75 76 /** 77 * Constructs an instance of the class to log to the same output stream. 78 * 79 * @param logWriter 80 * log writer containing stream for output 81 * @param prefix 82 * prefix for messages or null 83 */ 84 public JPDALogWriter(JPDALogWriter logWriter, String prefix) { 85 super(prefix); 86 logStream = logWriter.getLogStream(); 87 } 88 89 /** 90 * Sets prefix for messages. 91 * 92 * @param prefix 93 * to be set 94 */ 95 @Override 96 public void setPrefix(String prefix) { 97 super.setPrefix(prefix); 98 if (prefix == null || prefix.length() <= 0) { 99 printPrefix = ""; 100 } else { 101 printPrefix = prefix + "> "; 102 } 103 } 104 105 /** 106 * Prints error message. 107 * 108 * @param message 109 * error message to be printed 110 */ 111 @Override 112 public void printError(String message) { 113 if (null == errorMessage) { 114 errorMessage = message; 115 } 116 logStream.println(getErrorPrefix() + message); 117 } 118 119 /** 120 * Prints exception information with explaining message. 121 * 122 * @param message 123 * explaining message to be printed 124 * @param throwable 125 * exception to be printed 126 */ 127 @Override 128 public void printError(String message, Throwable throwable) { 129 if (null == errorMessage) { 130 errorMessage = message; 131 } 132 logStream.printStackTrace(getErrorPrefix() + message, throwable); 133 } 134 135 /** 136 * Prints exception information w/o explaining message. 137 * 138 * @param throwable 139 * exception to be printed 140 */ 141 @Override 142 public void printError(Throwable throwable) { 143 logStream.printStackTrace(null, throwable); 144 } 145 146 /** 147 * Prints message to the output stream w/o line feed. 148 * 149 * @param message 150 * to be printed 151 */ 152 @Override 153 public void print(String message) { 154 if (enablePrint) { 155 logStream.print(printPrefix + message); 156 } 157 } 158 159 /** 160 * Prints message to the output stream with line feed. 161 * 162 * @param message 163 * to be printed 164 */ 165 @Override 166 public void println(String message) { 167 if (enablePrint) { 168 logStream.println(printPrefix + message); 169 } 170 } 171 172 /** 173 * Returns saved error message. 174 * 175 * @return message string 176 */ 177 public String getErrorMessage() { 178 return errorMessage; 179 } 180 181 // ///////////////////////////////////////////////////////////////////////////// 182 183 /** 184 * Get prefix for error messages. 185 */ 186 protected String getErrorPrefix() { 187 return "# ERROR: " + printPrefix; 188 } 189 190 /** 191 * Get underlying LogStream object. 192 */ 193 protected LogStream getLogStream() { 194 return logStream; 195 } 196 197 /** 198 * Underlying stream with synchronous access. 199 */ 200 protected static class LogStream { 201 protected PrintStream outputStream; 202 203 /** 204 * A constructor. 205 * 206 * @param outputStream 207 */ 208 public LogStream(PrintStream outputStream) { 209 setOutputStream(outputStream); 210 } 211 212 /** 213 * @return The associated output stream. 214 */ 215 public synchronized PrintStream getOutputStream() { 216 return outputStream; 217 } 218 219 /** 220 * Sets new output stream. 221 * 222 * @param outputStream 223 * new output stream 224 */ 225 public synchronized void setOutputStream(PrintStream outputStream) { 226 this.outputStream = outputStream; 227 } 228 229 /** 230 * Prints the given message with the newline to the output stream. 231 * 232 * @param message 233 * log message 234 */ 235 public synchronized void println(String message) { 236 outputStream.println(message); 237 } 238 239 /** 240 * Prints the given message to the output stream. 241 * 242 * @param message 243 * log message 244 */ 245 public synchronized void print(String message) { 246 outputStream.print(message); 247 outputStream.flush(); 248 } 249 250 /** 251 * Prints the given message and the call stack of the exception to the 252 * output stream. 253 * 254 * @param message 255 * log message 256 * @param throwable 257 * exception 258 */ 259 public synchronized void printStackTrace(String message, 260 Throwable throwable) { 261 if (message != null) { 262 println(message); 263 } 264 throwable.printStackTrace(outputStream); 265 } 266 } 267 } 268