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   * @author Mathias Broekelmann (latest modification by $Author: lu4242 $)
28   * @version $Revision: 1151677 $ $Date: 2011-07-27 19:03:59 -0500 (Wed, 27 Jul 2011) $
29   */
30  public class TracingSupport
31  {
32      private static final Object[] EMPTY_PARAMS = new Object[0];
33  
34      private Logger _logger;
35      private Level _level = Level.FINE;
36      private String _sourceClass;
37  
38      public TracingSupport()
39      {
40          this(Logger.getAnonymousLogger());
41      }
42  
43      public TracingSupport(Class clazz)
44      {
45          this(clazz.getName());
46      }
47  
48      public TracingSupport(String className)
49      {
50          this(Logger.getLogger(className));
51      }
52  
53      public TracingSupport(Logger logger)
54      {
55          _logger = logger;
56          _sourceClass = logger.getName();
57      }
58  
59      /**
60       * @return the log level
61       */
62      public Level getLevel()
63      {
64          return _level;
65      }
66  
67      /**
68       * 
69       * @param level
70       *            the log level to use
71       */
72      public void setLevel(Level level)
73      {
74          if (level == null)
75          {
76              throw new IllegalArgumentException("log level can not be null");
77          }
78          _level = level;
79      }
80  
81      public <T> T trace(String methodName, Closure<T> closure)
82      {
83          return trace(methodName, closure, EMPTY_PARAMS);
84      }
85  
86      public <T> T trace(String methodName, Closure<T> closure, Object... params)
87      {
88          if (_logger.isLoggable(_level))
89          {
90              _logger.logp(_level, _sourceClass, methodName, "ENTRY" + prepareParams(params), params);
91              try
92              {
93                  T result = closure.call();
94                  if (!Void.class.equals(result))
95                  {
96                      _logger.logp(_level, _sourceClass, methodName, "RETURN {0}", result);
97                  }
98                  else
99                  {
100                     _logger.logp(_level, _sourceClass, methodName, "RETURN");
101                 }
102                 return result;
103             }
104             catch (RuntimeException e)
105             {
106                 _logger.logp(_level, _sourceClass, methodName, "THROW", e);
107                 throw e;
108             }
109         }
110         else
111         {
112             return closure.call();
113         }
114     }
115 
116     private String prepareParams(Object[] params)
117     {
118         if (params == null || params.length == 0)
119         {
120             return "";
121         }
122         StringBuilder builder = new StringBuilder(" ");
123         for (int i = 0, size = params.length; i < size; i++)
124         {
125             builder.append("{");
126             builder.append(i);
127             builder.append("}");
128             if (i + 1 < size)
129             {
130                 builder.append(",");
131             }
132         }
133         return builder.toString();
134     }
135 
136     public Logger getLogger()
137     {
138         return _logger;
139     }
140 
141     /**
142      * @param logger
143      *            the logger to set
144      */
145     public void setLogger(Logger logger)
146     {
147         if (logger == null)
148         {
149             throw new IllegalArgumentException("logger must not be null");
150         }
151         _logger = logger;
152     }
153 
154     /**
155      * @return the className
156      */
157     public String getSourceClass()
158     {
159         return _sourceClass;
160     }
161 
162     /**
163      * @param className
164      *            the className to set
165      */
166     public void setSourceClass(String className)
167     {
168         if (className == null)
169         {
170             throw new IllegalArgumentException("className must not be null");
171         }
172         _sourceClass = className;
173     }
174 }