mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Fabio Pistolesi
14.08.2016 74fea9c73aa679eebe68f78d34ae80fa0f263c24
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * The contents of this file are subject to the terms of the Common Development and
 * Distribution License (the License). You may not use this file except in compliance with the
 * License.
 *
 * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
 * specific language governing permission and limitations under the License.
 *
 * When distributing Covered Software, include this CDDL Header Notice in each file and include
 * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
 * Header, with the fields enclosed by brackets [] replaced by your own identifying
 * information: "Portions Copyright [year] [name of copyright owner]".
 *
 * Copyright 2006-2009 Sun Microsystems, Inc.
 * Portions Copyright 2014-2015 ForgeRock AS.
 */
package org.opends.server.loggers;
 
import static org.opends.server.loggers.TraceSettings.Level.*;
 
import java.util.Map;
 
import org.forgerock.i18n.slf4j.LocalizedLogger;
 
/**
 * Class for source-code tracing at the method level.
 *
 * One DebugTracer instance exists for each Java class using tracing.
 * Tracer must be registered with the DebugLogger.
 *
 * Logging is always done at a level basis, with debug log messages
 * exceeding the trace threshold being traced, others being discarded.
 */
public class DebugTracer
{
  /**
   *  We have to hardcode this value because we cannot import
   *  {@code org.opends.server.loggers.slf4j.OpenDJLoggerAdapter.class.getName()}
   *  to avoid OSGI split package issues.
   *  @see OPENDJ-2226
   */
  private static final String OPENDJ_LOGGER_ADAPTER_CLASS_NAME = "org.opends.server.loggers.slf4j.OpenDJLoggerAdapter";
 
  /** The class this aspect traces. */
  private String className;
 
  /** A class that represents a settings cache entry. */
  private class PublisherSettings
  {
    private final DebugLogPublisher<?> debugPublisher;
    private final TraceSettings classSettings;
    private final Map<String, TraceSettings> methodSettings;
 
    private PublisherSettings(String className, DebugLogPublisher<?> publisher)
    {
      debugPublisher = publisher;
      classSettings = publisher.getClassSettings(className);
      methodSettings = publisher.getMethodSettings(className);
    }
 
    @Override
    public String toString()
    {
      return getClass().getSimpleName() + "("
          + "className=" + className
          + ", classSettings=" + classSettings
          + ", methodSettings=" + methodSettings
          + ")";
    }
  }
 
  private PublisherSettings[] publisherSettings;
 
  /**
   * Construct a new DebugTracer object with cached settings obtained from
   * the provided array of publishers.
   *
   * @param className The class name to use as category for logging.
   * @param publishers The array of publishers to obtain the settings from.
   */
  DebugTracer(String className, DebugLogPublisher<?>[] publishers)
  {
    this.className = className;
    publisherSettings = toPublisherSettings(publishers);
  }
 
  /**
   * Log the provided message.
   *
   * @param msg
   *          message to log.
   */
  public void trace(String msg)
  {
    traceException(msg, null);
  }
 
