home *** CD-ROM | disk | FTP | other *** search
/ Chip 1998 November / Chip_1998-11_cd.bin / tema / Cafe / main.bin / LogStream.java < prev    next >
Text File  |  1997-05-20  |  6KB  |  214 lines

  1. /*
  2.  * @(#)LogStream.java    1.7 96/12/17
  3.  * 
  4.  * Copyright (c) 1995, 1996 Sun Microsystems, Inc. All Rights Reserved.
  5.  * 
  6.  * This software is the confidential and proprietary information of Sun
  7.  * Microsystems, Inc. ("Confidential Information").  You shall not
  8.  * disclose such Confidential Information and shall use it only in
  9.  * accordance with the terms of the license agreement you entered into
  10.  * with Sun.
  11.  * 
  12.  * SUN MAKES NO REPRESENTATIONS OR WARRANTIES ABOUT THE SUITABILITY OF THE
  13.  * SOFTWARE, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE
  14.  * IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
  15.  * PURPOSE, OR NON-INFRINGEMENT. SUN SHALL NOT BE LIABLE FOR ANY DAMAGES
  16.  * SUFFERED BY LICENSEE AS A RESULT OF USING, MODIFYING OR DISTRIBUTING
  17.  * THIS SOFTWARE OR ITS DERIVATIVES.
  18.  * 
  19.  * CopyrightVersion 1.1_beta
  20.  */
  21. package java.rmi.server;
  22.  
  23. import java.io.*;
  24. import java.util.*;
  25.  
  26. /**
  27.  * <code>LogStream</code> presents a mechanism for logging errors that are
  28.  * of possible interest to those monitoring the system.
  29.  *
  30.  * @author Ann Wollrath (lots of code stolen from Ken Arnold)
  31.  */
  32. public class LogStream extends PrintStream {
  33.  
  34.     /** table mapping known log names to log stream objects */
  35.     private static Hashtable    known = new Hashtable(5);
  36.     /** default output stream for new logs */
  37.     private static PrintStream    defaultStream = System.err;
  38.  
  39.     /** log name for this log */
  40.     private String name;
  41.  
  42.     /** stream where output of this log is sent to */
  43.     private OutputStream logOut;
  44.  
  45.     /** string writer for writing message prefixes to log stream */
  46.     private OutputStreamWriter logWriter;
  47.  
  48.     /** string buffer used for constructing log message prefixes */
  49.     private StringBuffer buffer = new StringBuffer();
  50.  
  51.     /** stream used for buffering lines */
  52.     private ByteArrayOutputStream bufOut;
  53.  
  54.     /**
  55.      * Create a new LogStream object.  Since this only constructor is
  56.      * private, users must have a LogStream created through the "log"
  57.      * method.
  58.      * @param name string identifying messages from this log
  59.      * @out output stream that log messages will be sent to
  60.      */
  61.     private LogStream(String name, OutputStream out)
  62.     {
  63.     super(new ByteArrayOutputStream());
  64.     bufOut = (ByteArrayOutputStream) super.out;
  65.  
  66.     this.name = name;
  67.     setOutputStream(out);
  68.     }
  69.  
  70.     /**
  71.      * Return the LogStream identified by the given name.  If
  72.      * a log corresponding to "name" does not exist, a log using
  73.      * the default stream is created.
  74.      */
  75.     public static LogStream log(String name) {
  76.     LogStream stream;
  77.     synchronized (known) {
  78.         stream = (LogStream)known.get(name);
  79.         if (stream == null) {
  80.         stream = new LogStream(name, defaultStream);
  81.         }
  82.         known.put(name, stream);
  83.     }
  84.     return stream;
  85.     }
  86.  
  87.     /**
  88.      * Return the current default stream for new logs.
  89.      */
  90.     public static synchronized PrintStream getDefaultStream() {
  91.     return defaultStream;
  92.     }
  93.  
  94.     /**
  95.      * Set the default stream for new logs.
  96.      */
  97.     public static synchronized void setDefaultStream(PrintStream newDefault) {
  98.     defaultStream = newDefault;
  99.     }
  100.  
  101.     /**
  102.      * Return the current stream to which output from this log is sent.
  103.      */
  104.     public synchronized OutputStream getOutputStream()
  105.     {
  106.     return logOut;
  107.     }
  108.     
  109.     /**
  110.      * Set the stream to which output from this log is sent.
  111.      */
  112.     public synchronized void setOutputStream(OutputStream out)
  113.     {
  114.     logOut = out;
  115.     // Maintain an OutputStreamWriter with default CharToByteConvertor
  116.     // (just like new PrintStream) for writing log message prefixes.
  117.     logWriter = new OutputStreamWriter(logOut);
  118.     }
  119.     
  120.     /**
  121.      * Write a byte of data to the stream.  If it is not a newline, then
  122.      * the byte is appended to the internal buffer.  If it is a newline,
  123.      * then the currently buffered line is sent to the log's output
  124.      * stream, prefixed with the appropriate logging information.
  125.      */
  126.     public void write(int b)
  127.     {
  128.     if (b == '\n') {
  129.         // synchronize on "this" first to avoid potential deadlock
  130.         synchronized (this) {
  131.         synchronized (logOut) {
  132.             // construct prefix for log messages:
  133.             buffer.setLength(0);;
  134.             buffer.append(        // date/time stamp...
  135.             (new Date()).toString());
  136.             buffer.append(':');
  137.             buffer.append(name);    // ...log name...
  138.             buffer.append(':');
  139.             buffer.append(Thread.currentThread().getName());
  140.             buffer.append(':');    // ...and thread name
  141.  
  142.             try {
  143.             // write prefix through to underlying byte stream
  144.             logWriter.write(buffer.toString());
  145.             logWriter.flush();
  146.  
  147.             // finally, write the already converted bytes of
  148.             // the log message
  149.             bufOut.writeTo(logOut);
  150.             logOut.write(b);
  151.             logOut.flush();
  152.             } catch (IOException e) {
  153.             setError();
  154.             } finally {
  155.             bufOut.reset();
  156.             }
  157.         }
  158.         }
  159.     }
  160.     else
  161.         super.write(b);
  162.     }
  163.  
  164.     /**
  165.      * Write a subarray of bytes.  Pass each through write byte method.
  166.      */
  167.     public void write(byte b[], int off, int len)
  168.     {
  169.     if (len < 0)
  170.         throw new ArrayIndexOutOfBoundsException(len);
  171.     for (int i = 0; i < len; ++ i)
  172.         write(b[off + i]);
  173.     }
  174.  
  175.     /**
  176.      * Return log name as string representation
  177.      */
  178.     public String toString()
  179.     {
  180.     return name;
  181.     }
  182.  
  183.     /** constants for logging levels */
  184.     public static final int SILENT  = 0;
  185.     public static final int BRIEF   = 10;
  186.     public static final int VERBOSE = 20;
  187.  
  188.     /**
  189.      * Convert a string name of a logging level to its internal
  190.      * integer representation.
  191.      */
  192.     public static int parseLevel(String s)
  193.     {
  194.     if ((s == null) || (s.length() < 1))
  195.         return -1;
  196.  
  197.     try {
  198.         return Integer.parseInt(s);
  199.     } catch (NumberFormatException e) {
  200.     }
  201.     if (s.length() < 1)
  202.         return -1;
  203.  
  204.     if ("SILENT".startsWith(s.toUpperCase()))
  205.         return SILENT;
  206.     else if ("BRIEF".startsWith(s.toUpperCase()))
  207.         return BRIEF;
  208.     else if ("VERBOSE".startsWith(s.toUpperCase()))
  209.         return VERBOSE;
  210.  
  211.     return -1;
  212.     }
  213. }
  214.