001: /*
002: * Copyright (c) 1998 - 2005 Versant Corporation
003: * All rights reserved. This program and the accompanying materials
004: * are made available under the terms of the Eclipse Public License v1.0
005: * which accompanies this distribution, and is available at
006: * http://www.eclipse.org/legal/epl-v10.html
007: *
008: * Contributors:
009: * Versant Corporation - initial API and implementation
010: */
011: package com.versant.core.logging;
012:
013: import com.versant.core.common.BindingSupportImpl;
014:
015: /**
016: * This stores a configurable number of events in a ring buffer and provides
017: * API's to access the buffer.
018: */
019: public final class LogEventStore {
020:
021: private int size = 1000;
022: private LogEvent[] buf = new LogEvent[size];
023: private int pos; // where the next event is stored in buf
024: private int count;
025: private int eventsLogged;
026:
027: public static final String LOG_EVENTS_NONE = "none";
028: public static final String LOG_EVENTS_ERRORS = "errors";
029: public static final String LOG_EVENTS_NORMAL = "normal";
030: public static final String LOG_EVENTS_VERBOSE = "verbose";
031: public static final String LOG_EVENTS_ALL = "all";
032:
033: private String logEvents;
034:
035: private boolean severe;
036: private boolean warning;
037: private boolean info;
038: private boolean config;
039: private boolean fine;
040: private boolean finer;
041: private boolean finest;
042:
043: private boolean logEventsToSysOut = true;
044:
045: public LogEventStore() {
046: setLogEvents(LOG_EVENTS_NORMAL);
047: }
048:
049: public synchronized void log(LogEvent ev) {
050: synchronized (this ) {
051: buf[pos] = ev;
052: pos = (pos + 1) % size;
053: if (count < size)
054: count++;
055: }
056: eventsLogged++;
057: if (logEventsToSysOut)
058: System.out.println(ev);
059: }
060:
061: public int getMaxEvents() {
062: return size;
063: }
064:
065: /**
066: * Get the number of events logged so far.
067: */
068: public int getEventsLogged() {
069: return eventsLogged;
070: }
071:
072: /**
073: * Set the maximum number of events to store in the buffer.
074: */
075: public synchronized void setMaxEvents(int max) {
076: LogEvent[] old = copyEvents(0);
077: buf = new LogEvent[size = max];
078: if (old != null) {
079: int n = old.length;
080: if (n > max)
081: n = max;
082: System.arraycopy(old, old.length - n, buf, 0, n);
083: count = n;
084: pos = n % size;
085: } else {
086: count = 0;
087: pos = 0;
088: }
089: }
090:
091: public synchronized LogEvent[] copyEvents(int id) {
092: if (count < size) {
093: int first;
094: for (first = pos - 1; first >= 0; first--) {
095: if (buf[first].getId() == id)
096: break;
097: }
098: first++;
099: int n = pos - first;
100: if (n == 0)
101: return null;
102: LogEvent[] ans = new LogEvent[n];
103: System.arraycopy(buf, first, ans, 0, n);
104: return ans;
105: } else {
106: if (buf[(pos + size - 1) % size].getId() == id)
107: return null;
108: int first = pos;
109: int c = size;
110: for (; c > 0; first = (first + 1) % size, c--) {
111: if (buf[first].getId() == id)
112: break;
113: }
114: first = (first + 1) % size;
115: if (first >= pos) {
116: int h1 = size - first;
117: int h2 = pos;
118: LogEvent[] ans = new LogEvent[h1 + h2];
119: System.arraycopy(buf, first, ans, 0, h1);
120: System.arraycopy(buf, 0, ans, h1, h2);
121: return ans;
122: } else {
123: int n = pos - first;
124: LogEvent[] ans = new LogEvent[n];
125: System.arraycopy(buf, first, ans, 0, n);
126: return ans;
127: }
128: }
129: }
130:
131: public boolean isSevere() {
132: return severe;
133: }
134:
135: public boolean isWarning() {
136: return warning;
137: }
138:
139: public boolean isInfo() {
140: return info;
141: }
142:
143: public boolean isConfig() {
144: return config;
145: }
146:
147: public boolean isFine() {
148: return fine;
149: }
150:
151: public boolean isFiner() {
152: return finer;
153: }
154:
155: public boolean isFinest() {
156: return finest;
157: }
158:
159: /**
160: * Return a Serializable Javabean that provides status information on
161: * this component. This may be null if not supported.
162: */
163: public Object getStatusBean() {
164: return null;
165: }
166:
167: public String getLogEvents() {
168: return logEvents;
169: }
170:
171: public void setLogEvents(String logEvents) {
172: if (logEvents.equals(LOG_EVENTS_NONE)) {
173: severe = false;
174: warning = false;
175: info = false;
176: config = false;
177: fine = false;
178: finer = false;
179: finest = false;
180: } else if (logEvents.equals(LOG_EVENTS_ERRORS)) {
181: severe = true;
182: warning = false;
183: info = false;
184: config = false;
185: fine = false;
186: finer = false;
187: finest = false;
188: } else if (logEvents.equals(LOG_EVENTS_NORMAL)) {
189: severe = true;
190: warning = true;
191: info = true;
192: config = false;
193: fine = false;
194: finer = false;
195: finest = false;
196: } else if (logEvents.equals(LOG_EVENTS_VERBOSE)) {
197: severe = true;
198: warning = true;
199: info = true;
200: config = true;
201: fine = true;
202: finer = false;
203: finest = false;
204: } else if (logEvents.equals(LOG_EVENTS_ALL)) {
205: severe = true;
206: warning = true;
207: info = true;
208: config = true;
209: fine = true;
210: finer = true;
211: finest = true;
212: } else {
213: throw BindingSupportImpl.getInstance().illegalArgument(
214: "Invalid group: '" + logEvents + "'");
215: }
216: this .logEvents = logEvents;
217: }
218:
219: public boolean isLogEventsToSysOut() {
220: return logEventsToSysOut;
221: }
222:
223: public void setLogEventsToSysOut(boolean logEventsToSysOut) {
224: this.logEventsToSysOut = logEventsToSysOut;
225: }
226:
227: }
|