Mercurial > hg > blitz_condensed
comparison src/com/go/trove/log/LogEventParsingWriter.java @ 0:3dc0c5604566
Initial checkin of blitz 2.0 fcs - no installer yet.
author | Dan Creswell <dan.creswell@gmail.com> |
---|---|
date | Sat, 21 Mar 2009 11:00:06 +0000 |
parents | |
children |
comparison
equal
deleted
inserted
replaced
-1:000000000000 | 0:3dc0c5604566 |
---|---|
1 /* ==================================================================== | |
2 * Trove - Copyright (c) 1997-2000 Walt Disney Internet Group | |
3 * ==================================================================== | |
4 * The Tea Software License, Version 1.1 | |
5 * | |
6 * Copyright (c) 2000 Walt Disney Internet Group. All rights reserved. | |
7 * | |
8 * Redistribution and use in source and binary forms, with or without | |
9 * modification, are permitted provided that the following conditions | |
10 * are met: | |
11 * | |
12 * 1. Redistributions of source code must retain the above copyright | |
13 * notice, this list of conditions and the following disclaimer. | |
14 * | |
15 * 2. Redistributions in binary form must reproduce the above copyright | |
16 * notice, this list of conditions and the following disclaimer in | |
17 * the documentation and/or other materials provided with the | |
18 * distribution. | |
19 * | |
20 * 3. The end-user documentation included with the redistribution, | |
21 * if any, must include the following acknowledgment: | |
22 * "This product includes software developed by the | |
23 * Walt Disney Internet Group (http://opensource.go.com/)." | |
24 * Alternately, this acknowledgment may appear in the software itself, | |
25 * if and wherever such third-party acknowledgments normally appear. | |
26 * | |
27 * 4. The names "Tea", "TeaServlet", "Kettle", "Trove" and "BeanDoc" must | |
28 * not be used to endorse or promote products derived from this | |
29 * software without prior written permission. For written | |
30 * permission, please contact opensource@dig.com. | |
31 * | |
32 * 5. Products derived from this software may not be called "Tea", | |
33 * "TeaServlet", "Kettle" or "Trove", nor may "Tea", "TeaServlet", | |
34 * "Kettle", "Trove" or "BeanDoc" appear in their name, without prior | |
35 * written permission of the Walt Disney Internet Group. | |
36 * | |
37 * THIS SOFTWARE IS PROVIDED ``AS IS'' AND ANY EXPRESSED OR IMPLIED | |
38 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES | |
39 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE | |
40 * DISCLAIMED. IN NO EVENT SHALL THE WALT DISNEY INTERNET GROUP OR ITS | |
41 * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, | |
42 * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, | |
43 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR | |
44 * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY | |
45 * OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT | |
46 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE | |
47 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. | |
48 * ==================================================================== | |
49 * | |
50 * For more information about Tea, please see http://opensource.go.com/. | |
51 */ | |
52 | |
53 package com.go.trove.log; | |
54 | |
55 import java.io.*; | |
56 import java.util.*; | |
57 | |
58 /****************************************************************************** | |
59 * LogEventParsingWriter parses the data written to it and converts it | |
60 * to LogEvent objects. Add a LogListener to intercept LogEvents. Events are | |
61 * parsed based on newline characters (LF, CRLF or CR) or a switch to a | |
62 * different thread. | |
63 * | |
64 * @author Brian S O'Neill | |
65 * @version | |
66 * <!--$$Revision: 1.1 $-->, <!--$$JustDate:--> 9/07/00 <!-- $--> | |
67 */ | |
68 public class LogEventParsingWriter extends Writer { | |
69 private Vector mListeners; | |
70 private Log mSource; | |
71 private int mType; | |
72 | |
73 private CharArrayWriter mMessageBuffer; | |
74 private Thread mMessageThread; | |
75 | |
76 private Date mTimestamp; | |
77 | |
78 // Set to true upon reading a CR so that if the next character is a LF, | |
79 // it is consumed. This is how CRLF patterns are discovered. | |
80 private boolean mTrackLF; | |
81 | |
82 /** | |
83 * @param source Source object to create events with. | |
84 * @param type Type of events to create. | |
85 */ | |
86 public LogEventParsingWriter(Log source, int type) { | |
87 mListeners = new Vector(2); | |
88 mSource = source; | |
89 mType = type; | |
90 | |
91 mMessageBuffer = new CharArrayWriter(); | |
92 } | |
93 | |
94 /** | |
95 * @param source Source object to create events with. | |
96 * @param type Type of events to create. | |
97 * @param lock Synchronization lock. | |
98 */ | |
99 public LogEventParsingWriter(Log source, int type, Object lock) { | |
100 super(lock); | |
101 mListeners = new Vector(2); | |
102 mSource = source; | |
103 mType = type; | |
104 | |
105 mMessageBuffer = new CharArrayWriter(); | |
106 } | |
107 | |
108 public void addLogListener(LogListener listener) { | |
109 mListeners.addElement(listener); | |
110 } | |
111 | |
112 public void removeLogListener(LogListener listener) { | |
113 mListeners.removeElement(listener); | |
114 } | |
115 | |
116 private void flushLogEvent() { | |
117 synchronized (lock) { | |
118 if (mMessageThread == null) { | |
119 return; | |
120 } | |
121 | |
122 String message = mMessageBuffer.toString(); | |
123 | |
124 if (mMessageBuffer.size() > 10000) { | |
125 mMessageBuffer = new CharArrayWriter(); | |
126 } | |
127 else { | |
128 mMessageBuffer.reset(); | |
129 } | |
130 | |
131 LogEvent e; | |
132 if (mTimestamp == null) { | |
133 e = new LogEvent(mSource, mType, message, mMessageThread); | |
134 } | |
135 else { | |
136 e = new LogEvent(mSource, mType, message, mMessageThread, | |
137 mTimestamp); | |
138 mTimestamp = null; | |
139 } | |
140 | |
141 synchronized (mListeners) { | |
142 Enumeration group = mListeners.elements(); | |
143 while (group.hasMoreElements()) { | |
144 ((LogListener)group.nextElement()).logMessage(e); | |
145 } | |
146 } | |
147 } | |
148 } | |
149 | |
150 public void write(char[] array, int off, int len) throws IOException { | |
151 synchronized (lock) { | |
152 if (!isEnabled()) { | |
153 if (mMessageBuffer.size() > 0) { | |
154 flushLogEvent(); | |
155 } | |
156 return; | |
157 } | |
158 | |
159 Thread current = Thread.currentThread(); | |
160 if (current != mMessageThread) { | |
161 if (mMessageBuffer.size() > 0) { | |
162 flushLogEvent(); | |
163 } | |
164 mMessageThread = current; | |
165 } | |
166 | |
167 int writtenLength = 0; | |
168 | |
169 int i = 0; | |
170 for (i=0; i<len; i++) { | |
171 char c = array[i + off]; | |
172 if (c == '\r') { | |
173 mTrackLF = true; | |
174 writeToBuffer(array, writtenLength + off, | |
175 i - writtenLength); | |
176 // Add one more than i to skip the CR. | |
177 writtenLength = i + 1; | |
178 flushLogEvent(); | |
179 } | |
180 else if (c == '\n') { | |
181 if (mTrackLF) { | |
182 // Consume the LF of CRLF. | |
183 mTrackLF = false; | |
184 writtenLength++; | |
185 } | |
186 else { | |
187 writeToBuffer(array, writtenLength + off, | |
188 i - writtenLength); | |
189 // Add one more than i to skip the LF. | |
190 writtenLength = i + 1; | |
191 flushLogEvent(); | |
192 } | |
193 } | |
194 else { | |
195 mTrackLF = false; | |
196 } | |
197 } | |
198 | |
199 writeToBuffer(array, writtenLength + off, i - writtenLength); | |
200 } | |
201 } | |
202 | |
203 public void flush() throws IOException { | |
204 } | |
205 | |
206 public void close() throws IOException { | |
207 synchronized (lock) { | |
208 mMessageBuffer.close(); | |
209 } | |
210 } | |
211 | |
212 /** | |
213 * Returning false discards written data, and events are not generated. | |
214 * Default implementation always returns true. | |
215 */ | |
216 public boolean isEnabled() { | |
217 return true; | |
218 } | |
219 | |
220 private void writeToBuffer(char[] array, int off, int len) { | |
221 if (mMessageBuffer.size() == 0) { | |
222 mTimestamp = new Date(); | |
223 } | |
224 mMessageBuffer.write(array, off, len); | |
225 } | |
226 } |