View Javadoc

1   /*
2    * Licensed to the Apache Software Foundation (ASF) under one
3    * or more contributor license agreements.  See the NOTICE file
4    * distributed with this work for additional information
5    * regarding copyright ownership.  The ASF licenses this file
6    * to you under the Apache License, Version 2.0 (the
7    * "License"); you may not use this file except in compliance
8    * with the License.  You may obtain a copy of the License at
9    *
10   *   http://www.apache.org/licenses/LICENSE-2.0
11   *
12   * Unless required by applicable law or agreed to in writing,
13   * software distributed under the License is distributed on an
14   * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15   * KIND, either express or implied.  See the License for the
16   * specific language governing permissions and limitations
17   * under the License.
18   */
19  package org.apache.myfaces.shared.trace;
20  
21  import java.util.logging.Level;
22  import java.util.logging.Logger;
23  
24  /**
25   * Support class for tracing method calls providing params, exceptions and return types
26   */
27  public class TracingSupport
28  {
29      private static final Object[] EMPTY_PARAMS = new Object[0];
30  
31      private Logger _logger;
32      private Level _level = Level.FINE;
33      private String _sourceClass;
34  
35      public TracingSupport()
36      {
37          this(Logger.getAnonymousLogger());
38      }
39  
40      public TracingSupport(Class clazz)
41      {
42          this(clazz.getName());
43      }
44  
45      public TracingSupport(String className)
46      {
47          this(Logger.getLogger(className));
48      }
49  
50      public TracingSupport(Logger logger)
51      {
52          _logger = logger;
53          _sourceClass = logger.getName();
54      }
55  
56      /**
57       * @return the log level
58       */
59      public Level getLevel()
60      {
61          return _level;
62      }
63  
64      /**
65       * 
66       * @param level
67       *            the log level to use
68       */
69      public void setLevel(Level level)
70      {
71          if (level == null)
72          {
73              throw new IllegalArgumentException("log level can not be null");
74          }
75          _level = level;
76      }
77  
78      public <T> T trace(String methodName, Closure<T> closure)
79      {
80          return trace(methodName, closure, EMPTY_PARAMS);
81      }
82  
83      public <T> T trace(String methodName, Closure<T> closure, Object... params)
84      {
85          if (_logger.isLoggable(_level))
86          {
87              _logger.logp(_level, _sourceClass, methodName, "ENTRY" + prepareParams(params), params);
88              try
89              {
90                  T result = closure.call();
91                  if (!Void.class.equals(result))
92                  {
93                      _logger.logp(_level, _sourceClass, methodName, "RETURN {0}", result);
94                  }
95                  else
96                  {
97                      _logger.logp(_level, _sourceClass, methodName, "RETURN");
98                  }
99                  return result;
100             }
101             catch (RuntimeException e)
102             {
103                 _logger.logp(_level, _sourceClass, methodName, "THROW", e);
104                 throw e;
105             }
106         }
107         else
108         {
109             return closure.call();
110         }
111     }
112 
113     private String prepareParams(Object[] params)
114     {
115         if (params == null || params.length == 0)
116         {
117             return "";
118         }
119         StringBuilder builder = new StringBuilder(" ");
120         for (int i = 0, size = params.length; i < size; i++)
121         {
122             builder.append("{");
123             builder.append(i);
124             builder.append("}");
125             if (i + 1 < size)
126             {
127                 builder.append(",");
128             }
129         }
130         return builder.toString();
131     }
132 
133     public Logger getLogger()
134     {
135         return _logger;
136     }
137 
138     /**
139      * @param logger
140      *            the logger to set
141      */
142     public void setLogger(Logger logger)
143     {
144         if (logger == null)
145         {
146             throw new IllegalArgumentException("logger must not be null");
147         }
148         _logger = logger;
149     }
150 
151     /**
152      * @return the className
153      */
154     public String getSourceClass()
155     {
156         return _sourceClass;
157     }
158 
159     /**
160      * @param className
161      *            the className to set
162      */
163     public void setSourceClass(String className)
164     {
165         if (className == null)
166         {
167             throw new IllegalArgumentException("className must not be null");
168         }
169         _sourceClass = className;
170     }
171 }