Home | History | Annotate | Download | only in share
      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     public void setPrefix(String prefix) {
     96         super.setPrefix(prefix);
     97         if (prefix == null || prefix.length() <= 0) {
     98             printPrefix = "";
     99         } 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.printStackTrace(getErrorPrefix() + message, throwable);
    130     }
    131 
    132     /**
    133      * Prints exception information w/o explaining message.
    134      *
    135      * @param throwable
    136      *            exception to be printed
    137      */
    138     public void printError(Throwable throwable) {
    139         logStream.printStackTrace(null, throwable);
    140     }
    141 
    142     /**
    143      * Prints message to the output stream w/o line feed.
    144      *
    145      * @param message
    146      *            to be printed
    147      */
    148     public void print(String message) {
    149         if (enablePrint) {
    150             logStream.print(printPrefix + message);
    151         }
    152     }
    153 
    154     /**
    155      * Prints message to the output stream with line feed.
    156      *
    157      * @param message
    158      *            to be printed
    159      */
    160     public void println(String message) {
    161         if (enablePrint) {
    162             logStream.println(printPrefix + message);
    163         }
    164     }
    165 
    166     /**
    167      * Returns saved error message.
    168      *
    169      * @return message string
    170      */
    171     public String getErrorMessage() {
    172         return errorMessage;
    173     }
    174 
    175     // /////////////////////////////////////////////////////////////////////////////
    176 
    177     /**
    178      * Get prefix for error messages.
    179      */
    180     protected String getErrorPrefix() {
    181         return "# ERROR: " + printPrefix;
    182     }
    183 
    184     /**
    185      * Get underlying LogStream object.
    186      */
    187     protected LogStream getLogStream() {
    188         return logStream;
    189     }
    190 
    191     /**
    192      * Underlying stream with synchronous access.
    193      */
    194     protected static class LogStream {
    195         protected PrintStream outputStream;
    196 
    197         /**
    198          * A constructor.
    199          *
    200          * @param outputStream
    201          */
    202         public LogStream(PrintStream outputStream) {
    203             setOutputStream(outputStream);
    204         }
    205 
    206         /**
    207          * @return The associated output stream.
    208          */
    209         public synchronized PrintStream getOutputStream() {
    210             return outputStream;
    211         }
    212 
    213         /**
    214          * Sets new output stream.
    215          *
    216          * @param outputStream
    217          *            new output stream
    218          */
    219         public synchronized void setOutputStream(PrintStream outputStream) {
    220             this.outputStream = outputStream;
    221         }
    222 
    223         /**
    224          * Prints the given message with the newline to the output stream.
    225          *
    226          * @param message
    227          *            log message
    228          */
    229         public synchronized void println(String message) {
    230             outputStream.println(message);
    231         }
    232 
    233         /**
    234          * Prints the given message to the output stream.
    235          *
    236          * @param message
    237          *            log message
    238          */
    239         public synchronized void print(String message) {
    240             outputStream.print(message);
    241             outputStream.flush();
    242         }
    243 
    244         /**
    245          * Prints the given message and the call stack of the exception to the
    246          * output stream.
    247          *
    248          * @param message
    249          *            log message
    250          * @param throwable
    251          *            exception
    252          */
    253         public synchronized void printStackTrace(String message,
    254                 Throwable throwable) {
    255             if (message != null) {
    256                 println(message);
    257             }
    258             throwable.printStackTrace(outputStream);
    259         }
    260     }
    261 }
    262