KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > org > columba > core > scripting > ScriptLogger


1 /*
2  The contents of this file are subject to the Mozilla Public License Version 1.1
3  (the "License"); you may not use this file except in compliance with the License.
4  You may obtain a copy of the License at http://www.mozilla.org/MPL/
5
6  Software distributed under the License is distributed on an "AS IS" basis,
7  WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
8  for the specific language governing rights and limitations under the License.
9
10  The Original Code is "The Columba Project"
11
12  The Initial Developers of the Original Code are Frederik Dietz and Timo Stich.
13  Portions created by Frederik Dietz and Timo Stich are Copyright (C) 2003.
14
15  All Rights Reserved.
16  */

17
18 package org.columba.core.scripting;
19
20 import java.io.PrintWriter JavaDoc;
21 import java.io.StringWriter JavaDoc;
22 import java.util.Collections JavaDoc;
23 import java.util.LinkedList JavaDoc;
24 import java.util.List JavaDoc;
25 import java.util.Observable JavaDoc;
26
27 /**
28     @author Celso Pinto (cpinto@yimports.com)
29  */

30 public class ScriptLogger
31     extends Observable JavaDoc
32 {
33
34     public static class LogEntry
35     {
36         private String JavaDoc
37             message,
38             details;
39
40         public LogEntry()
41         {
42             this("", "");
43         }
44
45         public LogEntry(String JavaDoc message)
46         {
47             this(message, "");
48         }
49
50         public LogEntry(String JavaDoc message, String JavaDoc details)
51         {
52             this.message = message;
53             this.details = details;
54         }
55
56         public String JavaDoc getMessage()
57         {
58             return message;
59         }
60
61         public void setMessage(String JavaDoc message)
62         {
63             this.message = message;
64         }
65
66         public String JavaDoc getDetails()
67         {
68             return details;
69         }
70
71         public void setDetails(String JavaDoc details)
72         {
73             this.details = details;
74         }
75     }
76
77     private LinkedList JavaDoc<LogEntry> logger;
78     private static ScriptLogger self = null;
79     private static final int MAX_LOG_ENTRIES = 200;
80
81     private ScriptLogger()
82     {
83         logger = new LinkedList JavaDoc<LogEntry>();
84     }
85
86
87     public static ScriptLogger getInstance()
88     {
89         if (self == null) self = new ScriptLogger();
90
91         return self;
92     }
93
94     public void append(String JavaDoc message, Exception JavaDoc details)
95     {
96         StringWriter JavaDoc writer = new StringWriter JavaDoc();
97         details.printStackTrace(new PrintWriter JavaDoc(writer));
98         append(message, writer.toString());
99     }
100
101     public void append(String JavaDoc message, String JavaDoc details)
102     {
103         append(new LogEntry(message, details));
104     }
105
106     public void append(String JavaDoc message)
107     {
108         append(message, "");
109     }
110
111     public void append(LogEntry entry)
112     {
113         logger.addFirst(entry);
114         if (logger.size() > MAX_LOG_ENTRIES) logger.remove(0);
115
116         setChanged();
117         notifyObservers(entry);
118     }
119
120     public void clear()
121     {
122         logger.clear();
123     }
124
125     public List JavaDoc<LogEntry> dumpCurrentLog()
126     {
127         return Collections.unmodifiableList(logger);
128     }
129
130 }
131
Popular Tags