001: //GuiHandler.java
002: //-------------------------------------
003: //part of YACY
004: //(C) by Michael Peter Christen; mc@anomic.de
005: //first published on http://www.anomic.de
006: //Frankfurt, Germany, 2004
007: //
008: //This file ist contributed by Martin Thelian
009: //last major change: $LastChangedDate: 2008-01-23 23:08:32 +0000 (Mi, 23 Jan 2008) $ by $LastChangedBy: orbiter $
010: //Revision: $LastChangedRevision: 4382 $
011: //
012: //This program is free software; you can redistribute it and/or modify
013: //it under the terms of the GNU General Public License as published by
014: //the Free Software Foundation; either version 2 of the License, or
015: //(at your option) any later version.
016: //
017: //This program is distributed in the hope that it will be useful,
018: //but WITHOUT ANY WARRANTY; without even the implied warranty of
019: //MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
020: //GNU General Public License for more details.
021: //
022: //You should have received a copy of the GNU General Public License
023: //along with this program; if not, write to the Free Software
024: //Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
025: //
026: //Using this software in any meaning (reading, learning, copying, compiling,
027: //running) means that you agree that the Author(s) is (are) not responsible
028: //for cost, loss of data or any harm that may be caused directly or indirectly
029: //by usage of this softare or this documentation. The usage of this software
030: //is on your own risk. The installation and usage (starting/running) of this
031: //software may allow other people or application to access your computer and
032: //any attached devices and is highly dependent on the configuration of the
033: //software which must be done by the user of the software; the author(s) is
034: //(are) also not responsible for proper configuration and usage of the
035: //software, even if provoked by documentation provided together with
036: //the software.
037: //
038: //Any changes to this file according to the GPL as documented in the file
039: //gpl.txt aside this file in the shipment you received can be done to the
040: //lines that follows this copyright notice here, but changes must not be
041: //done inside the copyright notive above. A re-distribution must contain
042: //the intact and unchanged copyright notice.
043: //Contributions and changes to the program code must be marked as such.
044:
045: package de.anomic.server.logging;
046:
047: import java.util.ArrayList;
048: import java.util.logging.ErrorManager;
049: import java.util.logging.Filter;
050: import java.util.logging.Formatter;
051: import java.util.logging.Handler;
052: import java.util.logging.Level;
053: import java.util.logging.LogManager;
054: import java.util.logging.LogRecord;
055: import java.util.logging.SimpleFormatter;
056:
057: public class GuiHandler extends Handler {
058:
059: private final static int DEFAULT_SIZE = 400;
060: private int size = DEFAULT_SIZE;
061: private LogRecord buffer[];
062: int start, count;
063:
064: public GuiHandler() {
065: super ();
066: configure();
067: init();
068: }
069:
070: /**
071: * Get any configuration properties set
072: */
073: private void configure() {
074: LogManager manager = LogManager.getLogManager();
075: String className = getClass().getName();
076:
077: String level = manager.getProperty(className + ".level");
078: setLevel((level == null) ? Level.INFO : Level.parse(level));
079:
080: String filter = manager.getProperty(className + ".filter");
081: setFilter(makeFilter(filter));
082:
083: String formatter = manager
084: .getProperty(className + ".formatter");
085: setFormatter(makeFormatter(formatter));
086:
087: String sizeString = manager.getProperty(className + ".size");
088: this .size = parseSize(sizeString);
089: }
090:
091: private int parseSize(String sizeString) {
092: int newSize = DEFAULT_SIZE;
093: try {
094: newSize = Integer.parseInt(sizeString);
095: } catch (NumberFormatException e) {
096: newSize = DEFAULT_SIZE;
097: }
098: return newSize;
099: }
100:
101: private Filter makeFilter(String name) {
102: if (name == null)
103: return null;
104:
105: Filter f = null;
106: try {
107: Class<?> c = Class.forName(name);
108: f = (Filter) c.newInstance();
109: } catch (Exception e) {
110: System.err.println("Unable to load filter: " + name);
111: }
112: return f;
113: }
114:
115: private Formatter makeFormatter(String name) {
116: if (name == null)
117: return null;
118:
119: Formatter f = null;
120: try {
121: Class<?> c = Class.forName(name);
122: f = (Formatter) c.newInstance();
123: } catch (Exception e) {
124: f = new SimpleFormatter();
125: }
126: return f;
127: }
128:
129: // Initialize. Size is a count of LogRecords.
130: private void init() {
131: this .buffer = new LogRecord[this .size];
132: this .start = 0;
133: this .count = 0;
134: }
135:
136: public int getSize() {
137: return this .size;
138: }
139:
140: public synchronized void publish(LogRecord record) {
141: if (!isLoggable(record))
142: return;
143:
144: // write it to the buffer
145: int ix = (this .start + this .count) % this .buffer.length;
146: this .buffer[ix] = record;
147: if (this .count < this .buffer.length) {
148: this .count++;
149: } else {
150: this .start++;
151: }
152: }
153:
154: public synchronized LogRecord[] getLogArray() {
155: return this .getLogArray(null);
156: }
157:
158: public synchronized LogRecord[] getLogArray(Long sequenceNumberStart) {
159: ArrayList<LogRecord> tempBuffer = new ArrayList<LogRecord>(
160: this .count);
161:
162: for (int i = 0; i < this .count; i++) {
163: int ix = (this .start + i) % this .buffer.length;
164: LogRecord record = this .buffer[ix];
165: if ((sequenceNumberStart == null)
166: || (record.getSequenceNumber() >= sequenceNumberStart
167: .longValue())) {
168: tempBuffer.add(record);
169: }
170: }
171:
172: return (LogRecord[]) tempBuffer
173: .toArray(new LogRecord[tempBuffer.size()]);
174: }
175:
176: public synchronized String getLog(boolean reversed, int lineCount) {
177:
178: if ((lineCount > this .count) || (lineCount < 0))
179: lineCount = this .count;
180:
181: StringBuffer logMessages = new StringBuffer(this .count * 40);
182: Formatter logFormatter = getFormatter();
183:
184: try {
185: int start = (reversed) ? this .start + this .count - 1
186: : this .start;
187: LogRecord record = null;
188: for (int i = 0; i < lineCount; i++) {
189: int ix = (reversed) ? Math.abs((start - i)
190: % this .buffer.length) : (start + i)
191: % this .buffer.length;
192: record = this .buffer[ix];
193: logMessages.append(logFormatter.format(record));
194: }
195: return logMessages.toString();
196: } catch (Exception ex) {
197: // We don't want to throw an exception here, but we
198: // report the exception to any registered ErrorManager.
199: reportError(null, ex, ErrorManager.FORMAT_FAILURE);
200: return "Error while formatting the logging message";
201: }
202: }
203:
204: public synchronized String[] getLogLines(boolean reversed,
205: int lineCount) {
206:
207: if ((lineCount > this .count) || (lineCount < 0))
208: lineCount = this .count;
209:
210: ArrayList<String> logMessages = new ArrayList<String>(
211: this .count);
212: Formatter logFormatter = getFormatter();
213:
214: try {
215: int theStart = (reversed) ? this .start + this .count - 1
216: : this .start + this .count - lineCount;
217: LogRecord record = null;
218: for (int i = 0; i < lineCount; i++) {
219: int ix = (reversed) ? Math.abs((theStart - i)
220: % this .buffer.length) : (theStart + i)
221: % this .buffer.length;
222: record = this .buffer[ix];
223: logMessages.add(logFormatter.format(record));
224: }
225: return (String[]) logMessages
226: .toArray(new String[logMessages.size()]);
227: } catch (Exception ex) {
228: // We don't want to throw an exception here, but we
229: // report the exception to any registered ErrorManager.
230: reportError(null, ex, ErrorManager.FORMAT_FAILURE);
231: return new String[] { "Error while formatting the logging message" };
232: }
233: }
234:
235: public void flush() {
236: // TODO Auto-generated method stub
237:
238: }
239:
240: public void close() throws SecurityException {
241: // TODO Auto-generated method stub
242:
243: }
244:
245: }
|