| 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 org.slf4j.Logger; |
|---|
| 36 | import org.slf4j.LoggerFactory; |
|---|
| 37 | |
|---|
| 38 | |
|---|
| 39 | /** |
|---|
| 40 | * A Tracer implementation that used {@link org.slf4j.Logger} |
|---|
| 41 | * as tracing sink. |
|---|
| 42 | * See {@link java.util.logging.Logger#entering(String, String)} for an |
|---|
| 43 | * example of the format and semantic used. |
|---|
| 44 | * |
|---|
| 45 | * TODO: Need Marker for classname & methodname? How? |
|---|
| 46 | * |
|---|
| 47 | * @author Andreas Mandel |
|---|
| 48 | */ |
|---|
| 49 | public class Slf4jTracer |
|---|
| 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 Slf4jTracer (String tracerName) |
|---|
| 65 | { |
|---|
| 66 | mLogger = LoggerFactory.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 Slf4jTracer getTracer (String className) |
|---|
| 77 | { |
|---|
| 78 | return new Slf4jTracer(className); |
|---|
| 79 | } |
|---|
| 80 | |
|---|
| 81 | |
|---|
| 82 | /** |
|---|
| 83 | * Returns true, if the level of the underlying slf4j logger has |
|---|
| 84 | * trace enabled. |
|---|
| 85 | * @see Logger#isTraceEnabled() |
|---|
| 86 | */ |
|---|
| 87 | public boolean isTracing () |
|---|
| 88 | { |
|---|
| 89 | return mLogger.isTraceEnabled(); |
|---|
| 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#trace(org.slf4j.Marker, 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.trace(/* getClassName(), methodName, */ "ENTRY"); |
|---|
| 122 | result = new Slf4jTracingToken(methodName); |
|---|
| 123 | } |
|---|
| 124 | else |
|---|
| 125 | { |
|---|
| 126 | result = null; |
|---|
| 127 | } |
|---|
| 128 | return result; |
|---|
| 129 | } |
|---|
| 130 | |
|---|
| 131 | /** |
|---|
| 132 | * Delegates to {@link Logger#trace(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.debug(/* getClassName(), methodName, */ "ENTRY {}", |
|---|
| 144 | argument); |
|---|
| 145 | result = new Slf4jTracingToken(methodName); |
|---|
| 146 | } |
|---|
| 147 | else |
|---|
| 148 | { |
|---|
| 149 | result = null; |
|---|
| 150 | } |
|---|
| 151 | return result; |
|---|
| 152 | } |
|---|
| 153 | |
|---|
| 154 | /** |
|---|
| 155 | * Delegates to {@link Logger#trace(String, String, Object)}. |
|---|
| 156 | * The className is expected to be the same as the tracer name and |
|---|
| 157 | * is silently ignored. |
|---|
| 158 | * @see Tracer#entering(String, String, Object[]) |
|---|
| 159 | */ |
|---|
| 160 | public TracingToken entering ( |
|---|
| 161 | String className, String methodName, Object[] arguments) |
|---|
| 162 | { |
|---|
| 163 | final TracingToken result; |
|---|
| 164 | if (isTracing()) |
|---|
| 165 | { |
|---|
| 166 | String msg = "ENTRY"; |
|---|
| 167 | if (arguments == null ) |
|---|
| 168 | { |
|---|
| 169 | result = entering(className, methodName); |
|---|
| 170 | } |
|---|
| 171 | else |
|---|
| 172 | { |
|---|
| 173 | for (int i = 0; i < arguments.length; i++) |
|---|
| 174 | { |
|---|
| 175 | msg = msg + " {}"; |
|---|
| 176 | } |
|---|
| 177 | mLogger.trace(/*getClassName(), methodName,*/ msg, arguments); |
|---|
| 178 | result = new Slf4jTracingToken(methodName); |
|---|
| 179 | } |
|---|
| 180 | } |
|---|
| 181 | else |
|---|
| 182 | { |
|---|
| 183 | result = null; |
|---|
| 184 | } |
|---|
| 185 | return result; |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | /** |
|---|
| 189 | * Delegates to {@link Logger#trace(String, Object)} the message |
|---|
| 190 | * string used is "RETURN". |
|---|
| 191 | * If the token is not a {@link Slf4jTracingToken} the call is |
|---|
| 192 | * silently ignored. |
|---|
| 193 | * @see Tracer#exiting(TracingToken) |
|---|
| 194 | */ |
|---|
| 195 | public void exiting (TracingToken token) |
|---|
| 196 | { |
|---|
| 197 | if (token instanceof Slf4jTracingToken) |
|---|
| 198 | { |
|---|
| 199 | // final Slf4jTracingToken tt = (Slf4jTracingToken) token; |
|---|
| 200 | mLogger.trace(/* getClassName(), tt.getMethodName(), */ "RETURN"); |
|---|
| 201 | } |
|---|
| 202 | } |
|---|
| 203 | |
|---|
| 204 | /** |
|---|
| 205 | * Delegates to {@link Logger#trace(String, Object)}. |
|---|
| 206 | * If the token is not a {@link Slf4jTracingToken} the call is |
|---|
| 207 | * silently ignored. |
|---|
| 208 | * @see Tracer#exiting(TracingToken, java.lang.Object) |
|---|
| 209 | */ |
|---|
| 210 | public void exiting (TracingToken token, Object result) |
|---|
| 211 | { |
|---|
| 212 | if (token instanceof Slf4jTracingToken) |
|---|
| 213 | { |
|---|
| 214 | // final Slf4jTracingToken tt = (Slf4jTracingToken) token; |
|---|
| 215 | mLogger.trace( |
|---|
| 216 | /* getClassName(), tt.getMethodName(), */ "RETURN {}", |
|---|
| 217 | result); |
|---|
| 218 | } |
|---|
| 219 | } |
|---|
| 220 | |
|---|
| 221 | /** |
|---|
| 222 | * Delegates to {@link Logger#throwing(String, String, Throwable)}. |
|---|
| 223 | * If the token is not a {@link Slf4jTracingToken} the call is |
|---|
| 224 | * silently ignored. |
|---|
| 225 | * @see Tracer#throwing(TracingToken, java.lang.Throwable) |
|---|
| 226 | */ |
|---|
| 227 | public void throwing (TracingToken token, Throwable thrown) |
|---|
| 228 | { |
|---|
| 229 | if (token instanceof Slf4jTracingToken) |
|---|
| 230 | { |
|---|
| 231 | // final Slf4jTracingToken tt = (Slf4jTracingToken) token; |
|---|
| 232 | mLogger.trace( |
|---|
| 233 | /* getClassName(), tt.getMethodName(), */ "THROW", |
|---|
| 234 | thrown); |
|---|
| 235 | } |
|---|
| 236 | } |
|---|
| 237 | |
|---|
| 238 | /** |
|---|
| 239 | * Stores context information for the {@link Slf4jTracer}. |
|---|
| 240 | * @see TracingToken |
|---|
| 241 | * @author Andreas Mandel |
|---|
| 242 | */ |
|---|
| 243 | private class Slf4jTracingToken implements TracingToken |
|---|
| 244 | { |
|---|
| 245 | private final String mMethodName; |
|---|
| 246 | |
|---|
| 247 | public Slf4jTracingToken (String methodName) |
|---|
| 248 | { |
|---|
| 249 | mMethodName = methodName; |
|---|
| 250 | } |
|---|
| 251 | |
|---|
| 252 | public String getMethodName () |
|---|
| 253 | { |
|---|
| 254 | return mMethodName; |
|---|
| 255 | } |
|---|
| 256 | |
|---|
| 257 | } |
|---|
| 258 | } |
|---|