  /**
   * Log the provided message and exception.
   *
   * @param msg
   *          the message
   * @param exception
   *          the exception caught. May be {@code null}.
   */
  public void traceException(String msg, Throwable exception)
  {
    StackTraceElement[] stackTrace = null;
    StackTraceElement[] filteredStackTrace = null;
    StackTraceElement callerFrame = null;
    final boolean hasException = exception != null;
    for (PublisherSettings settings : publisherSettings)
    {
      TraceSettings activeSettings = settings.classSettings;
      Map<String, TraceSettings> methodSettings = settings.methodSettings;
 
      if (shouldLog(activeSettings, hasException) || methodSettings != null)
      {
        if (stackTrace == null)
        {
          stackTrace = Thread.currentThread().getStackTrace();
        }
        if (callerFrame == null)
        {
          callerFrame = getCallerFrame(stackTrace);
        }
 
        String signature = callerFrame.getMethodName();
 
        // Specific method settings still could exist. Try getting
        // the settings for this method.
        if (methodSettings != null)
        {
          TraceSettings mSettings = methodSettings.get(signature);
          if (mSettings == null)
          {
            // Try looking for an undecorated method name
            int idx = signature.indexOf('(');
            if (idx != -1)
            {
              mSettings = methodSettings.get(signature.substring(0, idx));
            }
          }
 
          // If this method does have a specific setting
          // and it is not supposed to be logged, continue.
          if (!shouldLog(mSettings, hasException))
          {
            continue;
          }
          activeSettings = mSettings;
        }
 
        String sourceLocation = callerFrame.getFileName() + ":" + callerFrame.getLineNumber();
 
        if (filteredStackTrace == null && activeSettings.getStackDepth() > 0)
        {
          StackTraceElement[] trace = hasException ? exception.getStackTrace() : stackTrace;
          filteredStackTrace = DebugStackTraceFormatter.SMART_FRAME_FILTER.getFilteredStackTrace(trace);
        }
 
        if (hasException)
        {
          settings.debugPublisher.traceException(activeSettings, signature,
              sourceLocation, msg, exception, filteredStackTrace);
        }
        else
        {
          settings.debugPublisher.trace(activeSettings, signature,
              sourceLocation, msg, filteredStackTrace);
        }
      }
    }
  }
 
  /**
   * Gets the name of the class this tracer traces.
   *
   * @return The name of the class this tracer traces.
   */
  String getTracedClassName()
  {
    return className;
  }
 
  /**
   * Indicates if logging is enabled for at least one category
   * in a publisher.
   *
   * @return {@code true} if logging is enabled, false otherwise.
   */
  public boolean enabled()
  {
    for (PublisherSettings settings : publisherSettings)
    {
      if (shouldLog(settings.classSettings) || settings.methodSettings != null)
      {
        return true;
      }
    }
    return false;
  }
 
  /**
   * Update the cached settings of the tracer with the settings from the
   * provided publishers.
   *
   * @param publishers The array of publishers to obtain the settings from.
   */
  void updateSettings(DebugLogPublisher<?>[] publishers)
  {
    publisherSettings = toPublisherSettings(publishers);
  }
 
  private PublisherSettings[] toPublisherSettings(DebugLogPublisher<?>[] publishers)
  {
    // Get the settings from all publishers.
    PublisherSettings[] newSettings = new PublisherSettings[publishers.length];
    for(int i = 0; i < publishers.length; i++)
    {
      newSettings[i] = new PublisherSettings(className, publishers[i]);
    }
    return newSettings;
  }
 
  /**
   * Return the caller stack frame.
   *
   * @param stackTrace
   *          The stack trace frames of the caller.
   * @return the caller stack frame or null if none is found on the stack trace.
   */
  private StackTraceElement getCallerFrame(StackTraceElement[] stackTrace)
  {
    if (stackTrace != null && stackTrace.length > 0)
    {
      // Skip all logging related classes
      for (StackTraceElement aStackTrace : stackTrace)
      {
        if(!isLoggingStackTraceElement(aStackTrace))
        {
          return aStackTrace;
        }
      }
    }
 
    return null;
  }
 
  /**
   * Checks if element belongs to a class responsible for logging
   * (includes the Thread class that may be used to get the stack trace).
   *
   * @param trace
   *            the trace element to check.
   * @return {@code true} if element corresponds to logging
   */
  static boolean isLoggingStackTraceElement(StackTraceElement trace)
  {
    String name = trace.getClassName();
    return name.startsWith(Thread.class.getName())
        || name.startsWith(DebugTracer.class.getName())
        || name.startsWith(OPENDJ_LOGGER_ADAPTER_CLASS_NAME)
        || name.startsWith(LocalizedLogger.class.getName());
  }
 
  /** Indicates if there is something to log. */
  private boolean shouldLog(TraceSettings settings, boolean hasException)
  {
    return settings != null
        && (settings.getLevel() == ALL
          || (hasException && settings.getLevel() == EXCEPTIONS_ONLY));
  }
 
  /** Indicates if there is something to log. */
  private boolean shouldLog(TraceSettings settings)
  {
    return settings.getLevel() != DISABLED;
  }
}