View Javadoc

1   /* (c) Copyright 2003 Caleigo AB, All rights reserved. 
2    * 
3    * This library is free software; you can redistribute it and/or
4    * modify it under the terms of the GNU Lesser General Public
5    * License as published by the Free Software Foundation; either
6    * version 2.1 of the License, or (at your option) any later version.
7    * 
8    * This library is distributed in the hope that it will be useful,
9    * but WITHOUT ANY WARRANTY; without even the implied warranty of
10   * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
11   * Lesser General Public License for more details.
12   * 
13   * You should have received a copy of the GNU Lesser General Public
14   * License along with this library; if not, write to the Free Software
15   * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16   *  
17   */
18  
19  package org.caleigo.toolkit.log;
20  
21  import java.io.*;
22  
23  /*** The StreamLogSubscriber provides logging to OutputStreams and Writers.
24   * Subsequently it can used for loggings to both the console and files.
25   *
26   * @author  Dennis Zikovic
27   * @version 1.00
28   * 
29   *//* 
30   *
31   * WHEN        WHO               WHY & WHAT
32   * ------------------------------------------------------------------------------
33   * den 1 augusti 2001  Dennis Zikovic    Creation
34   */
35  public class StreamLogReceiver extends AbstractLogReceiver 
36  {
37      // Data members ------------------------------------------------------------
38      private PrintWriter mOutput;
39      
40      // Constructors ------------------------------------------------------------
41      
42      /*** Default constructor for DefaultLogSubscriber.
43       */
44      public StreamLogReceiver() 
45      {
46          this(System.out);
47      }
48      
49      public StreamLogReceiver(OutputStream output) 
50      {
51          this(new OutputStreamWriter(output));
52      }
53      
54      public StreamLogReceiver(Writer output) 
55      {
56          if(output instanceof PrintWriter)
57              mOutput = (PrintWriter)output;
58          else
59              mOutput = new PrintWriter(output);
60      }
61      
62      public StreamLogReceiver(String fileName) throws IOException
63      {
64          this(new FileWriter(fileName));
65      }
66      
67      // Superclass override -----------------------------------------------------
68      protected void logString(String msg)
69      {
70          mOutput.println(msg);
71          mOutput.flush();
72      }
73      
74      public void close()
75      {
76          if(mOutput!=null)
77              mOutput.close();
78      }
79  }