KickJava   Java API By Example, From Geeks To Geeks.

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


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 java.io.Serializable JavaDoc;
15
16 /**
17  * The base class for events. These are used for logging query execution
18  * times etc. for performance tuning and debugging. Every event created in
19  * a VM is assigned a unique id.
20  */

21 public abstract class LogEvent implements Serializable JavaDoc {
22
23     protected int id;
24     protected String JavaDoc remoteClient;
25     protected String JavaDoc userString;
26     protected long datastoreTxId;
27     protected long start;
28     protected int totalMs;
29     protected String JavaDoc errorMsg;
30
31     private static int lastId;
32
33     private static ThreadLocal JavaDoc contextStore = new ThreadLocal JavaDoc();
34
35
36     
37
38     /**
39      * Information associated with the current thread that is added to all
40      * events logged.
41      */

42     public static class Context {
43         public String JavaDoc remoteClient;
44         public String JavaDoc userString;
45     }
46
47     public LogEvent() {
48         id = ++lastId;
49         Context c = getContext();
50         remoteClient = c.remoteClient;
51         userString = c.userString;
52         totalMs = -1;
53         start = System.currentTimeMillis();
54     }
55
56     /**
57      * Get the event context for the calling thread.
58      */

59     public static Context getContext() {
60
61         Context c = (Context)contextStore.get();
62         if (c == null) contextStore.set(c = new Context());
63         return c;
64
65
66     }
67
68     /**
69      * Get the ID of the last event logged.
70      */

71     public static int getLastId() {
72         return lastId;
73     }
74
75     public long getStart() {
76         return start;
77     }
78
79     public final int getId() {
80         return id;
81     }
82
83     public long getDatastoreTxId() {
84         return datastoreTxId;
85     }
86
87     public void setDatastoreTxId(long datastoreTxId) {
88         this.datastoreTxId = datastoreTxId;
89     }
90
91     public int getTotalMs() {
92         return totalMs;
93     }
94
95     public void setTotalMs(int totalMs) {
96         this.totalMs = totalMs;
97     }
98
99     public void updateTotalMs() {
100         totalMs = (int)(System.currentTimeMillis() - start);
101     }
102
103     public void zeroTotalMs() {
104         totalMs = 0;
105     }
106
107     public String JavaDoc getErrorMsg() {
108         return errorMsg;
109     }
110
111     public void setErrorMsg(String JavaDoc errorMsg) {
112         this.errorMsg = errorMsg;
113     }
114
115     public void setErrorMsg(Throwable JavaDoc t) {
116         errorMsg = t.getClass().getName() + ": " + t.getMessage();
117     }
118
119     public boolean getOk() {
120         return errorMsg == null;
121     }
122
123     public String JavaDoc getRemoteClient() {
124         if (remoteClient == null) {
125             return userString;
126         } else if (userString == null) {
127             return remoteClient;
128         } else {
129             return remoteClient + " - " + userString;
130         }
131     }
132
133     /**
134      * Get a short descriptive name for this event.
135      */

136     public abstract String JavaDoc getName();
137
138     /**
139      * Get a long description for this event (e.g. the query text).
140      */

141     public abstract String JavaDoc getDescription();
142
143     /**
144      * Dummy set method so the Workbench will allow 'editing' for cut and
145      * paste.
146      */

147     public void setDescription(String JavaDoc s) {
148     }
149
150     public String JavaDoc toString() {
151         String JavaDoc d = getDescription();
152         if (d == null) return getName();
153         else return getName() + " " + d;
154     }
155
156     /**
157      * Should this event be sorted onto its own tab in the perf monitoring
158      * form? Major events e.g. executing a query should return true here.
159      */

160     public boolean isOwnTab() {
161         return false;
162     }
163
164     /**
165      * If this event has an int type then it is returned.
166      */

167     public int getType() {
168         return 0;
169     }
170
171 }
172
Popular Tags