KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sleepycat > je > utilint > Tracer


1 /*-
2  * See the file LICENSE for redistribution information.
3  *
4  * Copyright (c) 2002,2006 Oracle. All rights reserved.
5  *
6  * $Id: Tracer.java,v 1.42 2006/10/30 21:14:29 bostic Exp $
7  */

8
9 package com.sleepycat.je.utilint;
10
11 import java.io.PrintWriter JavaDoc;
12 import java.io.StringWriter JavaDoc;
13 import java.nio.ByteBuffer JavaDoc;
14 import java.sql.Timestamp JavaDoc;
15 import java.util.Calendar JavaDoc;
16 import java.util.logging.Level JavaDoc;
17
18 import com.sleepycat.je.DatabaseException;
19 import com.sleepycat.je.config.ConfigParam;
20 import com.sleepycat.je.dbi.EnvironmentImpl;
21 import com.sleepycat.je.log.LogEntryType;
22 import com.sleepycat.je.log.LogReadable;
23 import com.sleepycat.je.log.LogUtils;
24 import com.sleepycat.je.log.LoggableObject;
25
26 /**
27  * The Tracer generates debug messages that are sent to the java.util.Logging
28  * facility. There are three log handlers set up for logging -- the database
29  * log itself, an output file, and stdout (the "console"). By default, only
30  * the database file is enabled.
31  */

