001: /*
002: The contents of this file are subject to the Mozilla Public License Version 1.1
003: (the "License"); you may not use this file except in compliance with the License.
004: You may obtain a copy of the License at http://www.mozilla.org/MPL/
005:
006: Software distributed under the License is distributed on an "AS IS" basis,
007: WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
008: for the specific language governing rights and limitations under the License.
009:
010: The Original Code is "The Columba Project"
011:
012: The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
013: Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
014:
015: All Rights Reserved.
016: */
017:
018: package org.columba.core.scripting;
019:
020: import java.io.PrintWriter;
021: import java.io.StringWriter;
022: import java.util.Collections;
023: import java.util.LinkedList;
024: import java.util.List;
025: import java.util.Observable;
026:
027: /**
028: @author Celso Pinto (cpinto@yimports.com)
029: */
030: public class ScriptLogger extends Observable {
031:
032: public static class LogEntry {
033: private String message, details;
034:
035: public LogEntry() {
036: this ("", "");
037: }
038:
039: public LogEntry(String message) {
040: this (message, "");
041: }
042:
043: public LogEntry(String message, String details) {
044: this .message = message;
045: this .details = details;
046: }
047:
048: public String getMessage() {
049: return message;
050: }
051:
052: public void setMessage(String message) {
053: this .message = message;
054: }
055:
056: public String getDetails() {
057: return details;
058: }
059:
060: public void setDetails(String details) {
061: this .details = details;
062: }
063: }
064:
065: private LinkedList<LogEntry> logger;
066: private static ScriptLogger self = null;
067: private static final int MAX_LOG_ENTRIES = 200;
068:
069: private ScriptLogger() {
070: logger = new LinkedList<LogEntry>();
071: }
072:
073: public static ScriptLogger getInstance() {
074: if (self == null)
075: self = new ScriptLogger();
076:
077: return self;
078: }
079:
080: public void append(String message, Exception details) {
081: StringWriter writer = new StringWriter();
082: details.printStackTrace(new PrintWriter(writer));
083: append(message, writer.toString());
084: }
085:
086: public void append(String message, String details) {
087: append(new LogEntry(message, details));
088: }
089:
090: public void append(String message) {
091: append(message, "");
092: }
093:
094: public void append(LogEntry entry) {
095: logger.addFirst(entry);
096: if (logger.size() > MAX_LOG_ENTRIES)
097: logger.remove(0);
098:
099: setChanged();
100: notifyObservers(entry);
101: }
102:
103: public void clear() {
104: logger.clear();
105: }
106:
107: public List<LogEntry> dumpCurrentLog() {
108: return Collections.unmodifiableList(logger);
109: }
110:
111: }
|