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 java.io.Serializable;
014:
015: /**
016: * The base class for events. These are used for logging query execution
017: * times etc. for performance tuning and debugging. Every event created in
018: * a VM is assigned a unique id.
019: */
020: public abstract class LogEvent implements Serializable {
021:
022: protected int id;
023: protected String remoteClient;
024: protected String userString;
025: protected long datastoreTxId;
026: protected long start;
027: protected int totalMs;
028: protected String errorMsg;
029:
030: private static int lastId;
031:
032: private static ThreadLocal contextStore = new ThreadLocal();
033:
034: /**
035: * Information associated with the current thread that is added to all
036: * events logged.
037: */
038: public static class Context {
039: public String remoteClient;
040: public String userString;
041: }
042:
043: public LogEvent() {
044: id = ++lastId;
045: Context c = getContext();
046: remoteClient = c.remoteClient;
047: userString = c.userString;
048: totalMs = -1;
049: start = System.currentTimeMillis();
050: }
051:
052: /**
053: * Get the event context for the calling thread.
054: */
055: public static Context getContext() {
056:
057: Context c = (Context) contextStore.get();
058: if (c == null)
059: contextStore.set(c = new Context());
060: return c;
061:
062: }
063:
064: /**
065: * Get the ID of the last event logged.
066: */
067: public static int getLastId() {
068: return lastId;
069: }
070:
071: public long getStart() {
072: return start;
073: }
074:
075: public final int getId() {
076: return id;
077: }
078:
079: public long getDatastoreTxId() {
080: return datastoreTxId;
081: }
082:
083: public void setDatastoreTxId(long datastoreTxId) {
084: this .datastoreTxId = datastoreTxId;
085: }
086:
087: public int getTotalMs() {
088: return totalMs;
089: }
090:
091: public void setTotalMs(int totalMs) {
092: this .totalMs = totalMs;
093: }
094:
095: public void updateTotalMs() {
096: totalMs = (int) (System.currentTimeMillis() - start);
097: }
098:
099: public void zeroTotalMs() {
100: totalMs = 0;
101: }
102:
103: public String getErrorMsg() {
104: return errorMsg;
105: }
106:
107: public void setErrorMsg(String errorMsg) {
108: this .errorMsg = errorMsg;
109: }
110:
111: public void setErrorMsg(Throwable t) {
112: errorMsg = t.getClass().getName() + ": " + t.getMessage();
113: }
114:
115: public boolean getOk() {
116: return errorMsg == null;
117: }
118:
119: public String getRemoteClient() {
120: if (remoteClient == null) {
121: return userString;
122: } else if (userString == null) {
123: return remoteClient;
124: } else {
125: return remoteClient + " - " + userString;
126: }
127: }
128:
129: /**
130: * Get a short descriptive name for this event.
131: */
132: public abstract String getName();
133:
134: /**
135: * Get a long description for this event (e.g. the query text).
136: */
137: public abstract String getDescription();
138:
139: /**
140: * Dummy set method so the Workbench will allow 'editing' for cut and
141: * paste.
142: */
143: public void setDescription(String s) {
144: }
145:
146: public String toString() {
147: String d = getDescription();
148: if (d == null)
149: return getName();
150: else
151: return getName() + " " + d;
152: }
153:
154: /**
155: * Should this event be sorted onto its own tab in the perf monitoring
156: * form? Major events e.g. executing a query should return true here.
157: */
158: public boolean isOwnTab() {
159: return false;
160: }
161:
162: /**
163: * If this event has an int type then it is returned.
164: */
165: public int getType() {
166: return 0;
167: }
168:
169: }
|