Project Report: fawkez

Packagesummary org.jcoderz.commons.tracing

org.jcoderz.commons.tracing.JulTimingTracer

LineHitsNoteSource
1  /*
2   * $Id: ArraysUtil.java 1011 2008-06-16 17:57:36Z amandel $
3   *
4   * Copyright 2006, The jCoderZ.org Project. All rights reserved.
5   *
6   * Redistribution and use in source and binary forms, with or without
7   * modification, are permitted provided that the following conditions are
8   * met:
9   *
10   *    * Redistributions of source code must retain the above copyright
11   *      notice, this list of conditions and the following disclaimer.
12   *    * Redistributions in binary form must reproduce the above
13   *      copyright notice, this list of conditions and the following
14   *      disclaimer in the documentation and/or other materials
15   *      provided with the distribution.
16   *    * Neither the name of the jCoderZ.org Project nor the names of
17   *      its contributors may be used to endorse or promote products
18   *      derived from this software without specific prior written
19   *      permission.
20   *
21   * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS "AS IS" AND
22   * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
24   * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS AND CONTRIBUTORS
25   * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26   * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27   * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR
28   * BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
29   * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
30   * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
31   * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
32   */
33  package org.jcoderz.commons.tracing;
34  
35  import java.util.logging.Level;
36  import java.util.logging.LogRecord;
37  import java.util.logging.Logger;
38  
39  /**
40   * A Tracer implementation that used {@link java.util.logging} entering
41   * exiting logger as tracing sink and adds timing information.
42   * The format of the added timing information is '(after {0}ms)', the
43   * precision relies on {@link System#currentTimeMillis()}.
44   * 
45   * See {@link Logger#entering(String, String)} for an example.
46   *
47   * @author Andreas Mandel
48   */
49 (1)public class JulTimingTracer
50      implements Tracer
51  {
52      /** The backing java logger. */
53 (2)    private final Logger mLogger;
54      /** Name of the class where this logger belongs to. */
55      private final String mClassName;
56      
57      
58      /**
59       * Create a new instance of this Tracer.
60       * The underlying java logger is instantly created. 
61       * @param tracerName the name of the tracer to be created, should be
62       *     equal to the name of the class that uses the tracer.
63       */
64      private JulTimingTracer (String tracerName)
650     {
660         mLogger = Logger.getLogger(tracerName);
670         mClassName = tracerName;
680     }
69  
70      /**
71       * Factory for a tracer for classes with the given name.
72       * @param className the name of the class that uses this tracer.
73       * @return a tracer for the given class that dumps trace output to
74       *      java logging.
75       */
76      public static JulTimingTracer getTracer (String className)
77      {
780         return new JulTimingTracer(className);
79      }
80  
81  
82      /**
83       * Returns true, if the level of the underlying java logger is
84       * {@link Level#FINER} or below.
85       * @see org.jcoderz.commons.tracing.Tracer#isTracing()
86       */
87 (3)    public boolean isTracing ()
88      {
890         return mLogger.isLoggable(Level.FINER);
90      }
91  
92      /**
93       * Returns always true.
94       * @see org.jcoderz.commons.tracing.Tracer#isTracingArguments()
95       */
96 (4)    public boolean isTracingArguments ()
97      {
980         return true;
99      }
100  
101      /**
102       * Returns the Name of the class where this tracer belongs to.
103       * @return the Name of the class where this tracer belongs to.
104       */
105      public String getClassName ()
106      {
1070         return mClassName;
108      }
109  
110      /**
111       * Delegates to {@link Logger#entering(String, String)}.
112       * The className is expected to be the same as the tracer name and 
113       * is silently ignored.
114       * @see Tracer#entering(java.lang.String, java.lang.String)
115       */
116 (5)(6)(7)    public TracingToken entering (String className, String methodName)
117      {
118          final TracingToken result;
1190         if (isTracing())
120          {
1210             mLogger.entering(getClassName(), methodName);
1220             result = new JulTimingTracingToken(methodName);
123          }
124          else
125          {
1260             result = null;
127          }
1280         return result;
129      }
130  
131      /**
132       * Delegates to {@link Logger#entering(String, String, Object)}.
133       * The className is expected to be the same as the tracer name and 
134       * is silently ignored.
135       * @see Tracer#entering(String, String, Object)
136       */
137 (8)(9)(10)    public TracingToken entering (String className, String methodName,
138 (11)        Object argument)
139      {
140          final TracingToken result;
1410         if (isTracing())
142          {
1430             mLogger.entering(getClassName(), methodName, argument);
1440             result = new JulTimingTracingToken(methodName);
145          }
146          else
147          {
1480             result = null;
149          }
1500         return result;
151      }
152  
153      /**
154       * Delegates to {@link Logger#entering(String, String, Object)}.
155       * The className is expected to be the same as the tracer name and 
156       * is silently ignored.
157       * @see Tracer#entering(String, String, Object[])
158       */
159 (12)(13)(14)    public TracingToken entering (String className, String methodName,
160 (15)        Object[] arguments)
161      {
162          final TracingToken result;
1630         if (isTracing())
164          {
1650             mLogger.entering(getClassName(), methodName, arguments);
1660             result = new JulTimingTracingToken(methodName);
167          }
168          else
169          {
1700             result = null;
171          }
1720         return result;
173      }
174  
175      /**
176       * Logs the method exit like 
177       * {@link Logger#exiting(String, String)} with additional
178       * execution time information.
179       * If the token is not a {@link JulTimingTracingToken} the call is
180       * silently ignored.
181       * @see Tracer#exiting(TracingToken)
182       */
183 (16)(17)    public void exiting (TracingToken token)
184      {
1850         if (token instanceof JulTimingTracingToken)
186          {
1870             final JulTimingTracingToken julTracingToken
188                  = (JulTimingTracingToken) token;
1890             mLogger.logp(
190                  Level.FINER, mClassName, julTracingToken.getMethodName(),
191                  "RETURN (after {0}ms)",
192                  new Long(julTracingToken.getElapsedMillis()));
1930             mLogger.exiting(getClassName(), julTracingToken.getMethodName());
194          }
1950     }
196  
197      /**
198       * Logs the returned result like 
199       * {@link Logger#exiting(String, String, Object)} with additional
200       * execution time information.
201       * If the token is not a {@link JulTimingTracingToken} the call is
202       * silently ignored.
203       * @see Tracer#exiting(TracingToken, java.lang.Object)
204       */
205 (18)(19)(20)    public void exiting (TracingToken token, Object result)
206      {
2070         if (token instanceof JulTimingTracingToken)
208          {
2090             final JulTimingTracingToken julTracingToken
210                  = (JulTimingTracingToken) token;
2110             mLogger.logp(
212                  Level.FINER, mClassName, julTracingToken.getMethodName(),
213                  "RETURN {0} (after {1}ms)",
214                  new Object[]
215                  {
216                      result, new Long(julTracingToken.getElapsedMillis())
217                  });
218          }
2190     }
220  
221      /**
222       * Logs the thrown exception like 
223       * {@link Logger#throwing(String, String, Throwable)} with additional
224       * execution time information.
225       * If the token is not a {@link JulTimingTracingToken} the call is
226       * silently ignored.
227       * @see Tracer#throwing(TracingToken, java.lang.Throwable)
228       */
229 (21)(22)(23)    public void throwing (TracingToken token, Throwable thrown)
230      {
2310         if (token instanceof JulTimingTracingToken)
232          {
2330             final JulTimingTracingToken julTracingToken
234                  = (JulTimingTracingToken) token;
2350             final LogRecord record
236                  = new LogRecord(Level.FINER, "THROW (after {0}ms)");
2370             record.setSourceClassName(mClassName);
2380             record.setSourceMethodName(julTracingToken.getMethodName());
2390             record.setParameters(
240                  new Object[] {new Long(julTracingToken.getElapsedMillis())});
2410             record.setThrown(thrown);
2420             mLogger.log(record);
243          }
2440     }
245      
246      /**
247       * Stores context information for the {@link JulTimingTracer}.
248       * @see TracingToken
249       * @author Andreas Mandel
250       */
251      private class JulTimingTracingToken implements TracingToken
252      {
253          private final String mMethodName;
2540(24)        private final long mStartTime = System.currentTimeMillis();
255          
256          public JulTimingTracingToken (String methodName)
2570         {
2580             mMethodName = methodName;
2590         }
260  
261          public String getMethodName ()
262          {
2630             return mMethodName;
264          }
265  
266          public long getElapsedMillis ()
267          {
268              // ignore the fact that this might overflow
2690             return System.currentTimeMillis() - mStartTime;
270          }
271          
272      }
273  }

