KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > versant > core > logging > LogEventStore


1
2 /*
3  * Copyright (c) 1998 - 2005 Versant Corporation
4  * All rights reserved. This program and the accompanying materials
5  * are made available under the terms of the Eclipse Public License v1.0
6  * which accompanies this distribution, and is available at
7  * http://www.eclipse.org/legal/epl-v10.html
8  *
9  * Contributors:
10  * Versant Corporation - initial API and implementation
11  */

12 package com.versant.core.logging;
13
14 import com.versant.core.common.BindingSupportImpl;
15
16 /**
17  * This stores a configurable number of events in a ring buffer and provides
18  * API's to access the buffer.
19  */

20 public final class LogEventStore {
21
22     private int size = 1000;
23     private LogEvent[] buf = new LogEvent[size];
24     private int pos; // where the next event is stored in buf
25
private int count;
26     private int eventsLogged;
27
28     public static final String JavaDoc LOG_EVENTS_NONE = "none";
29     public static final String JavaDoc LOG_EVENTS_ERRORS = "errors";
30     public static final String JavaDoc LOG_EVENTS_NORMAL = "normal";
31     public static final String JavaDoc LOG_EVENTS_VERBOSE = "verbose";
32     public static final String JavaDoc LOG_EVENTS_ALL = "all";
33
34     private String JavaDoc logEvents;
35
36     private boolean severe;
37     private boolean warning;
38     private boolean info;
39     private boolean config;
40     private boolean fine;
41     private boolean finer;
42     private boolean finest;
43
44     private boolean logEventsToSysOut = true;
45
46     public LogEventStore() {
47         setLogEvents(LOG_EVENTS_NORMAL);
48     }
49
50     public synchronized void log(LogEvent ev) {
51         synchronized (this) {
52             buf[pos] = ev;
53             pos = (pos + 1) % size;
54             if (count < size) count++;
55         }
56         eventsLogged++;
57         if (logEventsToSysOut) System.out.println(ev);
58     }
59
60     public int getMaxEvents() {
61         return size;
62     }
63
64     /**
65      * Get the number of events logged so far.
66      */

67     public int getEventsLogged() {
68         return eventsLogged;
69     }
70
71     /**
72      * Set the maximum number of events to store in the buffer.
73      */

74     public synchronized void setMaxEvents(int max) {
75         LogEvent[] old = copyEvents(0);
76         buf = new LogEvent[size = max];
77         if (old != null) {
78             int n = old.length;
79             if (n > max) n = max;
80             System.arraycopy(old, old.length - n, buf, 0, n);
81             count = n;
82             pos = n % size;
83         } else {
84             count = 0;
85             pos = 0;
86         }
87     }
88
89     public synchronized LogEvent[] copyEvents(int id) {
90         if (count < size) {
91             int first;
92             for (first = pos - 1; first >= 0; first--) {
93                 if (buf[first].getId() == id) break;
94             }
95             first++;
96             int n = pos - first;
97             if (n == 0) return null;
98             LogEvent[] ans = new LogEvent[n];
99             System.arraycopy(buf, first, ans, 0, n);
100             return ans;
101         } else {
102             if (buf[(pos + size - 1) % size].getId() == id) return null;
103             int first = pos;
104             int c = size;
105             for (; c > 0; first = (first + 1) % size, c--) {
106                 if (buf[first].getId() == id) break;
107             }
108             first = (first + 1) % size;
109             if (first >= pos) {
110                 int h1 = size - first;
111                 int h2 = pos;
112                 LogEvent[] ans = new LogEvent[h1 + h2];
113                 System.arraycopy(buf, first, ans, 0, h1);
114                 System.arraycopy(buf, 0, ans, h1, h2);
115                 return ans;
116             } else {
117                 int n = pos - first;
118                 LogEvent[] ans = new LogEvent[n];
119                 System.arraycopy(buf, first, ans, 0, n);
120                 return ans;
121             }
122         }
123     }
124
125     public boolean isSevere() {
126         return severe;
127     }
128
129     public boolean isWarning() {
130         return warning;
131     }
132
133     public boolean isInfo() {
134         return info;
135     }
136
137     public boolean isConfig() {
138         return config;
139     }
140
141     public boolean isFine() {
142         return fine;
143     }
144
145     public boolean isFiner() {
146         return finer;
147     }
148
149     public boolean isFinest() {
150         return finest;
151     }
152
153     /**
154      * Return a Serializable Javabean that provides status information on
155      * this component. This may be null if not supported.
156      */

157     public Object JavaDoc getStatusBean() {
158         return null;
159     }
160
161     public String JavaDoc getLogEvents() {
162         return logEvents;
163     }
164
165     public void setLogEvents(String JavaDoc logEvents) {
166         if (logEvents.equals(LOG_EVENTS_NONE)) {
167             severe = false;
168             warning = false;
169             info = false;
170             config = false;
171             fine = false;
172             finer = false;
173             finest = false;
174         } else if (logEvents.equals(LOG_EVENTS_ERRORS)) {
175             severe = true;
176             warning = false;
177             info = false;
178             config = false;
179             fine = false;
180             finer = false;
181             finest = false;
182         } else if (logEvents.equals(LOG_EVENTS_NORMAL)) {
183             severe = true;
184             warning = true;
185             info = true;
186             config = false;
187             fine = false;
188             finer = false;
189             finest = false;
190         } else if (logEvents.equals(LOG_EVENTS_VERBOSE)) {
191             severe = true;
192             warning = true;
193             info = true;
194             config = true;
195             fine = true;
196             finer = false;
197             finest = false;
198         } else if (logEvents.equals(LOG_EVENTS_ALL)) {
199             severe = true;
200             warning = true;
201             info = true;
202             config = true;
203             fine = true;
204             finer = true;
205             finest = true;
206         } else {
207             throw BindingSupportImpl.getInstance().illegalArgument(
208                     "Invalid group: '" + logEvents + "'");
209         }
210         this.logEvents = logEvents;
211     }
212
213     public boolean isLogEventsToSysOut() {
214         return logEventsToSysOut;
215     }
216
217     public void setLogEventsToSysOut(boolean logEventsToSysOut) {
218         this.logEventsToSysOut = logEventsToSysOut;
219     }
220
221 }
222
Popular Tags