KickJava   Java API By Example, From Geeks To Geeks.

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


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.*;
54 import java.util.*;
55 import java.text.*;
56
57 /**
58  * A Writer that is attached to Syslog. When data is
59  * flushed, any information built up is sent off to Syslog.
60  * Generally, this object will be wrapped with a <tt>PrintWriter</tt>
61  * object for easier use, like this:<P>
62  *
63  * <BLOCKQUOTE><TT>
64  * SyslogWriter sw = new SyslogWriter(someOtherObject, Syslog.INFO);<BR>
65  * PrintWriter pw = new PrintWriter(sw, true);<BR>
66  * ...<BR>
67  * pw.println("Blah blah blah.. this will go to Syslog");
68  * </TT></BLOCKQUOTE>
69  *
70  * The <tt>pw.println(...)</tt> call is equivalent to calling:<P>
71  *
72  * <BLOCKQUOTE><TT>
73  * Syslog.info(someOtherObject, "Blah blah blah.. this will go to Syslog");
74  * </TT></BLOCKQUOTE>
75  *
76  * @see Syslog
77  */

78 public class SyslogWriter
79 extends Writer
80 {
81   private Object JavaDoc logger = null;
82   private Object JavaDoc channel = null;
83   private int level = Syslog.INFO;
84
85   private int maxLength = 60;
86
87   private boolean closed = false;
88
89   private StringBuffer JavaDoc data = null;
90   private String JavaDoc lineSep = null;
91   private int lineSepLength = 0;
92
93   private final void checkClosed()
94   {
95     if (closed)
96       throw new IllegalStateException JavaDoc(MessageFormat.format(
97         Syslog.getResourceString(MessageConstants.SYSLOGWRITER_IS_CLOSED_MESSAGE),
98         new Object JavaDoc[] { "SyslogWriter" }));
99   }
100
101   /**
102    * Create a new SyslogWriter. Messages will be written to
103    * Syslog as if they came from the given logger, and will be
104    * logged at the given level on the default channel.
105    *
106    * @param logger The object the log messages should appear to come from.
107    * @param level The message severity.
108    */

109   public SyslogWriter(Object JavaDoc logger, int level)
110   {
111     this(logger, null, level);
112   }
113
114   /**
115    * Create a new SyslogWriter. Messages will be written to
116    * Syslog as if they came from the given logger, and will be
117    * logged at the given level on given channel (or set of
118    * channels).
119    *
120    * @param logger The object the log messages should appear to come from.
121    * @param channel The channel to write messages to (a <tt>String</tt> or <tt>String[]</tt>).
122    * @param level The message severity.
123    */

124   public SyslogWriter(Object JavaDoc logger, Object JavaDoc channel, int level)
125   {
126     this.logger = logger;
127     this.channel = channel;
128     this.level = level;
129     this.lineSep = System.getProperty("line.separator");
130     this.lineSepLength = this.lineSep.length();
131   }
132
133   /**
134    * Close the writer.
135    */

136   public void close()
137   {
138     this.closed = true;
139   }
140
141   /**
142    * Flush unwritten data to Syslog. If the data is longer
143    * than 60 characters, then the first 60 characters (up
144    * to the first line separator, with "..." appended) are
145    * written as the "message" in Syslog, and the entire piece
146    * of data is written as the "detail".
147    */

148   public void flush()
149   {
150     checkClosed();
151     synchronized (lock)
152     {
153       if (data != null)
154       {
155         if (data.length() > maxLength)
156         {
157           StringBuffer JavaDoc msg = new StringBuffer JavaDoc(maxLength + 3);
158           String JavaDoc s = data.toString();
159
160           // this is done so that if the data is long enough
161
// to need to be broken up, but there's a line ending
162
// before maxLength, the output looks "correct"
163
int index = s.indexOf(lineSep);
164           if ((index != -1) && (index < maxLength))
165             msg.append(s.substring(0, index));
166           else
167             msg.append(s.substring(0, maxLength));
168           msg.append("...");
169
170           if (channel == null)
171             Syslog.log(logger, msg, s, level);
172           else
173             Syslog.log(logger, channel, msg, s, level);
174         }
175         else
176         {
177           String JavaDoc msg = data.toString();
178           if (msg.endsWith(lineSep))
179             msg = msg.substring(0, msg.length() - lineSepLength);
180           else if (channel == null)
181             Syslog.log(logger, msg, null, level);
182           else
183             Syslog.log(logger, channel, msg, null, level);
184         }
185         this.data = null;
186       }
187     }
188   }
189
190   /**
191    * Write the given data to the writer.
192    */

193   public void write(char buf[], int offset, int length)
194   {
195     checkClosed();
196     synchronized (lock)
197     {
198       if (data == null)
199         data = new StringBuffer JavaDoc();
200       data.append(buf, offset, length);
201     }
202   }
203 }
204
Popular Tags