KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > com > sun > jts > trace > TraceUtil


1 /*
2  * The contents of this file are subject to the terms
3  * of the Common Development and Distribution License
4  * (the License). You may not use this file except in
5  * compliance with the License.
6  *
7  * You can obtain a copy of the license at
8  * https://glassfish.dev.java.net/public/CDDLv1.0.html or
9  * glassfish/bootstrap/legal/CDDLv1.0.txt.
10  * See the License for the specific language governing
11  * permissions and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL
14  * Header Notice in each file and include the License file
15  * at glassfish/bootstrap/legal/CDDLv1.0.txt.
16  * If applicable, add the following below the CDDL Header,
17  * with the fields enclosed by brackets [] replaced by
18  * you own identifying information:
19  * "Portions Copyrighted [year] [name of copyright owner]"
20  *
21  * Copyright 2006 Sun Microsystems, Inc. All rights reserved.
22  */

23
24 /*
25  * Copyright 2004-2005 Sun Microsystems, Inc. All rights reserved.
26  * Use is subject to license terms.
27  */

28 package com.sun.jts.trace;
29
30 import java.io.*;
31 import com.sun.jts.CosTransactions.*;
32 import org.omg.CosTransactions.*;
33
34 /**
35  * This class is used to set trace properties and print trace statements. The print method does the printing.
36  * All the methods are unsynchronized. Since setting of properties doesn't happen simultaneously with print
37  * in current usage, this is fine. The tracing should be enabled/disabled by calling
38  * <code> Configuration.enableTrace()/disableTrace()</code>
39  * prior to any operation on TraceUtil.
40  * It uses TraceRecordFormatter for formatting the trace record.
41  *
42  * @author <a HREF="mailto:kannan.srinivasan@sun.com">Kannan Srinivasan</a>
43  * @version 1.0
44  */