Findings in this File

c (1) 49 : 0 Class JulTimingTracer should be declared as final.
d (2) 53 : 26 The Logger variable declaration does not contain the static and final modifiers
c (3) 87 : 0 Expected an @return tag.
c (4) 96 : 0 Expected an @return tag.
c (5) 116 : 0 Expected an @return tag.
c (6) 116 : 42 Expected @param tag for 'className'.
c (7) 116 : 60 Expected @param tag for 'methodName'.
c (8) 137 : 0 Expected an @return tag.
c (9) 137 : 42 Expected @param tag for 'className'.
c (10) 137 : 60 Expected @param tag for 'methodName'.
c (11) 138 : 16 Expected @param tag for 'argument'.
c (12) 159 : 0 Expected an @return tag.
c (13) 159 : 42 Expected @param tag for 'className'.
c (14) 159 : 60 Expected @param tag for 'methodName'.
c (15) 160 : 18 Expected @param tag for 'arguments'.
c (16) 183 : 39 Expected @param tag for 'token'.
d (17) 183 : 0 Tag @link: reference not found: JulTimingTracingToken
c (18) 205 : 39 Expected @param tag for 'token'.
c (19) 205 : 53 Expected @param tag for 'result'.
d (20) 205 : 0 Tag @link: reference not found: JulTimingTracingToken
c (21) 229 : 40 Expected @param tag for 'token'.
c (22) 229 : 57 Expected @param tag for 'thrown'.
d (23) 229 : 0 Tag @link: reference not found: JulTimingTracingToken
w (24) 254 : 0 Should org.jcoderz.commons.tracing.JulTimingTracer$JulTimingTracingToken be a _static_ inner class?