32 public class Tracer implements LoggableObject, LogReadable {
33
34     /*
35      * Name pattern for tracing output that's been directed into a log file by
36      * enabling the file handler.
37      */

38     public static final String JavaDoc INFO_FILES = "je.info";
39
40     /*
41      * Contents of a debug message.
42      */

43     private Timestamp JavaDoc time;
44     private String JavaDoc msg;
45
46     /**
47      * Create a new debug record.
48      */

49     public Tracer(String JavaDoc msg) {
50         this.time = getCurrentTimestamp();
51         this.msg = msg;
52     }
53
54     /**
55      * Create trace record that will be filled in from the log.
56      */

57     public Tracer() {
58     }
59
60     /*
61      * Static utility methods for submitting information for logging in the
62      * text log file, the database log, and stdout.
63      */

64
65     /**
66      * Logger method for recording a general message.
67      */

68     public static void trace(Level JavaDoc logLevel,
69                              EnvironmentImpl envImpl,
70                              String JavaDoc msg) {
71         envImpl.getLogger().log(logLevel, msg);
72     }
73     
74     /**
75      * Logger method for recording an exception and stacktrace.
76      */

77     public static void trace(EnvironmentImpl envImpl,
78                              String JavaDoc sourceClass,
79                              String JavaDoc sourceMethod,
80                              String JavaDoc msg,
81                              Throwable JavaDoc t) {
82
83         /*
84          * Give it to the Logger, which will funnel it to stdout and/or the
85          * text file and/or the database log file
86          */

87         envImpl.getLogger().logp(Level.SEVERE,
88                  sourceClass,
89                  sourceMethod,
90                  msg + "\n" + Tracer.getStackTrace(t));
91     }
92
93     /**
94      * Parse a logging level config parameter, and return a more explanatory
95      * error message if it doesn't parse.
96      */

97     public static Level JavaDoc parseLevel(EnvironmentImpl envImpl,
98                                    ConfigParam configParam)
99         throws DatabaseException {
100         
101         Level JavaDoc level = null;
102         try {
103             String JavaDoc levelVal = envImpl.getConfigManager().get(configParam);
104             level = Level.parse(levelVal);
105         } catch (IllegalArgumentException JavaDoc e) {
106             throw new DatabaseException("Problem parsing parameter " +
107                     configParam.getName() +
108                     ": " + e.getMessage(), e);
109         }
110         return level;
111     }
112              
113     /*
114      * Helpers
115      */

116     public String JavaDoc getMessage() {
117         return msg;
118     }
119
120     /**
121      * @return a timestamp for "now"
122      */

123     private Timestamp JavaDoc getCurrentTimestamp() {
124         Calendar JavaDoc cal = Calendar.getInstance();
125         return new Timestamp JavaDoc(cal.getTime().getTime());
126     }
127
128     /**
129      * @return the stacktrace for an exception
130      */

131     public static String JavaDoc getStackTrace(Throwable JavaDoc t) {
132         StringWriter JavaDoc s = new StringWriter JavaDoc();
133         t.printStackTrace(new PrintWriter JavaDoc(s));
134         String JavaDoc stackTrace = s.toString();
135         stackTrace = stackTrace.replaceAll("<", "&lt;");
136         stackTrace = stackTrace.replaceAll(">", "&gt;");
137         return stackTrace;
138     }
139         
140     /*
141      * Logging support
142      */

143
144     /**
145      * @see LoggableObject#getLogType
146      */

147     public LogEntryType getLogType() {
148         return LogEntryType.LOG_TRACE;
149     }
150
151     /**
152      * @see LoggableObject#marshallOutsideWriteLatch
153      * Can be marshalled outside the log write latch.
154      */

155     public boolean marshallOutsideWriteLatch() {
156         return true;
157     }
158
159     /**
160      * @see LoggableObject#countAsObsoleteWhenLogged
161      */

162     public boolean countAsObsoleteWhenLogged() {
163         return false;
164     }
165
166     /**
167      * @see LoggableObject#postLogWork
168      */

169     public void postLogWork(long justLoggedLsn) {
170     }
171
172     /**
173      * @see LoggableObject#getLogSize()
174      */

175     public int getLogSize() {
176         return (LogUtils.getTimestampLogSize() +
177                 LogUtils.getStringLogSize(msg));
178     }
179
180     /**
181      * @see LoggableObject#writeToLog
182      */

183     public void writeToLog(ByteBuffer JavaDoc logBuffer) {
184         /* Load the header. */
185         LogUtils.writeTimestamp(logBuffer, time);
186         LogUtils.writeString(logBuffer, msg);
187     }
188
189     /**
190      * @see LogReadable#readFromLog
191      */

192     public void readFromLog(ByteBuffer JavaDoc itemBuffer, byte entryTypeVersion) {
193         /* See how many we want to read direct. */
194         time = LogUtils.readTimestamp(itemBuffer);
195         msg = LogUtils.readString(itemBuffer);
196     }
197
198     /**
199      * @see LogReadable#dumpLog
200      */

201     public void dumpLog(StringBuffer JavaDoc sb, boolean verbose) {
202         sb.append("<Dbg time=\"");
203         sb.append(time);
204         sb.append("\">");
205         sb.append("<msg val=\"");
206         sb.append(msg);
207         sb.append("\"/>");
208         sb.append("</Dbg>");
209     }
210
211     /**
212      * @see LogReadable#logEntryIsTransactional
213      */

214     public boolean logEntryIsTransactional() {
215     return false;
216     }
217
218     /**
219      * @see LogReadable#getTransactionId
220      */

221     public long getTransactionId() {
222     return 0;
223     }
224
225     public String JavaDoc toString() {
226         return (time + "/" + msg);
227     }
228
229     /**
230      * For unit tests.
231      */

232
233     /**
234      * Just in case it's ever used as a hash key.
235      */

236     public int hashCode() {
237         return toString().hashCode();
238     }
239
240     /**
241      * Override Object.equals
242      */

243     public boolean equals(Object JavaDoc obj) {
244         /* Same instance? */
245         if (this == obj) {
246             return true;
247         }
248
249         /* Is it another Tracer? */
250         if (!(obj instanceof Tracer)) {
251             return false;
252         }
253
254         /*
255      * We could compare all the fields individually, but since they're all
256      * placed in our toString() method, we can just compare the String
257      * version of each offer.
258      */

259         return (toString().equals(obj.toString()));
260     }
261 }
262
Popular Tags