45 public class TraceUtil
46 {
47     private static int m_currentTraceLevel = TraceLevel.IAS_JTS_TRACE_TRIVIAL ;
48     private static char m_fieldDelimiter = ':';
49     private static String JavaDoc m_traceRecordTag = "iAS_JTS_Trace> ";
50     private static PrintWriter m_traceWriter = null ;
51
52     static
53     {
54         m_traceWriter = new PrintWriter(System.out);
55     }
56
57  /**
58    * Initialises the trace class with given output writer.
59    *
60    * @param traceWriter an <code>PrintWriter</code> value
61    */

62     public static void init(PrintWriter traceWriter)
63     {
64         setTraceWriter(traceWriter);
65     }
66
67   /**
68    * Sets the output writer. By default the output writer is set to Stdout.
69    *
70    * @param traceWriter an <code>PrintWriter</code> value
71    */

72     public static void setTraceWriter(PrintWriter traceWriter)
73     {
74     m_traceWriter = traceWriter;
75     }
76
77   /**
78    * Gets the current output writer.
79    *
80    * @return an <code>PrintWriter</code> value
81    */

82     public static PrintWriter getTraceWriter()
83     {
84         return m_traceWriter;
85     }
86
87   /**
88    * Gets the current trace level. Returns an integer as per the TraceLevel constants.
89    *
90    * @return an <code>int</code> value
91    */

92     public static int getCurrentTraceLevel()
93     {
94         return m_currentTraceLevel;
95     }
96
97   /**
98    * Sets the current trace level. The argument is tested for its validity and trace level is set.
99    * Else an exception is raised.
100    *
101    * @param traceLevel an <code>int</code> value
102    * @exception InvalidTraceLevelException if an error occurs
103    */

104     public static void setCurrentTraceLevel(int traceLevel) throws InvalidTraceLevelException
105     {
106         if(Configuration.traceOn)
107         {
108             int i;
109             boolean traceLevelSet = false;
110             for(i = 0; i <= TraceLevel.IAS_JTS_MAX_TRACE_LEVEL; i++)
111             {
112                 if(traceLevel == i)
113                 {
114                     m_currentTraceLevel = traceLevel;
115                     traceLevelSet = true;
116                     break;
117                 }
118             }
119             if(!traceLevelSet)
120                 throw new InvalidTraceLevelException();
121
122         }
123     }
124
125   /**
126    * This method formats and writes the trace record to output writer. The method is called
127    * with a tracelevel, which is checked with current trace level and if found equal or higher,
128    * the print is carried out. This method takes an PrintWriter also, which is used to write the
129    * output. This given outputWriter would override the set outputWriter. The origin object is printed
130    * using its toString() method.
131    * @param traceLevel an <code>int</code> value
132    * @param outWriter an <code>PrintWriter</code> value
133    * @param tid an <code>Object</code> value
134    * @param origin an <code>Object</code> value
135    * @param msg a <code>String</code> value
136    */

137     public static void print(int traceLevel, PrintWriter outWriter, Object JavaDoc tid, Object JavaDoc origin, String JavaDoc msg)
138     {
139             if( traceLevel <= m_currentTraceLevel )
140             {
141                 String JavaDoc traceRecord = TraceRecordFormatter.createTraceRecord(tid, origin, msg);
142         outWriter.println(traceRecord);
143             }
144     }
145
146   /**
147    * This method formats and writes the trace record to current output writer. The method is
148    * called with a tracelevel, which is checked with current trace level and if found equal
149    * or higher, the print is carried out. This method doesn't take a otid and tries to recover
150    * it from current obejct asscociated with this thread
151    * @param traceLevel an <code>int</code> value
152    * @param origin an <code>Object</code> value
153    * @param msg a <code>String</code> value
154    */

155     public static void print(int traceLevel, Object JavaDoc origin, String JavaDoc msg)
156     {
157     try{
158          print(traceLevel,
159               ((com.sun.jts.CosTransactions.TopCoordinator)
160           com.sun.jts.CosTransactions.CurrentTransaction.getCurrent().get_localCoordinator()).get_transaction_name(),
161           origin,
162           msg
163           );
164     }catch(Exception JavaDoc e){
165             print(traceLevel,null,origin,msg);
166     }
167     }
168
169   /**
170    * This method formats and writes the trace record to current output writer. The method is called
171    * with a tracelevel, which is checked with current trace level and if found equal or higher,
172    * the print is carried out. This uses the currently set output writer to write the trace output.
173    * @param traceLevel an <code>int</code> value
174    * @param tid an <code>Object</code> value
175    * @param origin an <code>Object</code> value
176    * @param msg a <code>String</code> value
177    */

178     public static void print(int traceLevel, Object JavaDoc tid, Object JavaDoc origin, String JavaDoc msg)
179     {
180         print(traceLevel,m_traceWriter,tid,origin,msg);
181     }
182
183   /**
184    * Gets the current field delimiter used in formatting trace record. The default is ':'.
185    *
186    * @return a <code>char</code> value
187    */

188     public static char getFieldDelimiter()
189     {
190         return m_fieldDelimiter;
191      }
192
193   /**
194    * Sets the current field delimiter.
195    *
196    * @param delimiter a <code>char</code> value
197    */

198     public static void setFieldDelimiter(char delimiter)
199     {
200         m_fieldDelimiter = delimiter;
201     }
202
203   /**
204    * Gets the current trace record tag used in formatting of trace record. The default is
205    * 'iAS_JTS_Trace> '.
206    *
207    * @return a <code>String</code> value
208    */

209     public static String JavaDoc getTraceRecordTag()
210     {
211         return m_traceRecordTag;
212      }
213
214   /**
215    * Sets the trace record tag.
216    *
217    * @param traceRecordTag a <code>String</code> value
218    */

219     public static void setTraceRecordTag(String JavaDoc traceRecordTag)
220     {
221         m_traceRecordTag = traceRecordTag;
222     }
223 }
224
Popular Tags