KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > protomatter > syslog > PrintWriterLog


1 package com.protomatter.syslog;
2
3 /**
4  * {{{ The Protomatter Software License, Version 1.0
5  * derived from The Apache Software License, Version 1.1
6  *
7  * Copyright (c) 1998-2002 Nate Sammons. All rights reserved.
8  *
9  * Redistribution and use in source and binary forms, with or without
10  * modification, are permitted provided that the following conditions
11  * are met:
12  *
13  * 1. Redistributions of source code must retain the above copyright
14  * notice, this list of conditions and the following disclaimer.
15  *
16  * 2. Redistributions in binary form must reproduce the above copyright
17  * notice, this list of conditions and the following disclaimer in
18  * the documentation and/or other materials provided with the
19  * distribution.
20  *
21  * 3. The end-user documentation included with the redistribution,
22  * if any, must include the following acknowledgment:
23  * "This product includes software developed for the
24  * Protomatter Software Project
25  * (http://protomatter.sourceforge.net/)."
26  * Alternately, this acknowledgment may appear in the software itself,
27  * if and wherever such third-party acknowledgments normally appear.
28  *
29  * 4. The names "Protomatter" and "Protomatter Software Project" must
30  * not be used to endorse or promote products derived from this
31  * software without prior written permission. For written
32  * permission, please contact support@protomatter.com.
33  *
34  * 5. Products derived from this software may not be called "Protomatter",
35  * nor may "Protomatter" appear in their name, without prior written
36  * permission of the Protomatter Software Project
37  * (support@protomatter.com).
38  *
39  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED
40  * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
41  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
42  * DISCLAIMED. IN NO EVENT SHALL THE PROTOMATTER SOFTWARE PROJECT OR
43  * ITS CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
44  * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
45  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF
46  * USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
47  * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
48  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
49  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
50  * SUCH DAMAGE. }}}
51  */

52
53 import java.io.PrintWriter JavaDoc;
54 import java.io.IOException JavaDoc;
55 import java.net.*;
56 import java.util.*;
57 import java.text.*;
58
59 import com.protomatter.util.*;
60
61 /**
62  * An implementation of an object that will log things
63  * using the Syslog facility.
64  *
65  * @see com.protomatter.syslog.xml.PrintWriterLog_Helper XML configuration class
66  */

67 public class PrintWriterLog
68 extends BasicLogger
69 {
70    private PrintWriter JavaDoc out = null;
71    private boolean writerChanged = true;
72    private String JavaDoc streamName = null;
73    private boolean canDumpConfig = false;
74
75    /**
76     * Construct a new PrintWriterLog attached to the given
77     * PrintWriter. By default, it will respond to log entries
78     * coming along on all channels.
79     */

80    public PrintWriterLog(PrintWriter JavaDoc writer)
81    {
82       this();
83       out = writer;
84    }
85
86    /**
87     * Construct a new PrintWriterLog attached to the given
88     * stream. The stream name must be either
89     * "<tt>System.out</tt>" or "<tt>System.err</tt>".
90     */

91    public PrintWriterLog(String JavaDoc streamName)
92    {
93       this();
94       if ("System.err".equals(streamName))
95       {
96         out = new PrintWriter JavaDoc(System.err);
97       }
98       else if ("System.out".equals(streamName))
99       {
100         out = new PrintWriter JavaDoc(System.out);
101       }
102       else
103       {
104          throw new IllegalArgumentException JavaDoc(MessageFormat.format(
105            Syslog.getResourceString(MessageConstants.PRINTWRITER_BAD_STREAM_NAME_MESSAGE),
106            new Object JavaDoc[] { "stream", "System.out", "System.err" }));
107       }
108       this.streamName = streamName;
109       this.canDumpConfig = true;
110    }
111
112    /**
113     * Get the name of the stream.
114     */

115    public String JavaDoc getStreamName()
116    {
117      return this.streamName;
118    }
119
120    /**
121     * Set the name of the stream.
122     */

123    public void setStreamName(String JavaDoc streamName)
124    {
125      this.streamName = streamName;
126    }
127
128    /**
129     * Construct a new PrintWriterLog -- you must call
130     * configure() after using this constructor.
131     */

132    public PrintWriterLog()
133    {
134       super();
135    }
136
137    /**
138     * Set the writer that we're writing to.
139     */

140    public void setWriter(PrintWriter JavaDoc writer)
141    {
142       cleanup();
143       out = writer;
144       writerChanged = true;
145       this.canDumpConfig = false;
146    }
147
148    /**
149     * Write a log message.
150     */

151    public void log(SyslogMessage message)
152    {
153       StringBuffer JavaDoc b = new StringBuffer JavaDoc();
154       formatLogEntry(b, message);
155       if (out != null)
156       {
157          if (writerChanged)
158          {
159            out.print(formatter.getLogHeader());
160            writerChanged = false;
161          }
162          out.print(b);
163          out.flush();
164       }
165    }
166
167    private void cleanup()
168    {
169      if (out != null)
170      {
171        out.print(formatter.getLogFooter());
172        out.flush();
173        out.close();
174        out = null;
175      }
176    }
177
178    /**
179     * Clean up and prepare for shutdown.
180     */

181    public synchronized void shutdown()
182    {
183      cleanup();
184    }
185
186    public void flush()
187    {
188      if (out != null)
189        out.flush();
190    }
191 }
192
Popular Tags