| 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 | public class JulTimingTracer |
|---|
| 50 | implements Tracer |
|---|
| 51 | { |
|---|
| 52 | /** The backing java logger. */ |
|---|
| 53 | 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) |
|---|
| 65 | { |
|---|
| 66 | mLogger = Logger.getLogger(tracerName); |
|---|
| 67 | mClassName = tracerName; |
|---|
| 68 | } |
|---|
| 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 | { |
|---|
| 78 | 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 | public boolean isTracing () |
|---|
| 88 | { |
|---|
| 89 | return mLogger.isLoggable(Level.FINER); |
|---|
| 90 | } |
|---|
| 91 | |
|---|
| 92 | /** |
|---|
| 93 | * Returns always true. |
|---|
| 94 | * @see org.jcoderz.commons.tracing.Tracer#isTracingArguments() |
|---|
| 95 | */ |
|---|
| 96 | public boolean isTracingArguments () |
|---|
| 97 | { |
|---|
| 98 | 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 | { |
|---|
| 107 | 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 | public TracingToken entering (String className, String methodName) |
|---|
| 117 | { |
|---|
| 118 | final TracingToken result; |
|---|
| 119 | if (isTracing()) |
|---|
| 120 | { |
|---|
| 121 | mLogger.entering(getClassName(), methodName); |
|---|
| 122 | result = new JulTimingTracingToken(methodName); |
|---|
| 123 | } |
|---|
| 124 | else |
|---|
| 125 | { |
|---|
| 126 | result = null; |
|---|
| 127 | } |
|---|
| 128 | 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 | public TracingToken entering (String className, String methodName, |
|---|
| 138 | Object argument) |
|---|
| 139 | { |
|---|
| 140 | final TracingToken result; |
|---|
| 141 | if (isTracing()) |
|---|
| 142 | { |
|---|
| 143 | mLogger.entering(getClassName(), methodName, argument); |
|---|
| 144 | result = new JulTimingTracingToken(methodName); |
|---|
| 145 | } |
|---|
| 146 | else |
|---|
| 147 | { |
|---|
| 148 | result = null; |
|---|
| 149 | } |
|---|
| 150 | 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 | public TracingToken entering (String className, String methodName, |
|---|
| 160 | Object[] arguments) |
|---|
| 161 | { |
|---|
| 162 | final TracingToken result; |
|---|
| 163 | if (isTracing()) |
|---|
| 164 | { |
|---|
| 165 | mLogger.entering(getClassName(), methodName, arguments); |
|---|
| 166 | result = new JulTimingTracingToken(methodName); |
|---|
| 167 | } |
|---|
| 168 | else |
|---|
| 169 | { |
|---|
| 170 | result = null; |
|---|
| 171 | } |
|---|
| 172 | 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 | public void exiting (TracingToken token) |
|---|
| 184 | { |
|---|
| 185 | if (token instanceof JulTimingTracingToken) |
|---|
| 186 | { |
|---|
| 187 | final JulTimingTracingToken julTracingToken |
|---|
| 188 | = (JulTimingTracingToken) token; |
|---|
| 189 | mLogger.logp( |
|---|
| 190 | Level.FINER, mClassName, julTracingToken.getMethodName(), |
|---|
| 191 | "RETURN (after {0}ms)", |
|---|
| 192 | new Long(julTracingToken.getElapsedMillis())); |
|---|
| 193 | mLogger.exiting(getClassName(), julTracingToken.getMethodName()); |
|---|
| 194 | } |
|---|
| 195 | } |
|---|
| 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 | public void exiting (TracingToken token, Object result) |
|---|
| 206 | { |
|---|
| 207 | if (token instanceof JulTimingTracingToken) |
|---|
| 208 | { |
|---|
| 209 | final JulTimingTracingToken julTracingToken |
|---|
| 210 | = (JulTimingTracingToken) token; |
|---|
| 211 | 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 | } |
|---|
| 219 | } |
|---|
| 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 | public void throwing (TracingToken token, Throwable thrown) |
|---|
| 230 | { |
|---|
| 231 | if (token instanceof JulTimingTracingToken) |
|---|
| 232 | { |
|---|
| 233 | final JulTimingTracingToken julTracingToken |
|---|
| 234 | = (JulTimingTracingToken) token; |
|---|
| 235 | final LogRecord record |
|---|
| 236 | = new LogRecord(Level.FINER, "THROW (after {0}ms)"); |
|---|
| 237 | record.setSourceClassName(mClassName); |
|---|
| 238 | record.setSourceMethodName(julTracingToken.getMethodName()); |
|---|
| 239 | record.setParameters( |
|---|
| 240 | new Object[] {new Long(julTracingToken.getElapsedMillis())}); |
|---|
| 241 | record.setThrown(thrown); |
|---|
| 242 | mLogger.log(record); |
|---|
| 243 | } |
|---|
| 244 | } |
|---|
| 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; |
|---|
| 254 | private final long mStartTime = System.currentTimeMillis(); |
|---|
| 255 | |
|---|
| 256 | public JulTimingTracingToken (String methodName) |
|---|
| 257 | { |
|---|
| 258 | mMethodName = methodName; |
|---|
| 259 | } |
|---|
| 260 | |
|---|
| 261 | public String getMethodName () |
|---|
| 262 | { |
|---|
| 263 | return mMethodName; |
|---|
| 264 | } |
|---|
| 265 | |
|---|
| 266 | public long getElapsedMillis () |
|---|
| 267 | { |
|---|
| 268 | // ignore the fact that this might overflow |
|---|
| 269 | return System.currentTimeMillis() - mStartTime; |
|---|
| 270 | } |
|---|
| 271 | |
|---|
| 272 | } |
|---|
| 273 | } |
|---|