KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > enhydra > xml > xmlc > StreamXMLCLogger


1 /*
2  * Enhydra Java Application Server Project
3  *
4  * The contents of this file are subject to the Enhydra Public License
5  * Version 1.1 (the "License"); you may not use this file except in
6  * compliance with the License. You may obtain a copy of the License on
7  * the Enhydra web site ( http://www.enhydra.org/ ).
8  *
9  * Software distributed under the License is distributed on an "AS IS"
10  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
11  * the License for the specific terms governing rights and limitations
12  * under the License.
13  *
14  * The Initial Developer of the Enhydra Application Server is Lutris
15  * Technologies, Inc. The Enhydra Application Server and portions created
16  * by Lutris Technologies, Inc. are Copyright Lutris Technologies, Inc.
17  * All Rights Reserved.
18  *
19  * Contributor(s):
20  *
21  * $Id: StreamXMLCLogger.java,v 1.1.1.1 2003/03/10 16:36:19 taweili Exp $
22  */

23
24 package org.enhydra.xml.xmlc;
25
26 import java.io.PrintWriter JavaDoc;
27 import java.io.StringWriter JavaDoc;
28
29 /**
30  * XMLC logger interface to PrintWriter streams.
31  */

32 public class StreamXMLCLogger implements XMLCLogger {
33     /**
34      * Writer for XMLC informative logging or null if not enabled.
35      */

36     private PrintWriter JavaDoc fInfoWriter;
37
38     /**
39      * Writer for XMLC debug logging or null if not enabled.
40      */

41     private PrintWriter JavaDoc fDebugWriter;
42
43     /**
44      * Writer for XMLC error logging or null if no logging.
45      */

46     private PrintWriter JavaDoc fErrorWriter;
47
48     /**
49      * Constructor with no streams specified. No loggin is initially enabled.
50      */

51     public StreamXMLCLogger() {
52     }
53
54     /**
55      * Constructor specifing a single stream to be used for both info
56      * and error logging.
57      */

58     public StreamXMLCLogger(PrintWriter JavaDoc logWriter) {
59         fInfoWriter = logWriter;
60         fErrorWriter = logWriter;
61     }
62
63     /**
64      * Constructor specifing a streams for info, error and debug.
65      * Any maybe null to disable.
66      */

67     public StreamXMLCLogger(PrintWriter JavaDoc infoWriter,
68                             PrintWriter JavaDoc errorWriter,
69                             PrintWriter JavaDoc debugWriter) {
70         fInfoWriter = infoWriter;
71         fErrorWriter = errorWriter;
72         fDebugWriter = debugWriter;
73     }
74
75     /**
76      * Log an error message with an exception
77      */

78     private void logExcept(PrintWriter JavaDoc writer,
79                            String JavaDoc msg,
80                            Throwable JavaDoc except) {
81         if (writer != null) {
82             // Get stack trace formatted
83
StringWriter JavaDoc trace = new StringWriter JavaDoc(1024);
84             PrintWriter JavaDoc ptrace = new PrintWriter JavaDoc(trace, true);
85             except.printStackTrace(ptrace);
86             writer.println(msg + "\n" + except.toString()
87                            + "\n" + trace);
88         writer.flush();
89         }
90     }
91
92     /**
93      * Determine if info logging is enabled.
94      */

95     public boolean infoEnabled() {
96         return (fInfoWriter != null);
97     }
98
99     /**
100      * Get the info logging writer.
101      */

102     public PrintWriter JavaDoc getInfoWriter() {
103         return fInfoWriter;
104     }
105
106     /**
107      * Set the info logging writer; disable by setting to null.
108      */

109     public void setInfoWriter(PrintWriter JavaDoc writer) {
110         fInfoWriter = writer;
111     }
112
113     /**
114      * Login an info message.
115      */

116     public void logInfo(String JavaDoc msg) {
117         if (fInfoWriter != null) {
118             fInfoWriter.println(msg);
119         fInfoWriter.flush();
120         }
121     }
122
123     /**
124      * Login an info message with exception.
125      */

126     public void logInfo(String JavaDoc msg,
127                         Throwable JavaDoc except) {
128         if (fInfoWriter != null) {
129             logExcept(fInfoWriter, msg, except);
130         }
131     }
132
133     /**
134      * Determine if error logging is enabled.
135      */

136     public boolean errorEnabled() {
137         return (fErrorWriter != null);
138     }
139
140     /**
141      * Get the error logging writer.
142      */

143     public PrintWriter JavaDoc getErrorWriter() {
144         return fErrorWriter;
145     }
146
147     /**
148      * Set the error logging writer; disable by setting to null.
149      */

150     public void setErrorWriter(PrintWriter JavaDoc writer) {
151         fErrorWriter = writer;
152     }
153
154     /**
155      * Login an error message.
156      */

157     public void logError(String JavaDoc msg) {
158         if (fErrorWriter != null) {
159             fErrorWriter.println(msg);
160         fErrorWriter.flush();
161         }
162     }
163
164     /**
165      * Login an error message with exception.
166      */

167     public void logError(String JavaDoc msg,
168                          Throwable JavaDoc except) {
169         if (fErrorWriter != null) {
170             logExcept(fErrorWriter, msg, except);
171         }
172     }
173
174     /**
175      * Determine if debug logging is enabled.
176      */

177     public boolean debugEnabled() {
178         return (fDebugWriter != null);
179     }
180
181     /**
182      * Get the debug logging writer.
183      */

184     public PrintWriter JavaDoc getDebugWriter() {
185         return fDebugWriter;
186     }
187
188     /**
189      * Set the debug logging writer; disable by setting to null.
190      */

191     public void setDebugWriter(PrintWriter JavaDoc writer) {
192         fDebugWriter = writer;
193     }
194
195     /**
196      * Login an debug message.
197      */

198     public void logDebug(String JavaDoc msg) {
199         if (fDebugWriter != null) {
200             fDebugWriter.println(msg);
201         fDebugWriter.flush();
202         }
203     }
204
205     /**
206      * Login an debug message with exception.
207      */

208     public void logDebug(String JavaDoc msg,
209                          Throwable JavaDoc except) {
210         if (fDebugWriter != null) {
211             logExcept(fDebugWriter, msg, except);
212         }
213     }
214
215     /**
216      * Close all streams associated with this object.
217      */

218     public void close() {
219         if (fInfoWriter != null) {
220             fInfoWriter.close();
221         }
222         if (fErrorWriter != null) {
223             fErrorWriter.close();
224         }
225         if (fDebugWriter != null) {
226             fDebugWriter.close();
227         }
228     }
229 }
230
Popular Tags